Permalink
Browse files

Add smarter Unicode comparisons

  • Loading branch information...
SupSuper committed Nov 5, 2018
1 parent f4ac110 commit 680eafb9a848392c2c3e54931f53a7d194af650e
Showing with 25 additions and 1 deletion.
  1. +23 −1 src/Engine/Unicode.cpp
  2. +2 −0 src/Engine/Unicode.h
@@ -27,6 +27,7 @@
#else
#include <locale.h>
#include <stdlib.h>
#include <ctype.h>
#endif
namespace OpenXcom
@@ -227,12 +228,33 @@ bool naturalCompare(const std::string &a, const std::string &b)
#endif
{
// sorry unix users you get ASCII sort
setlocale(LC_ALL, "");
std::string::const_iterator i, j;
for (i = a.begin(), j = b.begin(); i != a.end() && j != b.end() && tolower(*i) == tolower(*j); ++i, ++j);
return (i != a.end() && j != b.end() && tolower(*i) < tolower(*j));
bool result = (i != a.end() && j != b.end() && tolower(*i) < tolower(*j));
setlocale(LC_ALL, "C");
return result;
}
}
/**
* Uppercases a UTF-8 string, modified in place.
* Used for case-insensitive comparisons.
* @param s Source string.
*/
void upperCase(std::string &s)
{
#ifdef _WIN32
std::wstring ws = convMbToWc(s, CP_UTF8);
CharUpperW(&ws[0]);
s = convWcToMb(ws, CP_UTF8);
#else
setlocale(LC_ALL, "");
for (std::string::iterator i = s.begin(); i != s.end(); ++i) *i = toupper(*i);
setlocale(LC_ALL, "C");
#endif
}
/**
* Replaces every instance of a substring.
* @param str The string to modify.
@@ -66,6 +66,8 @@ namespace Unicode
/// Compares two strings by natural order.
bool naturalCompare(const std::string &a, const std::string &b);
/// Uppercases a string in place.
void upperCase(std::string &s);
/// Replaces a substring.
void replace(std::string &str, const std::string &find, const std::string &replace);
/// Formats an integer value as number with separators.

0 comments on commit 680eafb

Please sign in to comment.