Directory: | ./ |
---|---|
File: | tmp_project/StringUtils/src/string_function.cpp |
Date: | 2024-12-09 11:00:39 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 267 | 268 | 99.6% |
Branches: | 259 | 276 | 93.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | |||
8 | #include "string_function.h" | ||
9 | |||
10 | ///Find a char in a string | ||
11 | /** @param str : string to be used | ||
12 | * @param ch : char to be searched | ||
13 | * @return true if the char has been found, false otherwise | ||
14 | */ | ||
15 | 99 | bool findInString(const std::string& str, char ch){ | |
16 | 99 | std::string::const_iterator it = str.begin(); | |
17 |
2/2✓ Branch 2 taken 279 times.
✓ Branch 3 taken 54 times.
|
333 | while(it != str.end()){ |
18 |
2/2✓ Branch 1 taken 45 times.
✓ Branch 2 taken 234 times.
|
279 | if(*it == ch) return true; |
19 | 234 | ++it; | |
20 | } | ||
21 | 54 | return false; | |
22 | } | ||
23 | |||
24 | ///Find multiple chars in a string | ||
25 | /** @param str : string to be used | ||
26 | * @param chars : chars to be searched | ||
27 | * @return true if one of the chars has been found, false otherwise | ||
28 | */ | ||
29 | 15 | bool findCharsInString(const std::string & str, const std::string & chars){ | |
30 |
6/6✓ Branch 1 taken 13 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 12 times.
|
15 | if(str.size() == 0lu || chars.size() == 0lu){return false;} |
31 | 12 | bool foundChar = false; | |
32 | 12 | long unsigned int i(0lu), size(chars.size()); | |
33 |
4/4✓ Branch 0 taken 20 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 3 times.
|
29 | while(!foundChar && i < size){ |
34 | 17 | foundChar = findInString(str, chars[i]); | |
35 | 17 | ++i; | |
36 | } | ||
37 | 12 | return foundChar; | |
38 | } | ||
39 | |||
40 | ///Find a string in a list of string | ||
41 | /** @param listStr : list of string | ||
42 | * @param str : string to be searched | ||
43 | * @return true if the string has been found, false otherwise | ||
44 | */ | ||
45 | 9 | bool findInListString(const std::list<std::string> & listStr, const std::string & str){ | |
46 |
6/6✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 7 times.
|
9 | if(listStr.size() == 0lu || str == ""){return false;} |
47 | 7 | bool isSearch(true); | |
48 | 7 | std::list<std::string>::const_iterator it(listStr.begin()); | |
49 |
6/6✓ Branch 2 taken 15 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 14 times.
✓ Branch 7 taken 7 times.
|
21 | while(it != listStr.end() && isSearch){ |
50 | 14 | isSearch = *it != str; | |
51 | 14 | ++it; | |
52 | } | ||
53 | 7 | return !isSearch; | |
54 | } | ||
55 | |||
56 | ///Find a string in a vector of string | ||
57 | /** @param vecStr : vector of string | ||
58 | * @param str : string to be searched | ||
59 | * @return true if the string has been found, false otherwise | ||
60 | */ | ||
61 | 9 | bool findInVectorString(const std::vector<std::string> & vecStr, const std::string & str){ | |
62 |
6/6✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 7 times.
|
9 | if(vecStr.size() == 0lu || str == ""){return false;} |
63 | 7 | bool isSearch(true); | |
64 | 7 | std::vector<std::string>::const_iterator it(vecStr.begin()); | |
65 |
6/6✓ Branch 2 taken 15 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 14 times.
✓ Branch 7 taken 7 times.
|
21 | while(it != vecStr.end() && isSearch){ |
66 | 14 | isSearch = *it != str; | |
67 | 14 | ++it; | |
68 | } | ||
69 | 7 | return !isSearch; | |
70 | } | ||
71 | |||
72 | ///Erase first char in a string | ||
73 | /** @param str : string to be modifed | ||
74 | * @param chars : chars to be searched and removed | ||
75 | @return modifed string | ||
76 | */ | ||
77 | 9 | std::string eraseFirstCharsInStr(const std::string& str, const std::string & chars){ | |
78 |
1/1✓ Branch 1 taken 9 times.
|
9 | std::string buffer(str); |
79 | 9 | bool continuer = true; | |
80 | 9 | std::string::iterator it = buffer.begin(); | |
81 | //Let's remove the first chars | ||
82 |
5/6✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 21 times.
✓ Branch 5 taken 9 times.
✓ Branch 6 taken 21 times.
✓ Branch 7 taken 9 times.
|
30 | while(it != buffer.end() && continuer){ |
83 |
4/4✓ Branch 2 taken 21 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 9 times.
✓ Branch 8 taken 12 times.
|
21 | if(findInString(chars, *it)){it = buffer.erase(it);} |
84 | else{ | ||
85 | 9 | continuer = false; | |
86 | 9 | it++; | |
87 | } | ||
88 | } | ||
89 | 18 | return buffer; | |
90 | } | ||
91 | |||
92 | ///Erase first and last char in a string | ||
93 | /** @param str : string to be modifed | ||
94 | * @param chars : chars to be searched and removed | ||
95 | @return modifed string | ||
96 | */ | ||
97 | 10 | std::string eraseLastCharsInStr(const std::string& str, const std::string & chars){ | |
98 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 times.
|
10 | if(str.size() > 0lu){ |
99 | 9 | size_t nbCharToRemove(0lu); | |
100 | 9 | std::string::const_reverse_iterator it(str.rbegin()); | |
101 |
3/3✓ Branch 2 taken 21 times.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 9 times.
|
21 | while(findInString(chars, *it)){ |
102 | 12 | ++it; | |
103 | 12 | ++nbCharToRemove; | |
104 | } | ||
105 | |||
106 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5 times.
|
9 | if(nbCharToRemove == 0lu){ |
107 |
1/1✓ Branch 1 taken 4 times.
|
4 | return str; |
108 | }else{ | ||
109 |
1/1✓ Branch 2 taken 5 times.
|
5 | std::string buffer(str.substr(0, str.size() - nbCharToRemove)); |
110 | 5 | return buffer; | |
111 | 5 | } | |
112 | }else{ | ||
113 | 1 | return str; | |
114 | } | ||
115 | } | ||
116 | |||
117 | ///Erase first and last char in a string | ||
118 | /** @param str : string to be modifed | ||
119 | * @param chars : chars to be searched and removed | ||
120 | @return modifed string | ||
121 | */ | ||
122 | 5 | std::string eraseFirstLastChars(const std::string& str, const std::string & chars){ | |
123 |
1/1✓ Branch 1 taken 5 times.
|
5 | std::string buffer(eraseFirstCharsInStr(str, chars)); |
124 |
1/1✓ Branch 1 taken 5 times.
|
10 | return eraseLastCharsInStr(buffer, chars); |
125 | 5 | } | |
126 | |||
127 | ///Erase first and last char in a vector of strings | ||
128 | /** @param vecStr : vector of string to be modifed | ||
129 | * @param chars : chars to be searched and removed | ||
130 | @return modifed vector of strings | ||
131 | */ | ||
132 | 1 | std::vector<std::string> eraseFirstLastChars(const std::vector<std::string> & vecStr, const std::string & chars){ | |
133 | 1 | std::vector<std::string> vecOut; | |
134 |
2/2✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 | for(std::vector<std::string>::const_iterator it(vecStr.begin()); it != vecStr.end(); ++it){ |
135 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | vecOut.push_back(eraseFirstLastChars(*it, chars)); |
136 | } | ||
137 | 1 | return vecOut; | |
138 | } | ||
139 | |||
140 | ///Count number of chararacters ch in string | ||
141 | /** @param str : string to be used | ||
142 | * @param ch : character to be serached | ||
143 | * @return number of character ch in string | ||
144 | */ | ||
145 | 18 | size_t countNbChar(const std::string & str, char ch){ | |
146 | 18 | size_t nbChar(0lu); | |
147 | 18 | std::string::const_iterator it(str.begin()); | |
148 |
2/2✓ Branch 2 taken 304 times.
✓ Branch 3 taken 18 times.
|
322 | while(it != str.end()){ |
149 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 284 times.
|
304 | if(*it == ch) nbChar++; |
150 | 304 | it++; | |
151 | } | ||
152 | 18 | return nbChar; | |
153 | } | ||
154 | |||
155 | ///copie la string str en effaçant le caractère ch | ||
156 | /** @param str : chaîne à copier | ||
157 | @param ch : caractère à effacer | ||
158 | @return string sans le caractère ch | ||
159 | */ | ||
160 | 10 | std::string eraseCharInStr(const std::string& str, char ch){ | |
161 |
1/1✓ Branch 2 taken 10 times.
|
10 | std::string buffer = ""; |
162 |
2/2✓ Branch 4 taken 264 times.
✓ Branch 5 taken 10 times.
|
274 | for(std::string::const_iterator it = str.begin(); it != str.end(); it++){ |
163 |
3/3✓ Branch 1 taken 256 times.
✓ Branch 2 taken 8 times.
✓ Branch 5 taken 256 times.
|
264 | if(*it != ch) buffer += *it; |
164 | } | ||
165 | 10 | return buffer; | |
166 | } | ||
167 | |||
168 | ///copie la string str en effaçant les caractères rmchs | ||
169 | /** @param str : chaîne à copier | ||
170 | @param rmchs : caractères à effacer | ||
171 | @return string sans les caractères rmchs | ||
172 | */ | ||
173 | 4 | std::string eraseCharsInStr(const std::string& str, const std::string & rmchs){ | |
174 | 4 | std::string buffer = str; | |
175 |
2/2✓ Branch 3 taken 8 times.
✓ Branch 4 taken 4 times.
|
12 | for(std::string::const_iterator it = rmchs.begin(); it != rmchs.end(); it++){ |
176 |
1/1✓ Branch 2 taken 8 times.
|
8 | buffer = eraseCharInStr(buffer, *it); |
177 | } | ||
178 | 4 | return buffer; | |
179 | } | ||
180 | |||
181 | ///fonction qui remplace un caractère par un autre dans une string | ||
182 | /** @param str : string à modifier | ||
183 | * @param find : caractère à trouver dans la string | ||
184 | * @param replace : caractère à remplacer dans la string | ||
185 | * @return string modifiée | ||
186 | */ | ||
187 | 2 | std::string replaceCharInStr(const std::string& str, char find, char replace){ | |
188 |
1/1✓ Branch 2 taken 2 times.
|
2 | std::string buffer = ""; |
189 |
2/2✓ Branch 4 taken 44 times.
✓ Branch 5 taken 2 times.
|
46 | for(std::string::const_iterator it = str.begin(); it != str.end(); it++){ |
190 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 40 times.
|
44 | if(*it == find){ |
191 |
1/1✓ Branch 1 taken 4 times.
|
4 | buffer += replace; |
192 | }else{ | ||
193 |
1/1✓ Branch 2 taken 40 times.
|
40 | buffer += *it; |
194 | } | ||
195 | } | ||
196 | 2 | return buffer; | |
197 | } | ||
198 | |||
199 | ///Replace all char in the strFind by char replace | ||
200 | /** @param str : string to be modified | ||
201 | * @param strFind : string of the characters to be found | ||
202 | * @param replace : character to be found | ||
203 | * @return modified string | ||
204 | */ | ||
205 | 4 | std::string replaceCharsInStr(const std::string & str, const std::string & strFind, char replace){ | |
206 |
7/7✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 1 times.
✓ Branch 9 taken 3 times.
|
4 | if(str == "" || strFind == "") return str; |
207 |
1/1✓ Branch 1 taken 1 times.
|
1 | std::string out(str); |
208 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | for(long unsigned int i(0lu); i < strFind.size(); ++i){ |
209 |
1/1✓ Branch 2 taken 2 times.
|
2 | out = replaceCharInStr(out, strFind[i], replace); |
210 | } | ||
211 | 1 | return out; | |
212 | 1 | } | |
213 | |||
214 | ///fonction qui remplace un caractère par un autre dans une string | ||
215 | /** @param str : string à modifier | ||
216 | * @param find : caractère à trouver dans la string | ||
217 | * @param replace : chaîne à remplacer dans la string | ||
218 | * @return string modifiée | ||
219 | */ | ||
220 | 1 | std::string replaceCharInStr(const std::string& str, char find, const std::string& replace){ | |
221 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string buffer = ""; |
222 |
2/2✓ Branch 4 taken 22 times.
✓ Branch 5 taken 1 times.
|
23 | for(std::string::const_iterator it = str.begin(); it != str.end(); it++){ |
223 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 20 times.
|
22 | if(*it == find){ |
224 |
1/1✓ Branch 1 taken 2 times.
|
2 | buffer += replace; |
225 | }else{ | ||
226 |
1/1✓ Branch 2 taken 20 times.
|
20 | buffer += *it; |
227 | } | ||
228 | } | ||
229 | 1 | return buffer; | |
230 | } | ||
231 | |||
232 | ///Count the number of patern in string | ||
233 | /** @param src : string to be analysed | ||
234 | * @param patern : patern to be serached | ||
235 | * @return number of occurence of patern in src | ||
236 | */ | ||
237 | 5 | size_t countStrInStr(const std::string & src, const std::string & patern){ | |
238 | 5 | long unsigned int sizePatern(patern.size()); | |
239 |
6/6✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 2 times.
|
5 | if(sizePatern == 0lu || src == ""){return 0lu;} |
240 | 2 | size_t nbPaternFound(0lu); | |
241 | |||
242 | 2 | long unsigned int sizeSrc(src.size()); | |
243 | 2 | long unsigned int beginTest(0lu), nbMatch(0lu); | |
244 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 2 times.
|
45 | for(long unsigned int i(0lu); i < sizeSrc; ++i){ |
245 |
2/2✓ Branch 2 taken 12 times.
✓ Branch 3 taken 31 times.
|
43 | if(src[i] == patern[nbMatch]){ //si le caractère i est le même que le caractère nbMatch |
246 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
|
12 | if(nbMatch == 0lu){ //c'est le premier qu'on teste |
247 | 3 | beginTest = i; //il faut donc se rappeler où on a commencer à faire le teste | |
248 | } | ||
249 | 12 | ++nbMatch; //la prochaîne fois on testera le caractère suivant | |
250 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
|
12 | if(nbMatch == sizePatern){ //dans ce cas, on a tout testé et tout les caractères correspondent, donc on sauvegarde |
251 | 3 | ++nbPaternFound; | |
252 | 3 | beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais) | |
253 | 3 | nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif | |
254 | } | ||
255 | }else{ //si le caractère i n'est pas le même caractère que nbMatch | ||
256 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
|
31 | if(nbMatch != 0lu){ //si on avais déjà tester des caractères avant |
257 | ✗ | i = beginTest; | |
258 | } | ||
259 | 31 | beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais) | |
260 | 31 | nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif | |
261 | } | ||
262 | } | ||
263 | 2 | return nbPaternFound; | |
264 | } | ||
265 | |||
266 | ///Replace a patern by an other in the input string | ||
267 | /** @param[out] out : output string | ||
268 | * @param src : input string | ||
269 | * @param patern : patern to be searched | ||
270 | * @param replace : string which replace patern | ||
271 | */ | ||
272 | 14 | void replaceStrInStr(std::string & out, const std::string & src, const std::string & patern, const std::string & replace){ | |
273 | 14 | long unsigned int sizePatern(patern.size()); | |
274 |
6/6✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 11 times.
|
14 | if(sizePatern == 0lu || src == "") return; |
275 | 11 | out = ""; //on évite les petits désagréments | |
276 | 11 | long unsigned int sizeSrc(src.size()); | |
277 | 11 | long unsigned int beginTest(0lu), nbMatch(0lu); | |
278 |
2/2✓ Branch 0 taken 225 times.
✓ Branch 1 taken 11 times.
|
236 | for(long unsigned int i(0lu); i < sizeSrc; ++i){ |
279 |
2/2✓ Branch 2 taken 60 times.
✓ Branch 3 taken 165 times.
|
225 | if(src[i] == patern[nbMatch]){ //si le caractère i est le même que le caractère nbMatch |
280 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 42 times.
|
60 | if(nbMatch == 0lu){ //c'est le premier qu'on teste |
281 | 18 | beginTest = i; //il faut donc se rappeler où on a commencer à faire le teste | |
282 | } | ||
283 | 60 | ++nbMatch; //la prochaîne fois on testera le caractère suivant | |
284 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 49 times.
|
60 | if(nbMatch == sizePatern){ //dans ce cas, on a tout testé et tout les caractères correspondent, donc on sauvegarde |
285 | 11 | out += replace; //on a trouver le motif patern, donc on le remplace par le motif replace | |
286 | 11 | beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais) | |
287 | 11 | nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif | |
288 | } | ||
289 | }else{ //si le caractère i n'est pas le même caractère que nbMatch | ||
290 |
2/2✓ Branch 0 taken 158 times.
✓ Branch 1 taken 7 times.
|
165 | if(nbMatch == 0lu){ //si on n'en avait pas trouver de bon avant |
291 | 158 | out += src[i]; //on ne change rien à ce caractère | |
292 | }else{ //si on avais déjà tester des caractères avant | ||
293 | 7 | out += src[beginTest]; | |
294 | 7 | i = beginTest; | |
295 | } | ||
296 | 165 | beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais) | |
297 | 165 | nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif | |
298 | } | ||
299 | } | ||
300 | //We are potentially at the end of the source, so no more test | ||
301 | } | ||
302 | |||
303 | ///Replace a patern by an other in the input string | ||
304 | /** @param src : input string | ||
305 | * @param patern : patern to be searched | ||
306 | * @param replace : string which replace patern | ||
307 | * @return output string | ||
308 | */ | ||
309 | 12 | std::string replaceStrInStr(const std::string & src, const std::string & patern, const std::string & replace){ | |
310 |
1/1✓ Branch 2 taken 12 times.
|
12 | std::string result(""); |
311 |
1/1✓ Branch 1 taken 12 times.
|
12 | replaceStrInStr(result, src, patern, replace); |
312 | 12 | return result; | |
313 | } | ||
314 | |||
315 | ///Replace a patern by an other in the input string in a vector of string | ||
316 | /** @param vecSrc : vector of input strings | ||
317 | * @param patern : patern to be searched | ||
318 | * @param replace : string which replace patern | ||
319 | * @return output string | ||
320 | */ | ||
321 | 1 | std::vector<std::string> replaceStrInStr(const std::vector<std::string> & vecSrc, const std::string & patern, const std::string & replace){ | |
322 | 1 | std::vector<std::string> vecOut; | |
323 |
2/2✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 | for(std::vector<std::string>::const_iterator it(vecSrc.begin()); it != vecSrc.end(); ++it){ |
324 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | vecOut.push_back(replaceStrInStr(*it, patern, replace)); |
325 | } | ||
326 | 1 | return vecOut; | |
327 | } | ||
328 | |||
329 | ///Replace all the vector patern in the string srcDest by the replace string | ||
330 | /** @param[out] srcDest : source and modified string | ||
331 | * @param vecPatern : vector of the paterns we want to search | ||
332 | * @param replace : string we want to replace | ||
333 | */ | ||
334 | 5 | void replaceVectorStrInStr(std::string & srcDest, const std::vector<std::string> & vecPatern, const std::string & replace){ | |
335 |
6/6✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 2 times.
|
5 | if(srcDest == "" || vecPatern.size() == 0lu) return; |
336 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 4 taken 2 times.
|
5 | for(std::vector<std::string>::const_iterator it(vecPatern.begin()); it != vecPatern.end(); ++it){ |
337 |
1/1✓ Branch 2 taken 3 times.
|
3 | srcDest = replaceStrInStr(srcDest, *it, replace); |
338 | } | ||
339 | } | ||
340 | |||
341 | ///Replace all the list patern in the string srcDest by the replace string | ||
342 | /** @param[out] srcDest : source and modified string | ||
343 | * @param listPatern : vector of the paterns we want to search | ||
344 | * @param replace : string we want to replace | ||
345 | */ | ||
346 | 5 | void replaceListStrInStr(std::string & srcDest, const std::list<std::string> & listPatern, const std::string & replace){ | |
347 |
6/6✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 2 times.
|
5 | if(srcDest == "" || listPatern.size() == 0lu) return; |
348 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 4 taken 2 times.
|
5 | for(std::list<std::string>::const_iterator it(listPatern.begin()); it != listPatern.end(); ++it){ |
349 |
1/1✓ Branch 2 taken 3 times.
|
3 | srcDest = replaceStrInStr(srcDest, *it, replace); |
350 | } | ||
351 | } | ||
352 | |||
353 | ///Replace a map of string in a string | ||
354 | /** @param[out] out : updated string | ||
355 | * @param src : source string | ||
356 | * @param mapReplace : map of patterns (keys) and value to replace (values) | ||
357 | */ | ||
358 | 2 | void replaceStrInStr(std::string & out, const std::string & src, const std::map<std::string, std::string> & mapReplace){ | |
359 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | if(mapReplace.size() == 0lu){ |
360 |
1/1✓ Branch 1 taken 1 times.
|
1 | out = src; |
361 | 1 | return; | |
362 | } | ||
363 |
1/1✓ Branch 1 taken 1 times.
|
1 | std::string tmpStr(src); |
364 |
2/2✓ Branch 4 taken 2 times.
✓ Branch 5 taken 1 times.
|
3 | for(std::map<std::string, std::string>::const_iterator it(mapReplace.begin()); it != mapReplace.end(); ++it){ |
365 |
1/1✓ Branch 3 taken 2 times.
|
2 | replaceStrInStr(out, tmpStr, it->first, it->second); |
366 |
1/1✓ Branch 1 taken 2 times.
|
2 | tmpStr = out; |
367 | } | ||
368 | 1 | } | |
369 | |||
370 | ///Replace a map of string in a string | ||
371 | /** @param src : source string | ||
372 | * @param mapReplace : map of patterns (keys) and value to replace (values) | ||
373 | * @return updated string | ||
374 | */ | ||
375 | 2 | std::string replaceStrInStr(const std::string & src, const std::map<std::string, std::string> & mapReplace){ | |
376 |
1/1✓ Branch 2 taken 2 times.
|
2 | std::string out(""); |
377 |
1/1✓ Branch 1 taken 2 times.
|
2 | replaceStrInStr(out, src, mapReplace); |
378 | 2 | return out; | |
379 | } | ||
380 | |||
381 | ///Replace all the vector patern in the string srcDest by the replace string | ||
382 | /** @param src : source string | ||
383 | * @param vecPatern : vector of the paterns we want to search | ||
384 | * @param replace : string we want to replace | ||
385 | * @return modified string | ||
386 | */ | ||
387 | 5 | std::string replaceVectorStrInStr(const std::string & src, const std::vector<std::string> & vecPatern, const std::string & replace){ | |
388 | 5 | std::string result(src); | |
389 |
1/1✓ Branch 1 taken 5 times.
|
5 | replaceVectorStrInStr(result, vecPatern, replace); |
390 | 5 | return result; | |
391 | } | ||
392 | |||
393 | ///Replace all the list patern in the string srcDest by the replace string | ||
394 | /** @param src : source string | ||
395 | * @param vecPatern : vector of the paterns we want to search | ||
396 | * @param replace : string we want to replace | ||
397 | * @return modified string | ||
398 | */ | ||
399 | 5 | std::string replaceListStrInStr(const std::string & src, const std::list<std::string> & vecPatern, const std::string & replace){ | |
400 | 5 | std::string result(src); | |
401 |
1/1✓ Branch 1 taken 5 times.
|
5 | replaceListStrInStr(result, vecPatern, replace); |
402 | 5 | return result; | |
403 | } | ||
404 | |||
405 | ///Escape given string with passed characters | ||
406 | /** @param src : string to be excaped | ||
407 | * @param strCharToEscape : list of the characters to be escaped | ||
408 | * @param escapeStr : escape sequence (could be one char) | ||
409 | * @return escaped string | ||
410 | */ | ||
411 | 1 | std::string phoenix_escapeStr(const std::string & src, const std::string & strCharToEscape, const std::string & escapeStr){ | |
412 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string out(""); |
413 |
2/2✓ Branch 1 taken 32 times.
✓ Branch 2 taken 1 times.
|
33 | for(size_t i(0lu); i < src.size(); ++i){ |
414 | 32 | char ch = src[i]; | |
415 |
3/3✓ Branch 1 taken 32 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 25 times.
|
32 | if(findInString(strCharToEscape, ch)){ |
416 |
1/1✓ Branch 1 taken 7 times.
|
7 | out += escapeStr; |
417 | } | ||
418 |
1/1✓ Branch 1 taken 32 times.
|
32 | out += ch; |
419 | } | ||
420 | 1 | return out; | |
421 | } | ||
422 | |||
423 | ///Cut a string the the given separator char | ||
424 | /** @param str : string to be cut | ||
425 | @param separator : separtor char | ||
426 | @return list of string | ||
427 | */ | ||
428 | 145 | std::list<std::string> cutStringList(const std::string & str, char separator){ | |
429 | 145 | std::list<std::string> liste; | |
430 |
1/1✓ Branch 2 taken 145 times.
|
145 | std::string buffer = ""; |
431 |
2/2✓ Branch 4 taken 2627 times.
✓ Branch 5 taken 145 times.
|
2772 | for(std::string::const_iterator it = str.begin(); it != str.end(); ++it){ |
432 |
2/2✓ Branch 1 taken 2388 times.
✓ Branch 2 taken 239 times.
|
2627 | if(*it != separator){ |
433 |
1/1✓ Branch 2 taken 2388 times.
|
2388 | buffer += *it; |
434 | }else{ | ||
435 |
1/1✓ Branch 1 taken 239 times.
|
239 | liste.push_back(buffer); |
436 |
1/1✓ Branch 1 taken 239 times.
|
239 | buffer = ""; |
437 | } | ||
438 | } | ||
439 |
3/3✓ Branch 1 taken 101 times.
✓ Branch 2 taken 44 times.
✓ Branch 4 taken 101 times.
|
145 | if(buffer != ""){liste.push_back(buffer);} |
440 | 290 | return liste; | |
441 | 145 | } | |
442 | |||
443 | ///Cut a string the the given separator char | ||
444 | /** @param str : string to be cut | ||
445 | @param separator : separtor char | ||
446 | @return vector of string | ||
447 | */ | ||
448 | 2 | std::vector<std::string> cutStringVector(const std::string & str, char separator){ | |
449 | 2 | std::vector<std::string> vec; | |
450 |
1/1✓ Branch 2 taken 2 times.
|
2 | std::string buffer = ""; |
451 |
2/2✓ Branch 4 taken 57 times.
✓ Branch 5 taken 2 times.
|
59 | for(std::string::const_iterator it = str.begin(); it != str.end(); ++it){ |
452 |
2/2✓ Branch 1 taken 53 times.
✓ Branch 2 taken 4 times.
|
57 | if(*it != separator){ |
453 |
1/1✓ Branch 2 taken 53 times.
|
53 | buffer += *it; |
454 | }else{ | ||
455 |
1/1✓ Branch 1 taken 4 times.
|
4 | vec.push_back(buffer); |
456 |
1/1✓ Branch 1 taken 4 times.
|
4 | buffer = ""; |
457 | } | ||
458 | } | ||
459 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 1 times.
|
2 | if(buffer != ""){vec.push_back(buffer);} |
460 | 4 | return vec; | |
461 | 2 | } | |
462 | |||
463 | ///Cut a string on white characters ('\t' ou ' ') | ||
464 | /** @param str : string to be cut | ||
465 | @return list of string | ||
466 | */ | ||
467 | 1 | std::list<std::string> cutStringOnSpacesList(const std::string& str){ | |
468 | 1 | std::list<std::string> liste; | |
469 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if(str.size() != 0lu){ |
470 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string buffer(""); |
471 |
2/2✓ Branch 4 taken 13 times.
✓ Branch 5 taken 1 times.
|
14 | for(std::string::const_iterator it(str.begin()); it != str.end(); ++it){ |
472 |
6/8✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 2 times.
✓ Branch 7 taken 11 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 11 times.
✓ Branch 10 taken 2 times.
|
13 | if(*it != '\t' && *it != ' ' && *it !='\n'){ |
473 |
1/1✓ Branch 2 taken 11 times.
|
11 | buffer += *it; |
474 | }else{ | ||
475 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if(buffer != ""){ |
476 |
1/1✓ Branch 1 taken 2 times.
|
2 | liste.push_back(buffer); |
477 |
1/1✓ Branch 1 taken 2 times.
|
2 | buffer = ""; |
478 | } | ||
479 | } | ||
480 | } | ||
481 |
2/3✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
|
1 | if(buffer != "") liste.push_back(buffer); |
482 | 1 | } | |
483 | 1 | return liste; | |
484 | } | ||
485 | |||
486 | ///Cut a string on white characters ('\t' ou ' ') | ||
487 | /** @param str : string to be cut | ||
488 | @return vector of string | ||
489 | */ | ||
490 | 1 | std::vector<std::string> cutStringOnSpacesVector(const std::string& str){ | |
491 | 1 | std::vector<std::string> vec; | |
492 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if(str.size() != 0lu){ |
493 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string buffer(""); |
494 |
2/2✓ Branch 4 taken 13 times.
✓ Branch 5 taken 1 times.
|
14 | for(std::string::const_iterator it(str.begin()); it != str.end(); ++it){ |
495 |
6/8✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 2 times.
✓ Branch 7 taken 11 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 11 times.
✓ Branch 10 taken 2 times.
|
13 | if(*it != '\t' && *it != ' ' && *it !='\n'){ |
496 |
1/1✓ Branch 2 taken 11 times.
|
11 | buffer += *it; |
497 | }else{ | ||
498 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if(buffer != ""){ |
499 |
1/1✓ Branch 1 taken 2 times.
|
2 | vec.push_back(buffer); |
500 |
1/1✓ Branch 1 taken 2 times.
|
2 | buffer = ""; |
501 | } | ||
502 | } | ||
503 | } | ||
504 |
2/3✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
|
1 | if(buffer != "") vec.push_back(buffer); |
505 | 1 | } | |
506 | 1 | return vec; | |
507 | } | ||
508 | |||
509 | ///Copy a string of nbCh starting from begin char | ||
510 | /** @param str : string to be copied | ||
511 | @param begin : first character index | ||
512 | @param nbCh : number of characters to be copied | ||
513 | @return sub string of input string | ||
514 | */ | ||
515 | 3 | std::string copyStr(const std::string& str, long unsigned int begin, long unsigned int nbCh){ | |
516 |
1/4✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
3 | if(str.size() < begin) return ""; |
517 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if(str.size() < begin + nbCh) nbCh = str.size() - begin; |
518 |
1/1✓ Branch 2 taken 3 times.
|
3 | std::string str2(""); |
519 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
|
18 | for(long unsigned int i(begin); i < begin + nbCh; ++i){ |
520 |
1/1✓ Branch 2 taken 15 times.
|
15 | str2 += str[i]; |
521 | } | ||
522 | 3 | return str2; | |
523 | 3 | } | |
524 | |||
525 | |||
526 | ///Check if two string start the same way | ||
527 | /** @param str : string to be tested | ||
528 | @param beginig : begining to be checked | ||
529 | @return true if str starts as beginig | ||
530 | */ | ||
531 | 5 | bool isSameBegining(const std::string & str, const std::string & beginig){ | |
532 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
|
5 | if(str.size() < beginig.size()) return false; |
533 | 3 | std::string::const_iterator it = str.begin(); | |
534 | 3 | std::string::const_iterator it2 = beginig.begin(); | |
535 |
6/6✓ Branch 2 taken 8 times.
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 7 times.
✓ Branch 9 taken 3 times.
|
10 | while(it != str.end() && it2 != beginig.end()){ |
536 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
7 | if(*it != *it2){ return false;} |
537 | 7 | it++; | |
538 | 7 | it2++; | |
539 | } | ||
540 | 3 | return true; | |
541 | } | ||
542 | |||
543 | ///Convert a char pointer into a string (event if the char pointer is NULL) | ||
544 | /** @param ch : char pointer to be converted into a string | ||
545 | * @return corresponding string, or empty string if the input char pointer is NULL | ||
546 | */ | ||
547 | 2 | std::string phoenix_charToString(const char * ch){ | |
548 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if(ch != NULL){ |
549 |
1/1✓ Branch 2 taken 1 times.
|
1 | std::string str(ch); |
550 | 1 | return str; | |
551 | 1 | }else{ | |
552 |
1/1✓ Branch 2 taken 1 times.
|
1 | return ""; |
553 | } | ||
554 | } | ||
555 | |||
556 | ///Get the common begining between str1 and str2 | ||
557 | /** @param str1 : string | ||
558 | * @param str2 : string | ||
559 | * @return common begining between str1 and str2 | ||
560 | */ | ||
561 | 5 | std::string phoenix_getCommonBegining(const std::string & str1, const std::string & str2){ | |
562 |
1/1✓ Branch 2 taken 5 times.
|
5 | std::string out(""); |
563 | 5 | std::string::const_iterator it = str1.begin(); | |
564 | 5 | std::string::const_iterator it2 = str2.begin(); | |
565 |
6/6✓ Branch 2 taken 7 times.
✓ Branch 3 taken 2 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 6 times.
✓ Branch 9 taken 3 times.
|
9 | while(it != str1.end() && it2 != str2.end()){ |
566 |
2/2✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
|
6 | if(*it == *it2){ |
567 |
1/1✓ Branch 2 taken 4 times.
|
4 | out += *it; |
568 | }else{ | ||
569 | 2 | break; | |
570 | } | ||
571 | 4 | it++; | |
572 | 4 | it2++; | |
573 | } | ||
574 | 10 | return out; | |
575 | } | ||
576 | |||
577 |