Skip to content

Commit

Permalink
Partial case insensitivity for translation::icompare() fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkive committed May 7, 2018
1 parent 2f960ec commit 97d2e69
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/gettext_boost.cpp
Expand Up @@ -17,8 +17,10 @@
#include "log.hpp"
#include "filesystem.hpp"

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <fstream>
#include <locale>
#include <mutex>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<std::mutex> lock(get_mutex());

Expand All @@ -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
}
Expand Down

0 comments on commit 97d2e69

Please sign in to comment.