MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / OTTDStringCompare

Function OTTDStringCompare

src/os/windows/win32.cpp:442–470  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

440}
441
442int OTTDStringCompare(std::string_view s1, std::string_view s2)
443{
444 typedef int (WINAPI *PFNCOMPARESTRINGEX)(LPCWSTR, DWORD, LPCWCH, int, LPCWCH, int, LPVOID, LPVOID, LPARAM);
445 static const PFNCOMPARESTRINGEX _CompareStringEx = GetKernel32Function("CompareStringEx");
446
447#ifndef SORT_DIGITSASNUMBERS
448# define SORT_DIGITSASNUMBERS 0x00000008 // use digits as numbers sort method
449#endif
450#ifndef LINGUISTIC_IGNORECASE
451# define LINGUISTIC_IGNORECASE 0x00000010 // linguistically appropriate 'ignore case'
452#endif
453
454 int len_s1 = MultiByteToWideChar(CP_UTF8, 0, s1.data(), (int)s1.size(), nullptr, 0);
455 int len_s2 = MultiByteToWideChar(CP_UTF8, 0, s2.data(), (int)s2.size(), nullptr, 0);
456
457 std::wstring str_s1(len_s1, L'\0');
458 std::wstring str_s2(len_s2, L'\0');
459
460 if (len_s1 != 0) MultiByteToWideChar(CP_UTF8, 0, s1.data(), (int)s1.size(), str_s1.data(), len_s1);
461 if (len_s2 != 0) MultiByteToWideChar(CP_UTF8, 0, s2.data(), (int)s2.size(), str_s2.data(), len_s2);
462
463 /* CompareStringEx takes UTF-16 strings, even in ANSI-builds. */
464 if (_CompareStringEx != nullptr) {
465 int result = _CompareStringEx(_cur_iso_locale, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, str_s1.data(), len_s1, str_s2.data(), len_s2, nullptr, nullptr, 0);
466 if (result != 0) return result;
467 }
468
469 return CompareString(MAKELCID(_current_language->winlangid, SORT_DEFAULT), NORM_IGNORECASE, str_s1.data(), len_s1, str_s2.data(), len_s2);
470}
471
472/**
473 * Search if a string is contained in another string using the current locale.

Callers 1

StrNaturalCompareFunction · 0.85

Calls 3

GetKernel32FunctionFunction · 0.85
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected