Skip to content

Commit

Permalink
StringUtils: use the locale-unaware tolower function
Browse files Browse the repository at this point in the history
There is no change in behaviour since we were using the C locale.

The locale-aware tolower is much slower than the locale-unaware one. At least on Linux/GCC it calls dynamic_cast's, and is overall slower by an order of magnitude.
  • Loading branch information
scrawl committed Nov 27, 2015
1 parent 44dd620 commit 27e6692
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
8 changes: 0 additions & 8 deletions components/misc/stringops.cpp

This file was deleted.

11 changes: 4 additions & 7 deletions components/misc/stringops.hpp
Expand Up @@ -4,18 +4,15 @@
#include <cctype>
#include <string>
#include <algorithm>
#include <locale>

namespace Misc
{
class StringUtils
{

static std::locale mLocale;
struct ci
{
bool operator()(char x, char y) const {
return std::tolower(x, StringUtils::mLocale) < std::tolower(y, StringUtils::mLocale);
return tolower(x) < tolower(y);
}
};

Expand All @@ -31,7 +28,7 @@ class StringUtils
std::string::const_iterator xit = x.begin();
std::string::const_iterator yit = y.begin();
for (; xit != x.end(); ++xit, ++yit) {
if (std::tolower(*xit, mLocale) != std::tolower(*yit, mLocale)) {
if (tolower(*xit) != tolower(*yit)) {
return false;
}
}
Expand All @@ -45,7 +42,7 @@ class StringUtils
for(;xit != x.end() && yit != y.end() && len > 0;++xit,++yit,--len)
{
int res = *xit - *yit;
if(res != 0 && std::tolower(*xit, mLocale) != std::tolower(*yit, mLocale))
if(res != 0 && tolower(*xit) != tolower(*yit))
return (res > 0) ? 1 : -1;
}
if(len > 0)
Expand All @@ -61,7 +58,7 @@ class StringUtils
/// Transforms input string to lower case w/o copy
static std::string &toLower(std::string &inout) {
for (unsigned int i=0; i<inout.size(); ++i)
inout[i] = std::tolower(inout[i], mLocale);
inout[i] = tolower(inout[i]);
return inout;
}

Expand Down

0 comments on commit 27e6692

Please sign in to comment.