From afc34c7a60faaceb5fba9463aba44e3c4062f6cb Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Mon, 19 May 2014 22:37:14 +0200 Subject: [PATCH] Fix bug 9030 except for windows The format entries should be sorted according to the culture selected for the UI. This was not the case previously, resulting in unexpected sorting of small and capital letters. This is now fixed by using the standard C function strcoll(). Qt does only offer similar functionality in Qt5, and this is not mature enough yet to depend on it. Unfortunately we have a report that strcoll() does not work on MSVC, however this partial fix is better than nothing. The MSVC issue might also be a configuration problem, since MS claims that strcoll() is supported. This still needs to be checked. --- src/Format.cpp | 4 ++-- src/support/lstrings.cpp | 25 +++++++++++++++++++++++++ src/support/lstrings.h | 3 +++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Format.cpp b/src/Format.cpp index 4045d80d35..0bbd3fc144 100644 --- a/src/Format.cpp +++ b/src/Format.cpp @@ -104,12 +104,12 @@ private: bool Format::formatSorter(Format const * lhs, Format const * rhs) { - return _(lhs->prettyname()) < _(rhs->prettyname()); + return compare_locale(_(lhs->prettyname()), _(rhs->prettyname())) < 0; } bool operator<(Format const & a, Format const & b) { - return _(a.prettyname()) < _(b.prettyname()); + return compare_locale(_(a.prettyname()), _(b.prettyname())) < 0; } diff --git a/src/support/lstrings.cpp b/src/support/lstrings.cpp index 656f82ee11..a2487f8844 100644 --- a/src/support/lstrings.cpp +++ b/src/support/lstrings.cpp @@ -15,6 +15,7 @@ #include "support/lstrings.h" #include "support/convert.h" +#include "support/debug.h" #include "support/qstring_helpers.h" #include "support/lassert.h" @@ -22,6 +23,7 @@ #include #include +#include #include using namespace std; @@ -200,6 +202,29 @@ int compare_no_case(docstring const & s, docstring const & s2) } +int compare_locale(docstring const & s, docstring const & s2) +{ + // FIXME We have a report that this does not work on windows (bug 9030) + try + { + string const l = to_local8bit(s); + string const r = to_local8bit(s2); + return strcoll(l.c_str(), r.c_str()); + } + catch (bad_cast & e) + { + // fall back to builtin sorting + LYXERR0("Could not compare using the current locale: " + << e.what() << ", using fallback."); + if (s < s2) + return -1; + if (s > s2) + return 1; + return 0; + } +} + + namespace { template diff --git a/src/support/lstrings.h b/src/support/lstrings.h index 576a5b921d..02b5cf2f2b 100644 --- a/src/support/lstrings.h +++ b/src/support/lstrings.h @@ -28,6 +28,9 @@ namespace support { /// Does not depend on the locale. int compare_no_case(docstring const & s, docstring const & s2); +/// Compare \p s and \p s2 using the collating rules of the current locale. +int compare_locale(docstring const & s, docstring const & s2); + /// Compare \p s and \p s2, ignoring the case of ASCII characters only. int compare_ascii_no_case(std::string const & s, std::string const & s2);