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, ...@@ -55,9 +55,15 @@ void SplitStringAlongWhitespace(const std::string &input,
bool HexStringToUInt(const std::string &input, unsigned int *uintOut) 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 // Simple validity check
if (input[0] != '0' || input[1] != 'x' || if (input.find_first_not_of("0123456789ABCDEFabcdef", offset) != std::string::npos)
input.find_first_not_of("0123456789ABCDEFabcdef", 2) != std::string::npos)
{ {
return false; return false;
} }
...@@ -70,13 +76,16 @@ bool HexStringToUInt(const std::string &input, unsigned int *uintOut) ...@@ -70,13 +76,16 @@ bool HexStringToUInt(const std::string &input, unsigned int *uintOut)
bool ReadFileToString(const std::string &path, std::string *stringOut) bool ReadFileToString(const std::string &path, std::string *stringOut)
{ {
std::ifstream inFile(path.c_str()); std::ifstream inFile(path.c_str());
std::string str; if (inFile.fail())
{
return false;
}
inFile.seekg(0, std::ios::end); 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); 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(); return !inFile.fail();
} }
......
...@@ -49,8 +49,12 @@ TEST(StringUtilsTest, SplitStringAlongWhitespaceBasic) ...@@ -49,8 +49,12 @@ TEST(StringUtilsTest, SplitStringAlongWhitespaceBasic)
// Basic functionality tests for HexStringToUInt // Basic functionality tests for HexStringToUInt
TEST(StringUtilsTest, HexStringToUIntBasic) TEST(StringUtilsTest, HexStringToUIntBasic)
{ {
std::string testStringA("0xBADF00D");
unsigned int uintValue; unsigned int uintValue;
std::string emptyString;
ASSERT_FALSE(HexStringToUInt(emptyString, &uintValue));
std::string testStringA("0xBADF00D");
ASSERT_TRUE(HexStringToUInt(testStringA, &uintValue)); ASSERT_TRUE(HexStringToUInt(testStringA, &uintValue));
EXPECT_EQ(0xBADF00Du, uintValue); EXPECT_EQ(0xBADF00Du, uintValue);
...@@ -58,7 +62,11 @@ TEST(StringUtilsTest, HexStringToUIntBasic) ...@@ -58,7 +62,11 @@ TEST(StringUtilsTest, HexStringToUIntBasic)
EXPECT_FALSE(HexStringToUInt(testStringB, &uintValue)); EXPECT_FALSE(HexStringToUInt(testStringB, &uintValue));
std::string testStringC("BADF00D"); 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 // 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