Commit c7be82ae by Jamie Madill

Change behaviour of string_utils, also fix file io.

File IO tested manually in a follow-up patch. BUG=angleproject:998 Change-Id: I0bfa778d1787cdedfbf3cd68cd134477e6dcd23c Reviewed-on: https://chromium-review.googlesource.com/274030Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org> Tested-by: 's avatarJamie Madill <jmadill@chromium.org>
parent 10eb591a
......@@ -55,9 +55,15 @@ void SplitStringAlongWhitespace(const std::string &input,
bool HexStringToUInt(const std::string &input, unsigned int *uintOut)
{
unsigned int offset = 0;
if (input.size() >= 2 && input[0] == '0' && input[1] == 'x')
{
offset = 2u;
}
// Simple validity check
if (input[0] != '0' || input[1] != 'x' ||
input.find_first_not_of("0123456789ABCDEFabcdef", 2) != std::string::npos)
if (input.find_first_not_of("0123456789ABCDEFabcdef", offset) != std::string::npos)
{
return false;
}
......@@ -70,13 +76,16 @@ bool HexStringToUInt(const std::string &input, unsigned int *uintOut)
bool ReadFileToString(const std::string &path, std::string *stringOut)
{
std::ifstream inFile(path.c_str());
std::string str;
if (inFile.fail())
{
return false;
}
inFile.seekg(0, std::ios::end);
str.reserve(static_cast<std::string::size_type>(inFile.tellg()));
stringOut->reserve(static_cast<std::string::size_type>(inFile.tellg()));
inFile.seekg(0, std::ios::beg);
str.assign(std::istreambuf_iterator<char>(inFile), std::istreambuf_iterator<char>());
stringOut->assign(std::istreambuf_iterator<char>(inFile), std::istreambuf_iterator<char>());
return !inFile.fail();
}
......
......@@ -49,8 +49,12 @@ TEST(StringUtilsTest, SplitStringAlongWhitespaceBasic)
// Basic functionality tests for HexStringToUInt
TEST(StringUtilsTest, HexStringToUIntBasic)
{
std::string testStringA("0xBADF00D");
unsigned int uintValue;
std::string emptyString;
ASSERT_FALSE(HexStringToUInt(emptyString, &uintValue));
std::string testStringA("0xBADF00D");
ASSERT_TRUE(HexStringToUInt(testStringA, &uintValue));
EXPECT_EQ(0xBADF00Du, uintValue);
......@@ -58,7 +62,11 @@ TEST(StringUtilsTest, HexStringToUIntBasic)
EXPECT_FALSE(HexStringToUInt(testStringB, &uintValue));
std::string testStringC("BADF00D");
EXPECT_FALSE(HexStringToUInt(testStringC, &uintValue));
EXPECT_TRUE(HexStringToUInt(testStringC, &uintValue));
EXPECT_EQ(0xBADF00Du, uintValue);
std::string testStringD("0x BADF00D");
EXPECT_FALSE(HexStringToUInt(testStringD, &uintValue));
}
// Note: ReadFileToString is harder to test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment