All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
replace.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_REPLACE_HPP
23 #define GEARS_STRING_REPLACE_HPP
24 
25 #include "../meta/alias.hpp"
26 #include <cstddef>
27 
28 namespace gears {
29 namespace string {
30 namespace string_replace_detail {
31 template<typename String>
32 inline size_t nth_finder(const String& str, const String& find, size_t nth) {
33  size_t start = 0;
34  auto pos = str.find(find);
35  while(start < nth && pos != String::npos) {
36  pos = str.find(find, pos + find.length());
37  ++start;
38  }
39  return pos;
40 }
41 } // string_replace_detail
42 
53 template<typename String>
54 inline meta::Unqualified<String> replace_first(String str, const String& from, const String& to) {
55  auto start_pos = str.find(from);
56  if(start_pos == String::npos)
57  return str;
58  str.replace(start_pos, from.length(), to);
59  return str;
60 }
61 
72 template<typename String>
73 inline meta::Unqualified<String> replace_last(String str, const String& from, const String& to) {
74  auto start_pos = str.rfind(from);
75  if(start_pos == String::npos)
76  return str;
77  str.replace(start_pos, from.length(), to);
78  return str;
79 }
80 
93 template<typename String>
94 inline meta::Unqualified<String> replace_nth(String str, size_t nth, const String& from, const String& to) {
95  auto pos = string_replace_detail::nth_finder(str, from, nth);
96  if(pos == String::npos)
97  return str;
98  str.replace(pos, from.length(), to);
99  return str;
100 }
101 
112 template<typename String>
113 inline meta::Unqualified<String> replace_all(String str, const String& from, const String& to) {
114  size_t start_pos = 0;
115  while((start_pos = str.find(from, start_pos)) != String::npos) {
116  str.replace(start_pos, from.length(), to);
117  start_pos += to.length();
118  }
119  return str;
120 }
121 
133 template<typename String>
134 inline meta::Unqualified<String> erase_first(String str, const String& erase) {
135  auto start_pos = str.find(erase);
136  if(start_pos == String::npos)
137  return str;
138  str.replace(start_pos, erase.length(), "");
139  return str;
140 }
141 
153 template<typename String>
154 inline meta::Unqualified<String> erase_last(String str, const String& erase) {
155  auto start_pos = str.rfind(erase);
156  if(start_pos == String::npos)
157  return str;
158  str.replace(start_pos, erase.length(), "");
159  return str;
160 }
161 
174 template<typename String>
175 inline meta::Unqualified<String> erase_nth(String str, size_t nth, const String& erase) {
176  auto pos = string_replace_detail::nth_finder(str, erase, nth);
177  if(pos == String::npos)
178  return str;
179  str.replace(pos, erase.length(), "");
180  return str;
181 }
182 
194 template<typename String>
195 inline meta::Unqualified<String> erase_all(String str, const String& erase) {
196  size_t start_pos = 0;
197  while((start_pos = str.find(erase, start_pos)) != String::npos) {
198  str.replace(start_pos, erase.length(), "");
199  }
200  return str;
201 }
202 } // string
203 } // gears
204 
205 #endif // GEARS_STRING_REPLACE_HPP