OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
stringops.hpp
Go to the documentation of this file.
1 #ifndef MISC_STRINGOPS_H
2 #define MISC_STRINGOPS_H
3 
4 #include <cctype>
5 #include <string>
6 #include <algorithm>
7 
8 namespace Misc
9 {
11 {
12  struct ci
13  {
14  bool operator()(char x, char y) const {
15  return toLower(x) < toLower(y);
16  }
17  };
18 
19 public:
20 
24  static char toLower(char c)
25  {
26  switch(c)
27  {
28  case 'A':return 'a';
29  case 'B':return 'b';
30  case 'C':return 'c';
31  case 'D':return 'd';
32  case 'E':return 'e';
33  case 'F':return 'f';
34  case 'G':return 'g';
35  case 'H':return 'h';
36  case 'I':return 'i';
37  case 'J':return 'j';
38  case 'K':return 'k';
39  case 'L':return 'l';
40  case 'M':return 'm';
41  case 'N':return 'n';
42  case 'O':return 'o';
43  case 'P':return 'p';
44  case 'Q':return 'q';
45  case 'R':return 'r';
46  case 'S':return 's';
47  case 'T':return 't';
48  case 'U':return 'u';
49  case 'V':return 'v';
50  case 'W':return 'w';
51  case 'X':return 'x';
52  case 'Y':return 'y';
53  case 'Z':return 'z';
54  default:return c;
55  };
56  }
57 
58  static bool ciLess(const std::string &x, const std::string &y) {
59  return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), ci());
60  }
61 
62  static bool ciEqual(const std::string &x, const std::string &y) {
63  if (x.size() != y.size()) {
64  return false;
65  }
66  std::string::const_iterator xit = x.begin();
67  std::string::const_iterator yit = y.begin();
68  for (; xit != x.end(); ++xit, ++yit) {
69  if (toLower(*xit) != toLower(*yit)) {
70  return false;
71  }
72  }
73  return true;
74  }
75 
76  static int ciCompareLen(const std::string &x, const std::string &y, size_t len)
77  {
78  std::string::const_iterator xit = x.begin();
79  std::string::const_iterator yit = y.begin();
80  for(;xit != x.end() && yit != y.end() && len > 0;++xit,++yit,--len)
81  {
82  int res = *xit - *yit;
83  if(res != 0 && toLower(*xit) != toLower(*yit))
84  return (res > 0) ? 1 : -1;
85  }
86  if(len > 0)
87  {
88  if(xit != x.end())
89  return 1;
90  if(yit != y.end())
91  return -1;
92  }
93  return 0;
94  }
95 
97  static void lowerCaseInPlace(std::string &inout) {
98  for (unsigned int i=0; i<inout.size(); ++i)
99  inout[i] = toLower(inout[i]);
100  }
101 
103  static std::string lowerCase(const std::string &in)
104  {
105  std::string out = in;
106  lowerCaseInPlace(out);
107  return out;
108  }
109 };
110 
111 }
112 
113 #endif
Definition: stringops.hpp:10
static bool ciEqual(const std::string &x, const std::string &y)
Definition: stringops.hpp:62
static bool ciLess(const std::string &x, const std::string &y)
Definition: stringops.hpp:58
static char toLower(char c)
Definition: stringops.hpp:24
static std::string lowerCase(const std::string &in)
Returns lower case copy of input string.
Definition: stringops.hpp:103
bool operator()(char x, char y) const
Definition: stringops.hpp:14
static int ciCompareLen(const std::string &x, const std::string &y, size_t len)
Definition: stringops.hpp:76
Definition: stringops.hpp:12
static void lowerCaseInPlace(std::string &inout)
Transforms input string to lower case w/o copy.
Definition: stringops.hpp:97