1 /*******************************************************************************
2 * tlx/string/replace.cpp
3 *
4 * Part of tlx - http://panthema.net/tlx
5 *
6 * Copyright (C) 2007-2017 Timo Bingmann <tb@panthema.net>
7 *
8 * All rights reserved. Published under the Boost Software License, Version 1.0
9 ******************************************************************************/
10
11 #include <tlx/string/replace.hpp>
12
13 #include <algorithm>
14 #include <cstring>
15
16 namespace tlx {
17
18 /******************************************************************************/
19 // replace_first() in-place
20
replace_first(std::string * str,const std::string & needle,const std::string & instead)21 std::string& replace_first(
22 std::string* str, const std::string& needle, const std::string& instead) {
23
24 std::string::size_type firstpos = str->find(needle);
25
26 if (firstpos != std::string::npos)
27 str->replace(firstpos, needle.size(), instead);
28
29 return *str;
30 }
31
replace_first(std::string * str,const std::string & needle,const char * instead)32 std::string& replace_first(
33 std::string* str, const std::string& needle, const char* instead) {
34
35 std::string::size_type firstpos = str->find(needle);
36
37 if (firstpos != std::string::npos)
38 str->replace(firstpos, needle.size(), instead);
39
40 return *str;
41 }
42
replace_first(std::string * str,const char * needle,const std::string & instead)43 std::string& replace_first(
44 std::string* str, const char* needle, const std::string& instead) {
45
46 std::string::size_type firstpos = str->find(needle);
47
48 if (firstpos != std::string::npos)
49 str->replace(firstpos, strlen(needle), instead);
50
51 return *str;
52 }
53
replace_first(std::string * str,const char * needle,const char * instead)54 std::string& replace_first(
55 std::string* str, const char* needle, const char* instead) {
56
57 std::string::size_type firstpos = str->find(needle);
58
59 if (firstpos != std::string::npos)
60 str->replace(firstpos, strlen(needle), instead);
61
62 return *str;
63 }
64
replace_first(std::string * str,char needle,char instead)65 std::string& replace_first(std::string* str, char needle, char instead) {
66
67 std::string::size_type firstpos = str->find(needle);
68
69 if (firstpos != std::string::npos)
70 (*str)[firstpos] = instead;
71
72 return *str;
73 }
74
75 /******************************************************************************/
76 // replace_first() copy
77
replace_first(const std::string & str,const std::string & needle,const std::string & instead)78 std::string replace_first(
79 const std::string& str,
80 const std::string& needle, const std::string& instead) {
81
82 std::string newstr = str;
83 std::string::size_type firstpos = newstr.find(needle);
84
85 if (firstpos != std::string::npos)
86 newstr.replace(firstpos, needle.size(), instead);
87
88 return newstr;
89 }
90
replace_first(const std::string & str,const std::string & needle,const char * instead)91 std::string replace_first(
92 const std::string& str, const std::string& needle, const char* instead) {
93
94 std::string newstr = str;
95 std::string::size_type firstpos = newstr.find(needle);
96
97 if (firstpos != std::string::npos)
98 newstr.replace(firstpos, needle.size(), instead);
99
100 return newstr;
101 }
102
replace_first(const std::string & str,const char * needle,const std::string & instead)103 std::string replace_first(
104 const std::string& str, const char* needle, const std::string& instead) {
105
106 std::string newstr = str;
107 std::string::size_type firstpos = newstr.find(needle);
108
109 if (firstpos != std::string::npos)
110 newstr.replace(firstpos, strlen(needle), instead);
111
112 return newstr;
113 }
114
replace_first(const std::string & str,const char * needle,const char * instead)115 std::string replace_first(
116 const std::string& str, const char* needle, const char* instead) {
117
118 std::string newstr = str;
119 std::string::size_type firstpos = newstr.find(needle);
120
121 if (firstpos != std::string::npos)
122 newstr.replace(firstpos, strlen(needle), instead);
123
124 return newstr;
125 }
126
replace_first(const std::string & str,char needle,char instead)127 std::string replace_first(const std::string& str, char needle, char instead) {
128
129 std::string newstr = str;
130 std::string::size_type firstpos = newstr.find(needle);
131
132 if (firstpos != std::string::npos)
133 newstr[firstpos] = instead;
134
135 return newstr;
136 }
137
138 /******************************************************************************/
139 // replace_all() in-place
140
replace_all(std::string * str,const std::string & needle,const std::string & instead)141 std::string& replace_all(
142 std::string* str, const std::string& needle, const std::string& instead) {
143
144 std::string::size_type lastpos = 0, thispos;
145
146 while ((thispos = str->find(needle, lastpos)) != std::string::npos)
147 {
148 str->replace(thispos, needle.size(), instead);
149 lastpos = thispos + instead.size();
150 }
151 return *str;
152 }
153
replace_all(std::string * str,const std::string & needle,const char * instead)154 std::string& replace_all(
155 std::string* str, const std::string& needle, const char* instead) {
156
157 std::string::size_type lastpos = 0, thispos;
158 size_t instead_size = strlen(instead);
159
160 while ((thispos = str->find(needle, lastpos)) != std::string::npos)
161 {
162 str->replace(thispos, needle.size(), instead);
163 lastpos = thispos + instead_size;
164 }
165 return *str;
166 }
167
replace_all(std::string * str,const char * needle,const std::string & instead)168 std::string& replace_all(
169 std::string* str, const char* needle, const std::string& instead) {
170
171 std::string::size_type lastpos = 0, thispos;
172 size_t needle_size = strlen(needle);
173
174 while ((thispos = str->find(needle, lastpos)) != std::string::npos)
175 {
176 str->replace(thispos, needle_size, instead);
177 lastpos = thispos + instead.size();
178 }
179 return *str;
180 }
181
replace_all(std::string * str,const char * needle,const char * instead)182 std::string& replace_all(
183 std::string* str, const char* needle, const char* instead) {
184
185 std::string::size_type lastpos = 0, thispos;
186 size_t needle_size = strlen(needle);
187 size_t instead_size = strlen(instead);
188
189 while ((thispos = str->find(needle, lastpos)) != std::string::npos)
190 {
191 str->replace(thispos, needle_size, instead);
192 lastpos = thispos + instead_size;
193 }
194 return *str;
195 }
196
replace_all(std::string * str,char needle,char instead)197 std::string& replace_all(std::string* str, char needle, char instead) {
198
199 std::string::size_type lastpos = 0, thispos;
200
201 while ((thispos = str->find(needle, lastpos)) != std::string::npos)
202 {
203 (*str)[thispos] = instead;
204 lastpos = thispos + 1;
205 }
206 return *str;
207 }
208
209 /******************************************************************************/
210 // replace_all() copy
211
replace_all(const std::string & str,const std::string & needle,const std::string & instead)212 std::string replace_all(
213 const std::string& str,
214 const std::string& needle, const std::string& instead) {
215
216 std::string newstr = str;
217 std::string::size_type lastpos = 0, thispos;
218
219 while ((thispos = newstr.find(needle, lastpos)) != std::string::npos)
220 {
221 newstr.replace(thispos, needle.size(), instead);
222 lastpos = thispos + instead.size();
223 }
224 return newstr;
225 }
226
replace_all(const std::string & str,const std::string & needle,const char * instead)227 std::string replace_all(
228 const std::string& str, const std::string& needle, const char* instead) {
229
230 std::string newstr = str;
231 std::string::size_type lastpos = 0, thispos;
232 size_t instead_size = strlen(instead);
233
234 while ((thispos = newstr.find(needle, lastpos)) != std::string::npos)
235 {
236 newstr.replace(thispos, needle.size(), instead);
237 lastpos = thispos + instead_size;
238 }
239 return newstr;
240 }
241
replace_all(const std::string & str,const char * needle,const std::string & instead)242 std::string replace_all(
243 const std::string& str, const char* needle, const std::string& instead) {
244
245 std::string newstr = str;
246 std::string::size_type lastpos = 0, thispos;
247 size_t needle_size = strlen(needle);
248
249 while ((thispos = newstr.find(needle, lastpos)) != std::string::npos)
250 {
251 newstr.replace(thispos, needle_size, instead);
252 lastpos = thispos + instead.size();
253 }
254 return newstr;
255 }
256
replace_all(const std::string & str,const char * needle,const char * instead)257 std::string replace_all(
258 const std::string& str, const char* needle, const char* instead) {
259
260 std::string newstr = str;
261 std::string::size_type lastpos = 0, thispos;
262 size_t needle_size = strlen(needle);
263 size_t instead_size = strlen(instead);
264
265 while ((thispos = newstr.find(needle, lastpos)) != std::string::npos)
266 {
267 newstr.replace(thispos, needle_size, instead);
268 lastpos = thispos + instead_size;
269 }
270 return newstr;
271 }
272
replace_all(const std::string & str,char needle,char instead)273 std::string replace_all(const std::string& str, char needle, char instead) {
274
275 std::string newstr = str;
276 std::string::size_type lastpos = 0, thispos;
277
278 while ((thispos = newstr.find(needle, lastpos)) != std::string::npos)
279 {
280 newstr[thispos] = instead;
281 lastpos = thispos + 1;
282 }
283 return newstr;
284 }
285
286 } // namespace tlx
287
288 /******************************************************************************/
289