From dd0427514a0090d324de40dd8ad1aab1e8d188c0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 25 Aug 2021 00:30:22 +0200 Subject: [PATCH 1/3] Move wxCmpNatural() MSW definition to MSW-specific code It is tidier to have MSW-specific stuff, such as shlwapi.h inclusion, in wxMSW rather than common code. No real changes. --- src/common/arrstr.cpp | 29 +++++------------------------ src/msw/utils.cpp | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/common/arrstr.cpp b/src/common/arrstr.cpp index 6c830ef631..c2fdb7fda9 100644 --- a/src/common/arrstr.cpp +++ b/src/common/arrstr.cpp @@ -25,23 +25,6 @@ #include #include "wx/afterstd.h" -#if defined( __WINDOWS__ ) - #include - - // In some distributions of MinGW32, this function is exported in the library, - // but not declared in shlwapi.h. Therefore we declare it here. - #if defined( __MINGW32_TOOLCHAIN__ ) - extern "C" __declspec(dllimport) int WINAPI StrCmpLogicalW(LPCWSTR psz1, LPCWSTR psz2); - #endif - - // For MSVC we can also link the library containing StrCmpLogicalW() - // directly from here, for the other compilers this needs to be done at - // makefiles level. - #ifdef __VISUALC__ - #pragma comment(lib, "shlwapi") - #endif -#endif - // ============================================================================ // ArrayString // ============================================================================ @@ -912,15 +895,13 @@ int wxCMPFUNC_CONV wxCmpNaturalGeneric(const wxString& s1, const wxString& s2) // ---------------------------------------------------------------------------- // wxCmpNatural // ---------------------------------------------------------------------------- -// -// If a native version of Natural sort is available, then use that, otherwise -// use the generic version. + +// If native natural sort function isn't available, use the generic version. +#if !defined(__WINDOWS__) + int wxCMPFUNC_CONV wxCmpNatural(const wxString& s1, const wxString& s2) { -#if defined( __WINDOWS__ ) - return StrCmpLogicalW(s1.wc_str(), s2.wc_str()); -#else return wxCmpNaturalGeneric(s1, s2); -#endif // #if defined( __WINDOWS__ ) } +#endif // #if !defined( __WINDOWS__ ) diff --git a/src/msw/utils.cpp b/src/msw/utils.cpp index 6090e1351d..f4e55c31ab 100644 --- a/src/msw/utils.cpp +++ b/src/msw/utils.cpp @@ -95,6 +95,22 @@ // For wxKillAllChildren #include +// For wxCmpNatural() +#include + +// In some distributions of MinGW32, this function is exported in the library, +// but not declared in shlwapi.h. Therefore we declare it here. +#if defined( __MINGW32_TOOLCHAIN__ ) + extern "C" __declspec(dllimport) int WINAPI StrCmpLogicalW(LPCWSTR psz1, LPCWSTR psz2); +#endif + +// For MSVC we can also link the library containing StrCmpLogicalW() +// directly from here, for the other compilers this needs to be done at +// makefiles level. +#ifdef __VISUALC__ + #pragma comment(lib, "shlwapi") +#endif + // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- @@ -1583,3 +1599,8 @@ wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc) return hwnd; } + +int wxCMPFUNC_CONV wxCmpNatural(const wxString& s1, const wxString& s2) +{ + return StrCmpLogicalW(s1.wc_str(), s2.wc_str()); +} From 9c0b543c04139b29e5bf82d96607752e2d7e3be0 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 25 Aug 2021 00:47:26 +0200 Subject: [PATCH 2/3] Rename wxCmpNaturalGeneric() test to its actual name The name of the test was still using the original function name, update it to the actually used name. No real changes. --- tests/arrays/arrays.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/arrays/arrays.cpp b/tests/arrays/arrays.cpp index 4d03aa1751..0ef734fd7a 100644 --- a/tests/arrays/arrays.cpp +++ b/tests/arrays/arrays.cpp @@ -769,7 +769,7 @@ TEST_CASE("wxDynArray::IndexFromEnd", "[dynarray]") } -TEST_CASE("wxNaturalStringComparisonGeneric()", "[wxString][compare]") +TEST_CASE("wxCmpNaturalGeneric", "[wxString][compare]") { // simple string comparison CHECK(wxCmpNaturalGeneric("a", "a") == 0); From 96ab1f0824a54532e76e782c78d9a01aafa6a83b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 25 Aug 2021 00:48:01 +0200 Subject: [PATCH 3/3] Implement wxCmpNatural() using the native function under macOS Use [NSStting localizedStandardCompare:] which is supposed to use the same order as Finder does. --- src/common/arrstr.cpp | 4 ++-- src/osx/cocoa/utils_base.mm | 7 +++++++ tests/arrays/arrays.cpp | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/common/arrstr.cpp b/src/common/arrstr.cpp index c2fdb7fda9..3e9b9aef31 100644 --- a/src/common/arrstr.cpp +++ b/src/common/arrstr.cpp @@ -897,11 +897,11 @@ int wxCMPFUNC_CONV wxCmpNaturalGeneric(const wxString& s1, const wxString& s2) // ---------------------------------------------------------------------------- // If native natural sort function isn't available, use the generic version. -#if !defined(__WINDOWS__) +#if !(defined(__WINDOWS__) || defined(__DARWIN__) || defined(__WXOSX_IPHONE__)) int wxCMPFUNC_CONV wxCmpNatural(const wxString& s1, const wxString& s2) { return wxCmpNaturalGeneric(s1, s2); } -#endif // #if !defined( __WINDOWS__ ) +#endif // not a platform with native implementation diff --git a/src/osx/cocoa/utils_base.mm b/src/osx/cocoa/utils_base.mm index a99f3c30d4..bc3c1dfbbe 100644 --- a/src/osx/cocoa/utils_base.mm +++ b/src/osx/cocoa/utils_base.mm @@ -266,3 +266,10 @@ bool wxCocoaLaunch(const char* const* argv, pid_t &pid) } #endif + +int wxCMPFUNC_CONV wxCmpNatural(const wxString& s1, const wxString& s2) +{ + // The values of NSOrdered{Ascending,Same,Descending} are the same as + // expected return values of wxCmpNatural(), so we don't need to convert. + return [wxCFStringRef(s1).AsNSString() localizedStandardCompare: wxCFStringRef(s2).AsNSString()]; +} diff --git a/tests/arrays/arrays.cpp b/tests/arrays/arrays.cpp index 0ef734fd7a..5db623f89b 100644 --- a/tests/arrays/arrays.cpp +++ b/tests/arrays/arrays.cpp @@ -844,3 +844,9 @@ TEST_CASE("wxCmpNaturalGeneric", "[wxString][compare]") CHECK(wxCmpNaturalGeneric("a5th 5", "a 10th 10") > 0); } +TEST_CASE("wxCmpNatural", "[wxString][compare]") +{ + // We can't expect much from the native natural comparison function as it's + // locale-dependent, so just run a simple sanity test + CHECK(wxCmpNatural("same", "same") == 0); +}