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/274030
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Tested-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/common/string_utils.cpp b/src/common/string_utils.cpp
index 68a96e9..0d3869c 100644
--- a/src/common/string_utils.cpp
+++ b/src/common/string_utils.cpp
@@ -55,9 +55,15 @@
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 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();
}