All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
transforms.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_TRANSFORMS_HPP
23 #define GEARS_STRING_TRANSFORMS_HPP
24 
25 #include <string>
26 #include <sstream>
27 #include "../meta/alias.hpp"
28 
29 namespace gears {
30 namespace string {
49 template<typename String>
50 inline meta::Unqualified<String> left(String str, size_t n) {
51  if(n >= str.size())
52  return str;
53  return { str.substr(0, n) };
54 }
55 
74 template<typename String>
75 inline meta::Unqualified<String> right(String str, size_t n) {
76  if(n >= str.size())
77  return str;
78  return { str.substr(str.size() - n) };
79 }
80 
89 template<typename String>
90 inline meta::Unqualified<String> reverse(String str) {
91  auto first = str.begin();
92  auto last = str.end();
93  while((first != last) && (first != --last)) {
94  using std::swap;
95  swap(*first++, *last);
96  }
97  return str;
98 }
99 
130 template<typename String, typename Cont>
131 inline meta::Unqualified<String> join(const Cont& cont, const String& sep) {
132  auto first = cont.cbegin();
133  auto last = cont.cend();
134  std::basic_ostringstream<meta::ValueType<String>> ss;
135  if(first != last) {
136  ss << *first++;
137  }
138  while(first != last) {
139  ss << sep << *first++;
140  }
141  return { ss.str() };
142 }
143 
156 template<typename String, typename Cont, typename UnaryPredicate>
157 inline meta::Unqualified<String> join_if(const Cont& cont, const String& sep, UnaryPredicate&& pred) {
158  auto first = cont.cbegin();
159  auto last = cont.cend();
160  std::basic_ostringstream<meta::ValueType<String>> ss;
161 
162  // Increase first until predicate is met
163  while(!pred(*first)) ++first;
164 
165  if(first != last) {
166  ss << *first++;
167  }
168 
169  while(first != last) {
170  if(pred(*first)) {
171  ss << sep << *first++;
172  continue;
173  }
174  ++first;
175  }
176 
177  return { ss.str() };
178 }
179 
216 template<typename String, typename OutIt>
217 inline OutIt split(const String& str, const String& sep, OutIt it) {
218  size_t start = 0, end = 0;
219  while((end = str.find(sep, start)) != String::npos) {
220  *it++ = str.substr(start, end - start);
221  start = end + sep.length();
222  }
223  *it++ = str.substr(start);
224  return it;
225 }
226 } // string
227 } // gears
228 
229 #endif // GEARS_STRING_TRANSFORMS_HPP