2018-10-07 03:42:33 +00:00
|
|
|
#pragma once
|
2015-08-23 06:42:29 +00:00
|
|
|
|
2019-09-14 15:14:51 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
2015-08-23 06:42:29 +00:00
|
|
|
#include <string>
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2022-02-18 07:37:54 +00:00
|
|
|
class CInputStream;
|
2018-12-08 05:30:43 +00:00
|
|
|
class CStringExtras {
|
2015-08-23 06:42:29 +00:00
|
|
|
public:
|
2019-09-14 15:14:51 +00:00
|
|
|
// Checks if the provided views into string data can be considered equal or not based on
|
2019-09-28 15:00:49 +00:00
|
|
|
// whether or not all their characters are equal to one another in a character insensitive manner.
|
2019-09-14 15:14:51 +00:00
|
|
|
//
|
|
|
|
// NOTE: This differs slightly from the actual version of this function within the game executable
|
|
|
|
// in order to better accomodate string views and potentially non-null-terminated string data.
|
|
|
|
//
|
|
|
|
// In the game executable, the function essentially behaves like strcasecmp in that it returns
|
2019-09-28 15:00:49 +00:00
|
|
|
// an int indicating whether or not the first argument is less than, equal to,
|
2019-09-14 15:14:51 +00:00
|
|
|
// or greater than the second argument. Given no usages in the code depend on the less than or
|
|
|
|
// greater than cases, but rather just care about whether or not the strings are equal to one
|
|
|
|
// another, this is a safe change to make.
|
|
|
|
//
|
|
|
|
static bool CompareCaseInsensitive(std::string_view a, std::string_view b) {
|
2021-06-07 19:29:18 +00:00
|
|
|
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
|
|
|
|
[](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); });
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-01-15 03:07:01 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
static int IndexOfSubstring(std::string_view haystack, std::string_view needle) {
|
|
|
|
std::string str(haystack);
|
2019-09-14 15:28:15 +00:00
|
|
|
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) {
|
2018-12-08 05:30:43 +00:00
|
|
|
return -1;
|
2019-09-14 15:28:15 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
return s;
|
|
|
|
}
|
2022-02-18 07:37:54 +00:00
|
|
|
|
|
|
|
static std::string ReadString(CInputStream& in);
|
2015-08-23 06:42:29 +00:00
|
|
|
};
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|