mirror of https://github.com/AxioDL/metaforce.git
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:
parent
0d2b2f45a8
commit
991d048694
|
@ -29,10 +29,12 @@ public:
|
|||
|
||||
static int IndexOfSubstring(std::string_view haystack, std::string_view needle) {
|
||||
std::string str(haystack);
|
||||
std::transform(str.begin(), str.end(), str.begin(), tolower);
|
||||
std::string::size_type s = str.find(needle);
|
||||
if (s == std::string::npos)
|
||||
std::transform(str.begin(), str.end(), str.begin(),
|
||||
[](char c) { return std::tolower(static_cast<unsigned char>(c)); });
|
||||
const std::string::size_type s = str.find(needle);
|
||||
if (s == std::string::npos) {
|
||||
return -1;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue