Skip to content

Commit

Permalink
Perform localized comparison via wstring
Browse files Browse the repository at this point in the history
On Windows the existing solution for localized comparison seems to do
the wrong thing, but evidence suggests that comparison of wstring should
work.

Convert the strings before comparison on Windows.

At the same time, do the reverse conversion on MacOS (this won't
actually be used anywhere in the current code, but it seemse a good idea
to implement it while we had the experimental data to suggest it was
necessary.

See CleverRaven#40041 for more discussion.
  • Loading branch information
jbytheway committed May 5, 2020
1 parent 50810da commit 8591e64
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,14 @@ std::string operator+( const translation &lhs, const translation &rhs )

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(MACOSX)
CFStringRef lr = CFStringCreateWithCStringNoCopy( kCFAllocatorDefault, l.c_str(),
kCFStringEncodingUTF8, kCFAllocatorNull );
Expand All @@ -707,6 +715,18 @@ bool localized_comparator::operator()( const std::string &l, const std::string &
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(MACOSX)
return ( *this )( wstr_to_utf8( l ), wstr_to_utf8( r ) );
#else
return std::locale()( l, r );
#endif
}
1 change: 1 addition & 0 deletions src/translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ struct localized_comparator {
}

bool operator()( const std::string &, const std::string & ) const;
bool operator()( const std::wstring &, const std::wstring & ) const;
};

constexpr localized_comparator localized_compare{};
Expand Down

0 comments on commit 8591e64

Please sign in to comment.