Codechange: replace strnatcmp with C++ string capable version
This commit is contained in:
@@ -322,15 +322,15 @@ void MacOSSetCurrentLocaleName(const char *iso_code)
|
||||
* @param s2 Second string to compare.
|
||||
* @return 1 if s1 < s2, 2 if s1 == s2, 3 if s1 > s2, or 0 if not supported by the OS.
|
||||
*/
|
||||
int MacOSStringCompare(const char *s1, const char *s2)
|
||||
int MacOSStringCompare(std::string_view s1, std::string_view s2)
|
||||
{
|
||||
static bool supported = MacOSVersionIsAtLeast(10, 5, 0);
|
||||
if (!supported) return 0;
|
||||
|
||||
CFStringCompareFlags flags = kCFCompareCaseInsensitive | kCFCompareNumerically | kCFCompareLocalized | kCFCompareWidthInsensitive | kCFCompareForcedOrdering;
|
||||
|
||||
CFAutoRelease<CFStringRef> cf1(CFStringCreateWithCString(kCFAllocatorDefault, s1, kCFStringEncodingUTF8));
|
||||
CFAutoRelease<CFStringRef> cf2(CFStringCreateWithCString(kCFAllocatorDefault, s2, kCFStringEncodingUTF8));
|
||||
CFAutoRelease<CFStringRef> cf1(CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8 *)s1.data(), s1.size(), kCFStringEncodingUTF8, false));
|
||||
CFAutoRelease<CFStringRef> cf2(CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8 *)s2.data(), s2.size(), kCFStringEncodingUTF8, false));
|
||||
|
||||
/* If any CFString could not be created (e.g., due to UTF8 invalid chars), return OS unsupported functionality */
|
||||
if (cf1 == nullptr || cf2 == nullptr) return 0;
|
||||
|
@@ -83,7 +83,7 @@ public:
|
||||
|
||||
void MacOSResetScriptCache(FontSize size);
|
||||
void MacOSSetCurrentLocaleName(const char *iso_code);
|
||||
int MacOSStringCompare(const char *s1, const char *s2);
|
||||
int MacOSStringCompare(std::string_view s1, std::string_view s2);
|
||||
|
||||
void MacOSRegisterExternalFont(const char *file_path);
|
||||
|
||||
|
@@ -567,7 +567,7 @@ void Win32SetCurrentLocaleName(const char *iso_code)
|
||||
MultiByteToWideChar(CP_UTF8, 0, iso, -1, _cur_iso_locale, lengthof(_cur_iso_locale));
|
||||
}
|
||||
|
||||
int OTTDStringCompare(const char *s1, const char *s2)
|
||||
int OTTDStringCompare(std::string_view s1, std::string_view s2)
|
||||
{
|
||||
typedef int (WINAPI *PFNCOMPARESTRINGEX)(LPCWSTR, DWORD, LPCWCH, int, LPCWCH, int, LPVOID, LPVOID, LPARAM);
|
||||
static PFNCOMPARESTRINGEX _CompareStringEx = nullptr;
|
||||
@@ -588,15 +588,15 @@ int OTTDStringCompare(const char *s1, const char *s2)
|
||||
|
||||
if (_CompareStringEx != nullptr) {
|
||||
/* CompareStringEx takes UTF-16 strings, even in ANSI-builds. */
|
||||
int len_s1 = MultiByteToWideChar(CP_UTF8, 0, s1, -1, nullptr, 0);
|
||||
int len_s2 = MultiByteToWideChar(CP_UTF8, 0, s2, -1, nullptr, 0);
|
||||
int len_s1 = MultiByteToWideChar(CP_UTF8, 0, s1.data(), (int)s1.size(), nullptr, 0);
|
||||
int len_s2 = MultiByteToWideChar(CP_UTF8, 0, s2.data(), (int)s2.size(), nullptr, 0);
|
||||
|
||||
if (len_s1 != 0 && len_s2 != 0) {
|
||||
std::wstring str_s1(len_s1, L'\0'); // len includes terminating null
|
||||
std::wstring str_s2(len_s2, L'\0');
|
||||
|
||||
MultiByteToWideChar(CP_UTF8, 0, s1, -1, str_s1.data(), len_s1);
|
||||
MultiByteToWideChar(CP_UTF8, 0, s2, -1, str_s2.data(), len_s2);
|
||||
MultiByteToWideChar(CP_UTF8, 0, s1.data(), (int)s1.size(), str_s1.data(), len_s1);
|
||||
MultiByteToWideChar(CP_UTF8, 0, s2.data(), (int)s2.size(), str_s2.data(), len_s2);
|
||||
|
||||
int result = _CompareStringEx(_cur_iso_locale, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, str_s1.c_str(), -1, str_s2.c_str(), -1, nullptr, nullptr, 0);
|
||||
if (result != 0) return result;
|
||||
|
@@ -59,6 +59,6 @@ char *convert_from_fs(const wchar_t *name, char *utf8_buf, size_t buflen);
|
||||
wchar_t *convert_to_fs(const std::string_view name, wchar_t *utf16_buf, size_t buflen);
|
||||
|
||||
void Win32SetCurrentLocaleName(const char *iso_code);
|
||||
int OTTDStringCompare(const char *s1, const char *s2);
|
||||
int OTTDStringCompare(std::string_view s1, std::string_view s2);
|
||||
|
||||
#endif /* WIN32_H */
|
||||
|
Reference in New Issue
Block a user