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

Function StrNaturalCompare

src/string.cpp:427–454  ·  view source on GitHub ↗

* Compares two strings using case insensitive natural sort. * * @param s1 First string to compare. * @param s2 Second string to compare. * @param ignore_garbage_at_front Skip punctuation characters in the front * @return Less than zero if s1 < s2, zero if s1 == s2, greater than zero if s1 > s2. */

Source from the content-addressed store, hash-verified

425 * @return Less than zero if s1 < s2, zero if s1 == s2, greater than zero if s1 > s2.
426 */
427int StrNaturalCompare(std::string_view s1, std::string_view s2, bool ignore_garbage_at_front)
428{
429 if (ignore_garbage_at_front) {
430 s1 = SkipGarbage(s1);
431 s2 = SkipGarbage(s2);
432 }
433
434#ifdef WITH_ICU_I18N
435 if (_current_collator) {
436 UErrorCode status = U_ZERO_ERROR;
437 int result = _current_collator->compareUTF8(icu::StringPiece(s1.data(), s1.size()), icu::StringPiece(s2.data(), s2.size()), status);
438 if (U_SUCCESS(status)) return result;
439 }
440#endif /* WITH_ICU_I18N */
441
442#if defined(_WIN32) && !defined(STRGEN) && !defined(SETTINGSGEN)
443 int res = OTTDStringCompare(s1, s2);
444 if (res != 0) return res - 2; // Convert to normal C return values.
445#endif
446
447#if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
448 int res = MacOSStringCompare(s1, s2);
449 if (res != 0) return res - 2; // Convert to normal C return values.
450#endif
451
452 /* Do a normal comparison if ICU is missing or if we cannot create a collator. */
453 return StrCompareIgnoreCase(s1, s2);
454}
455
456#ifdef WITH_ICU_I18N
457

Callers 15

SignNameSorterMethod · 0.85
NameSorterMethod · 0.85
NatSortFuncMethod · 0.85
IndustryTypeNameSorterFunction · 0.85
IndustryNameSorterMethod · 0.85
TownNameSorterMethod · 0.85
SortGUIGroupListFunction · 0.85
CargoSpecNameSorterFunction · 0.85
StationNameSorterMethod · 0.85
SortStationMethod · 0.85
GRFSorterFunction · 0.85
EngineNameSorterFunction · 0.85

Calls 6

SkipGarbageFunction · 0.85
OTTDStringCompareFunction · 0.85
MacOSStringCompareFunction · 0.85
StrCompareIgnoreCaseFunction · 0.85
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected