2011-06-19 18:14:03 +02:00
|
|
|
#ifndef MISC_STRINGOPS_H
|
|
|
|
#define MISC_STRINGOPS_H
|
|
|
|
|
2012-12-30 21:16:54 -08:00
|
|
|
#include <cctype>
|
2012-12-23 23:23:24 +04:00
|
|
|
#include <string>
|
|
|
|
#include <algorithm>
|
|
|
|
|
2011-06-19 18:14:03 +02:00
|
|
|
namespace Misc
|
|
|
|
{
|
2012-12-23 23:23:24 +04:00
|
|
|
class StringUtils
|
|
|
|
{
|
|
|
|
struct ci
|
|
|
|
{
|
2014-07-04 23:19:40 +02:00
|
|
|
bool operator()(char x, char y) const {
|
2015-11-27 03:22:52 +01:00
|
|
|
return tolower(x) < tolower(y);
|
2012-12-23 23:23:24 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
static bool ciLess(const std::string &x, const std::string &y) {
|
|
|
|
return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), ci());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ciEqual(const std::string &x, const std::string &y) {
|
|
|
|
if (x.size() != y.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string::const_iterator xit = x.begin();
|
|
|
|
std::string::const_iterator yit = y.begin();
|
|
|
|
for (; xit != x.end(); ++xit, ++yit) {
|
2015-11-27 03:22:52 +01:00
|
|
|
if (tolower(*xit) != tolower(*yit)) {
|
2012-12-23 23:23:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-19 18:50:07 +11:00
|
|
|
static bool cEqual(const std::string &x, const std::string &y) {
|
|
|
|
if (x.size() != y.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::string::const_iterator xit = x.begin();
|
|
|
|
std::string::const_iterator yit = y.begin();
|
|
|
|
for (; xit != x.end(); ++xit, ++yit) {
|
|
|
|
if (*xit != *yit) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-11 00:35:19 -07:00
|
|
|
static int ciCompareLen(const std::string &x, const std::string &y, size_t len)
|
|
|
|
{
|
|
|
|
std::string::const_iterator xit = x.begin();
|
|
|
|
std::string::const_iterator yit = y.begin();
|
|
|
|
for(;xit != x.end() && yit != y.end() && len > 0;++xit,++yit,--len)
|
|
|
|
{
|
|
|
|
int res = *xit - *yit;
|
2015-11-27 03:22:52 +01:00
|
|
|
if(res != 0 && tolower(*xit) != tolower(*yit))
|
2013-08-11 00:35:19 -07:00
|
|
|
return (res > 0) ? 1 : -1;
|
|
|
|
}
|
|
|
|
if(len > 0)
|
|
|
|
{
|
|
|
|
if(xit != x.end())
|
|
|
|
return 1;
|
|
|
|
if(yit != y.end())
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-23 23:23:24 +04:00
|
|
|
/// Transforms input string to lower case w/o copy
|
2015-12-07 22:49:15 +01:00
|
|
|
static void lowerCaseInPlace(std::string &inout) {
|
2014-07-04 23:19:40 +02:00
|
|
|
for (unsigned int i=0; i<inout.size(); ++i)
|
2015-11-27 03:22:52 +01:00
|
|
|
inout[i] = tolower(inout[i]);
|
2012-12-23 23:23:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns lower case copy of input string
|
|
|
|
static std::string lowerCase(const std::string &in)
|
|
|
|
{
|
|
|
|
std::string out = in;
|
2015-12-07 22:49:15 +01:00
|
|
|
lowerCaseInPlace(out);
|
|
|
|
return out;
|
2012-12-23 23:23:24 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-19 18:14:03 +02:00
|
|
|
}
|
|
|
|
|
2010-01-04 14:48:18 +01:00
|
|
|
#endif
|