* Compares two strings using case insensitive natural sort. * * @param s1 First string to compare. * @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. */
| 329 | * @return 1 if s1 < s2, 2 if s1 == s2, 3 if s1 > s2, or 0 if not supported by the OS. |
| 330 | */ |
| 331 | int MacOSStringCompare(std::string_view s1, std::string_view s2) |
| 332 | { |
| 333 | static const bool supported = MacOSVersionIsAtLeast(10, 5, 0); |
| 334 | if (!supported) return 0; |
| 335 | |
| 336 | CFStringCompareFlags flags = kCFCompareCaseInsensitive | kCFCompareNumerically | kCFCompareLocalized | kCFCompareWidthInsensitive | kCFCompareForcedOrdering; |
| 337 | |
| 338 | CFAutoRelease<CFStringRef> cf1(CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8 *)s1.data(), s1.size(), kCFStringEncodingUTF8, false)); |
| 339 | CFAutoRelease<CFStringRef> cf2(CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8 *)s2.data(), s2.size(), kCFStringEncodingUTF8, false)); |
| 340 | |
| 341 | /* If any CFString could not be created (e.g., due to UTF8 invalid chars), return OS unsupported functionality */ |
| 342 | if (cf1 == nullptr || cf2 == nullptr) return 0; |
| 343 | |
| 344 | return (int)CFStringCompareWithOptionsAndLocale(cf1.get(), cf2.get(), CFRangeMake(0, CFStringGetLength(cf1.get())), flags, _osx_locale.get()) + 2; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Search if a string is contained in another string using the current locale. |
no test coverage detected