Implement wxUILocale::CompareStrings() for Unix systems

This required changing CompareStrings() to be a method of wxUILocale
object, rather than just as a static function, as we must only allocate
the locale_t object once, and not during each to this function, as this
could make it unusably slow when using it as a comparison function when
sorting a large list of strings.

This is also more efficient under Mac, where we can similarly allocate
NSLocale only once and even marginally more efficient under MSW, where
we don't have to construct the locale string during each call. And,
under all platforms, it also simplifies code by separating this function
implementation from the initialization of wxUILocaleImpl.

Also document that case-insensitive comparison is not available under
Unix and adjust the tests accordingly.
This commit is contained in:
Vadim Zeitlin
2021-08-29 18:57:30 +02:00
parent 45f9908e05
commit ae81d9d207
8 changed files with 141 additions and 117 deletions

View File

@@ -260,44 +260,49 @@ TEST_CASE("wxUILocale::CompareStrings", "[uilocale]")
{
SECTION("English")
{
const wxLocaleIdent l("en");
const wxUILocale l("en");
// This is not very interesting, but check that comparison works at all.
CHECK( wxUILocale::CompareStrings("x", "x", l) == 0 );
CHECK( wxUILocale::CompareStrings("x", "y", l) == -1 );
CHECK( wxUILocale::CompareStrings("z", "y", l) == +1 );
CHECK( l.CompareStrings("x", "x") == 0 );
CHECK( l.CompareStrings("x", "y") == -1 );
CHECK( l.CompareStrings("z", "y") == +1 );
// Also check for some degenerate cases.
CHECK( wxUILocale::CompareStrings("", "", l) == 0 );
CHECK( wxUILocale::CompareStrings("", "a", l) == -1 );
CHECK( l.CompareStrings("", "") == 0 );
CHECK( l.CompareStrings("", "a") == -1 );
// And for case handling.
CHECK( wxUILocale::CompareStrings("a", "A", l) == -1 );
CHECK( wxUILocale::CompareStrings("a", "A", l,
wxCompare_CaseInsensitive) == 0 );
CHECK( wxUILocale::CompareStrings("b", "A", l) == 1 );
CHECK( wxUILocale::CompareStrings("B", "a", l) == 1 );
CHECK( l.CompareStrings("a", "A") == -1 );
CHECK( l.CompareStrings("b", "A") == 1 );
CHECK( l.CompareStrings("B", "a") == 1 );
// Case insensitive comparison is not supported with POSIX APIs.
#if defined(__WINDOWS__) || defined(__WXOSX__)
CHECK( l.CompareStrings("a", "A", wxCompare_CaseInsensitive) == 0 );
#endif
}
SECTION("German")
{
const wxLocaleIdent l = wxLocaleIdent("de").Region("DE");
const wxUILocale l(wxLocaleIdent("de").Region("DE"));
// This is more interesting and shows that CompareStrings() uses German
// dictionary rules (DIN 5007-1 variant 1).
CHECK( wxUILocale::CompareStrings("a", u8("ä"), l) == -1 );
CHECK( wxUILocale::CompareStrings(u8("ä"), "ae", l) == -1 );
CHECK( wxUILocale::CompareStrings(u8("ß"), "ss", l,
wxCompare_CaseInsensitive) == 0 );
CHECK( l.CompareStrings("a", u8("ä")) == -1 );
CHECK( l.CompareStrings(u8("ä"), "ae") == -1 );
#if defined(__WINDOWS__) || defined(__WXOSX__)
CHECK( l.CompareStrings(u8("ß"), "ss", wxCompare_CaseInsensitive) == 0 );
#endif
}
SECTION("Swedish")
{
const wxLocaleIdent l("sv");
const wxUILocale l("sv");
// And this shows that sort order really depends on the language.
CHECK( wxUILocale::CompareStrings(u8("ä"), "ae", l) == 1 );
CHECK( wxUILocale::CompareStrings(u8("ö"), "z" , l) == 1 );
CHECK( l.CompareStrings(u8("ä"), "ae") == 1 );
CHECK( l.CompareStrings(u8("ö"), "z" ) == 1 );
}
}