2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-05-13 13:51:21 +00:00
metaforce/Runtime/CStringExtras.hpp
Luke Street 57d96dbb17
Fully decouple hecl from Metaforce
- Added CStringExtras Convert functions
  (and UTF-compatible versions)
- GX header copied into Runtime
- SFX headers copied into Runtime/Audio
2022-02-21 08:01:06 -08:00

49 lines
2.0 KiB
C++

#pragma once
#include <algorithm>
#include <cctype>
#include <string>
namespace metaforce {
class CInputStream;
class CStringExtras {
public:
static std::string ConvertToANSI(std::u16string_view sv);
static std::u16string ConvertToUNICODE(std::string_view sv);
// Metaforce addition: UTF-8/16 compatible versions of the above
static std::string ConvertToUTF8(std::u16string_view sv);
static std::u16string ConvertToUTF16(std::string_view sv);
// Checks if the provided views into string data can be considered equal or not based on
// whether or not all their characters are equal to one another in a character insensitive manner.
//
// 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
// an int indicating whether or not the first argument is less than, equal to,
// 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) {
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
[](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); });
}
static int IndexOfSubstring(std::string_view haystack, std::string_view needle) {
std::string str(haystack);
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;
}
static std::string ReadString(CInputStream& in);
};
} // namespace metaforce