forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalized_comparator.cpp
49 lines (44 loc) · 1.76 KB
/
localized_comparator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <locale>
#if defined(__APPLE__)
// needed by localized_comparator
#include <CoreFoundation/CoreFoundation.h>
#endif
#include "catacharset.h"
#include "localized_comparator.h"
bool localized_comparator::operator()( const std::string &l, const std::string &r ) const
{
// We need different implementations on each platform. MacOS seems to not
// support localized comparison of strings via the standard library at all,
// so resort to MacOS-specific solution. Windows cannot be expected to be
// using a UTF-8 locale (whereas our strings are always UTF-8) and so we
// must convert to wstring for comparison there. Linux seems to work as
// expected on regular strings; no workarounds needed.
// See https://github.com/CleverRaven/Cataclysm-DDA/pull/40041 for further
// discussion.
#if defined(__APPLE__) // macOS and iOS
CFStringRef lr = CFStringCreateWithCStringNoCopy( kCFAllocatorDefault, l.c_str(),
kCFStringEncodingUTF8, kCFAllocatorNull );
CFStringRef rr = CFStringCreateWithCStringNoCopy( kCFAllocatorDefault, r.c_str(),
kCFStringEncodingUTF8, kCFAllocatorNull );
bool result = CFStringCompare( lr, rr, kCFCompareLocalized ) < 0;
CFRelease( lr );
CFRelease( rr );
return result;
#elif defined(_WIN32)
return ( *this )( utf8_to_wstr( l ), utf8_to_wstr( r ) );
#else
return std::locale()( l, r );
#endif
}
bool localized_comparator::operator()( const std::wstring &l, const std::wstring &r ) const
{
#if defined(__APPLE__) // macOS and iOS
return ( *this )( wstr_to_utf8( l ), wstr_to_utf8( r ) );
#else
return std::locale()( l, r );
#endif
}
bool localized_comparator::operator()( const translation &l, const translation &r ) const
{
return l.translated_lt( r );
}