From 97d2e694a1ffdbffd558501967588dc0cfdd4054 Mon Sep 17 00:00:00 2001 From: Jyrki Vesterinen Date: Mon, 7 May 2018 19:43:45 +0300 Subject: [PATCH] Partial case insensitivity for translation::icompare() fallback --- src/gettext_boost.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/gettext_boost.cpp b/src/gettext_boost.cpp index fb04ffeea903..3bf5506a3295 100644 --- a/src/gettext_boost.cpp +++ b/src/gettext_boost.cpp @@ -17,8 +17,10 @@ #include "log.hpp" #include "filesystem.hpp" +#include #include #include +#include #include #include #include @@ -350,6 +352,17 @@ namespace return *mng; } + // Converts ASCII letters to lowercase. Ignores Unicode letters. + std::string ascii_to_lowercase(const std::string& str) + { + std::string result; + result.reserve(str.length()); + std::transform(str.begin(), str.end(), std::back_inserter(result), [](char c) + { + return c >= 'A' && c <= 'Z' ? c | 0x20 : c; + }); + return result; + } } namespace translation @@ -442,7 +455,7 @@ int icompare(const std::string& s1, const std::string& s2) { #ifdef __APPLE__ // https://github.com/wesnoth/wesnoth/issues/2094 - return compare(s1, s2); + return compare(ascii_to_lowercase(s1), ascii_to_lowercase(s2)); #else std::lock_guard lock(get_mutex()); @@ -457,8 +470,8 @@ int icompare(const std::string& s1, const std::string& s2) bad_cast_once = true; } - // FIXME: not even lazily case-insensitive - return s1.compare(s2); + // Let's convert at least ASCII letters to lowercase to get a somewhat case-insensitive comparison. + return ascii_to_lowercase(s1).compare(ascii_to_lowercase(s2)); } #endif }