Use explicit C locale in Misc::StringUtils (Fixes #1216)

deque
scrawl 11 years ago
parent 7cd819f24f
commit 41d73377b0

@ -12,59 +12,6 @@
namespace Misc namespace Misc
{ {
bool begins(const char* str1, const char* str2) std::locale StringUtils::mLocale = std::locale::classic();
{
while(*str2)
{
if(*str1 == 0 || *str1 != *str2) return false;
str1++;
str2++;
}
return true;
}
bool ends(const char* str1, const char* str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
if(len1 < len2) return false;
return strcmp(str2, str1+len1-len2) == 0;
}
// True if the given chars match, case insensitive
static bool icmp(char a, char b)
{
if(a >= 'A' && a <= 'Z')
a += 'a' - 'A';
if(b >= 'A' && b <= 'Z')
b += 'a' - 'A';
return a == b;
}
bool ibegins(const char* str1, const char* str2)
{
while(*str2)
{
if(*str1 == 0 || !icmp(*str1,*str2)) return false;
str1++;
str2++;
}
return true;
}
bool iends(const char* str1, const char* str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
if(len1 < len2) return false;
return strcasecmp(str2, str1+len1-len2) == 0;
}
} }

@ -4,15 +4,18 @@
#include <cctype> #include <cctype>
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <locale>
namespace Misc namespace Misc
{ {
class StringUtils class StringUtils
{ {
static std::locale mLocale;
struct ci struct ci
{ {
bool operator()(int x, int y) const { bool operator()(char x, char y) const {
return std::tolower(x) < std::tolower(y); return std::tolower(x, StringUtils::mLocale) < std::tolower(y, StringUtils::mLocale);
} }
}; };
@ -28,7 +31,7 @@ public:
std::string::const_iterator xit = x.begin(); std::string::const_iterator xit = x.begin();
std::string::const_iterator yit = y.begin(); std::string::const_iterator yit = y.begin();
for (; xit != x.end(); ++xit, ++yit) { for (; xit != x.end(); ++xit, ++yit) {
if (std::tolower(*xit) != std::tolower(*yit)) { if (std::tolower(*xit, mLocale) != std::tolower(*yit, mLocale)) {
return false; return false;
} }
} }
@ -42,7 +45,7 @@ public:
for(;xit != x.end() && yit != y.end() && len > 0;++xit,++yit,--len) for(;xit != x.end() && yit != y.end() && len > 0;++xit,++yit,--len)
{ {
int res = *xit - *yit; int res = *xit - *yit;
if(res != 0 && std::tolower(*xit) != std::tolower(*yit)) if(res != 0 && std::tolower(*xit, mLocale) != std::tolower(*yit, mLocale))
return (res > 0) ? 1 : -1; return (res > 0) ? 1 : -1;
} }
if(len > 0) if(len > 0)
@ -57,12 +60,8 @@ public:
/// Transforms input string to lower case w/o copy /// Transforms input string to lower case w/o copy
static std::string &toLower(std::string &inout) { static std::string &toLower(std::string &inout) {
std::transform( for (unsigned int i=0; i<inout.size(); ++i)
inout.begin(), inout[i] = std::tolower(inout[i], mLocale);
inout.end(),
inout.begin(),
(int (*)(int)) std::tolower
);
return inout; return inout;
} }
@ -74,19 +73,6 @@ public:
} }
}; };
/// Returns true if str1 begins with substring str2
bool begins(const char* str1, const char* str2);
/// Returns true if str1 ends with substring str2
bool ends(const char* str1, const char* str2);
/// Case insensitive, returns true if str1 begins with substring str2
bool ibegins(const char* str1, const char* str2);
/// Case insensitive, returns true if str1 ends with substring str2
bool iends(const char* str1, const char* str2);
} }
#endif #endif

Loading…
Cancel
Save