All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
predicate.hpp
1 // The MIT License (MIT)
2 
3 // Copyright (c) 2012-2014 Danny Y., Rapptz
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal in
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 // the Software, and to permit persons to whom the Software is furnished to do so,
10 // subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef GEARS_STRING_PREDICATE_HPP
23 #define GEARS_STRING_PREDICATE_HPP
24 
25 #include <locale>
26 
27 namespace gears {
28 namespace string {
37 template<typename CharT = char>
38 struct is_any_of {
39 private:
40  const CharT* str;
41 public:
42  is_any_of(const CharT* str): str(str) {}
43  bool operator()(CharT s) const {
44  auto copy = str;
45  while(*copy) {
46  if(*copy++ == s)
47  return true;
48  }
49  return false;
50  }
51 };
52 
70 template<typename String>
71 inline bool iequal(const String& lhs, const String& rhs, const std::locale& loc = std::locale()) {
72  if(lhs.length() != rhs.length())
73  return false;
74  auto i = lhs.cbegin();
75  auto j = rhs.cbegin();
76  for(; i != lhs.cend() && j != rhs.cend(); ++i, ++j) {
77  if(std::toupper(*i, loc) != std::toupper(*j, loc))
78  return false;
79  }
80  return true;
81 }
82 
98 template<typename String>
99 inline bool starts_with(const String& str, const String& other) {
100  return str.find(other) == 0;
101 }
102 
120 template<typename String>
121 inline bool istarts_with(const String& str, const String& other, const std::locale& loc = std::locale()) {
122  if(other.length() > str.length())
123  return false;
124  for(unsigned i = 0; i < other.length(); ++i) {
125  if(std::toupper(str[i], loc) != std::toupper(other[i], loc))
126  return false;
127  }
128  return true;
129 }
130 
146 template<typename String>
147 inline bool ends_with(const String& str, const String& other) {
148  if(str.length() >= other.length())
149  return str.compare(str.length() - other.length(), other.length(), other) == 0;
150  else
151  return false;
152 }
153 
171 template<typename String>
172 inline bool iends_with(const String& str, const String& other, const std::locale& loc = std::locale()) {
173  if(str.length() >= other.length()) {
174  for(unsigned start = str.length() - other.length(), i = 0; i < other.length(); ++i, ++start) {
175  if(std::toupper(str[start], loc) != std::toupper(other[i], loc))
176  return false;
177  }
178  return true;
179  }
180  else
181  return false;
182 }
183 
195 template<typename String>
196 inline bool contains(const String& str, const String& other) {
197  return str.find(other) != String::npos;
198 }
199 
212 template<typename String>
213 inline bool icontains(const String& str, const String& other, const std::locale& loc = std::locale()) {
214  auto first = str.cbegin();
215  auto last = str.cend();
216  auto other_last = other.cend();
217  for(; ; ++first) {
218  auto it = first;
219  for(auto i = other.cbegin(); ; ++i, ++it) {
220  if(i == other_last)
221  return true;
222  if(it == last)
223  return false;
224  if(std::toupper(*it, loc) != std::toupper(*i, loc))
225  break;
226  }
227  }
228 }
229 
239 template<typename String, typename UnaryPredicate>
240 inline bool all(const String& str, UnaryPredicate&& pred) {
241  for(auto&& c : str) {
242  if(!pred(c))
243  return false;
244  }
245  return true;
246 }
247 } // string
248 } // gears
249 
250 #endif // GEARS_STRING_PREDICATE_HPP