CStringExtras: Prevent undefined behavior within IndexOfSubstring

Unlikely to occur, but does completely prevent the case of undefined
behavior if a non-ascii character ends up within the given string.
This commit is contained in:
Lioncash 2019-09-14 11:28:15 -04:00
parent 0d2b2f45a8
commit 991d048694
1 changed files with 5 additions and 3 deletions

View File

@ -29,10 +29,12 @@ public:
static int IndexOfSubstring(std::string_view haystack, std::string_view needle) { static int IndexOfSubstring(std::string_view haystack, std::string_view needle) {
std::string str(haystack); std::string str(haystack);
std::transform(str.begin(), str.end(), str.begin(), tolower); std::transform(str.begin(), str.end(), str.begin(),
std::string::size_type s = str.find(needle); [](char c) { return std::tolower(static_cast<unsigned char>(c)); });
if (s == std::string::npos) const std::string::size_type s = str.find(needle);
if (s == std::string::npos) {
return -1; return -1;
}
return s; return s;
} }
}; };