PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
string_function.cpp
Go to the documentation of this file.
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 
11 
15 bool findInString(const std::string& str, char ch){
16  std::string::const_iterator it = str.begin();
17  while(it != str.end()){
18  if(*it == ch) return true;
19  ++it;
20  }
21  return false;
22 }
23 
25 
29 bool findCharsInString(const std::string & str, const std::string & chars){
30  if(str.size() == 0lu || chars.size() == 0lu){return false;}
31  bool foundChar = false;
32  long unsigned int i(0lu), size(chars.size());
33  while(!foundChar && i < size){
34  foundChar = findInString(str, chars[i]);
35  ++i;
36  }
37  return foundChar;
38 }
39 
41 
45 bool findInListString(const std::list<std::string> & listStr, const std::string & str){
46  if(listStr.size() == 0lu || str == ""){return false;}
47  bool isSearch(true);
48  std::list<std::string>::const_iterator it(listStr.begin());
49  while(it != listStr.end() && isSearch){
50  isSearch = *it != str;
51  ++it;
52  }
53  return !isSearch;
54 }
55 
57 
61 bool findInVectorString(const std::vector<std::string> & vecStr, const std::string & str){
62  if(vecStr.size() == 0lu || str == ""){return false;}
63  bool isSearch(true);
64  std::vector<std::string>::const_iterator it(vecStr.begin());
65  while(it != vecStr.end() && isSearch){
66  isSearch = *it != str;
67  ++it;
68  }
69  return !isSearch;
70 }
71 
73 
77 std::string eraseFirstCharsInStr(const std::string& str, const std::string & chars){
78  std::string buffer(str);
79  bool continuer = true;
80  std::string::iterator it = buffer.begin();
81  //Let's remove the first chars
82  while(it != buffer.end() && continuer){
83  if(findInString(chars, *it)){it = buffer.erase(it);}
84  else{
85  continuer = false;
86  it++;
87  }
88  }
89  return buffer;
90 }
91 
93 
97 std::string eraseLastCharsInStr(const std::string& str, const std::string & chars){
98  if(str.size() > 0lu){
99  size_t nbCharToRemove(0lu);
100  std::string::const_reverse_iterator it(str.rbegin());
101  while(findInString(chars, *it)){
102  ++it;
103  ++nbCharToRemove;
104  }
105 
106  if(nbCharToRemove == 0lu){
107  return str;
108  }else{
109  std::string buffer(str.substr(0, str.size() - nbCharToRemove));
110  return buffer;
111  }
112  }else{
113  return str;
114  }
115 }
116 
118 
122 std::string eraseFirstLastChars(const std::string& str, const std::string & chars){
123  std::string buffer(eraseFirstCharsInStr(str, chars));
124  return eraseLastCharsInStr(buffer, chars);
125 }
126 
128 
132 std::vector<std::string> eraseFirstLastChars(const std::vector<std::string> & vecStr, const std::string & chars){
133  std::vector<std::string> vecOut;
134  for(std::vector<std::string>::const_iterator it(vecStr.begin()); it != vecStr.end(); ++it){
135  vecOut.push_back(eraseFirstLastChars(*it, chars));
136  }
137  return vecOut;
138 }
139 
141 
145 size_t countNbChar(const std::string & str, char ch){
146  size_t nbChar(0lu);
147  std::string::const_iterator it(str.begin());
148  while(it != str.end()){
149  if(*it == ch) nbChar++;
150  it++;
151  }
152  return nbChar;
153 }
154 
156 
160 std::string eraseCharInStr(const std::string& str, char ch){
161  std::string buffer = "";
162  for(std::string::const_iterator it = str.begin(); it != str.end(); it++){
163  if(*it != ch) buffer += *it;
164  }
165  return buffer;
166 }
167 
169 
173 std::string eraseCharsInStr(const std::string& str, const std::string & rmchs){
174  std::string buffer = str;
175  for(std::string::const_iterator it = rmchs.begin(); it != rmchs.end(); it++){
176  buffer = eraseCharInStr(buffer, *it);
177  }
178  return buffer;
179 }
180 
182 
187 std::string replaceCharInStr(const std::string& str, char find, char replace){
188  std::string buffer = "";
189  for(std::string::const_iterator it = str.begin(); it != str.end(); it++){
190  if(*it == find){
191  buffer += replace;
192  }else{
193  buffer += *it;
194  }
195  }
196  return buffer;
197 }
198 
200 
205 std::string replaceCharsInStr(const std::string & str, const std::string & strFind, char replace){
206  if(str == "" || strFind == "") return str;
207  std::string out(str);
208  for(long unsigned int i(0lu); i < strFind.size(); ++i){
209  out = replaceCharInStr(out, strFind[i], replace);
210  }
211  return out;
212 }
213 
215 
220 std::string replaceCharInStr(const std::string& str, char find, const std::string& replace){
221  std::string buffer = "";
222  for(std::string::const_iterator it = str.begin(); it != str.end(); it++){
223  if(*it == find){
224  buffer += replace;
225  }else{
226  buffer += *it;
227  }
228  }
229  return buffer;
230 }
231 
233 
237 size_t countStrInStr(const std::string & src, const std::string & patern){
238  long unsigned int sizePatern(patern.size());
239  if(sizePatern == 0lu || src == ""){return 0lu;}
240  size_t nbPaternFound(0lu);
241 
242  long unsigned int sizeSrc(src.size());
243  long unsigned int beginTest(0lu), nbMatch(0lu);
244  for(long unsigned int i(0lu); i < sizeSrc; ++i){
245  if(src[i] == patern[nbMatch]){ //si le caractère i est le même que le caractère nbMatch
246  if(nbMatch == 0lu){ //c'est le premier qu'on teste
247  beginTest = i; //il faut donc se rappeler où on a commencer à faire le teste
248  }
249  ++nbMatch; //la prochaîne fois on testera le caractère suivant
250  if(nbMatch == sizePatern){ //dans ce cas, on a tout testé et tout les caractères correspondent, donc on sauvegarde
251  ++nbPaternFound;
252  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
253  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  if(nbMatch != 0lu){ //si on avais déjà tester des caractères avant
257  i = beginTest;
258  }
259  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
260  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
261  }
262  }
263  return nbPaternFound;
264 }
265 
267 
272 void replaceStrInStr(std::string & out, const std::string & src, const std::string & patern, const std::string & replace){
273  long unsigned int sizePatern(patern.size());
274  if(sizePatern == 0lu || src == "") return;
275  out = ""; //on évite les petits désagréments
276  long unsigned int sizeSrc(src.size());
277  long unsigned int beginTest(0lu), nbMatch(0lu);
278  for(long unsigned int i(0lu); i < sizeSrc; ++i){
279  if(src[i] == patern[nbMatch]){ //si le caractère i est le même que le caractère nbMatch
280  if(nbMatch == 0lu){ //c'est le premier qu'on teste
281  beginTest = i; //il faut donc se rappeler où on a commencer à faire le teste
282  }
283  ++nbMatch; //la prochaîne fois on testera le caractère suivant
284  if(nbMatch == sizePatern){ //dans ce cas, on a tout testé et tout les caractères correspondent, donc on sauvegarde
285  out += replace; //on a trouver le motif patern, donc on le remplace par le motif replace
286  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
287  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  if(nbMatch == 0lu){ //si on n'en avait pas trouver de bon avant
291  out += src[i]; //on ne change rien à ce caractère
292  }else{ //si on avais déjà tester des caractères avant
293  out += src[beginTest];
294  i = beginTest;
295  }
296  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
297  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 
304 
309 std::string replaceStrInStr(const std::string & src, const std::string & patern, const std::string & replace){
310  std::string result("");
311  replaceStrInStr(result, src, patern, replace);
312  return result;
313 }
314 
316 
321 std::vector<std::string> replaceStrInStr(const std::vector<std::string> & vecSrc, const std::string & patern, const std::string & replace){
322  std::vector<std::string> vecOut;
323  for(std::vector<std::string>::const_iterator it(vecSrc.begin()); it != vecSrc.end(); ++it){
324  vecOut.push_back(replaceStrInStr(*it, patern, replace));
325  }
326  return vecOut;
327 }
328 
330 
334 void replaceVectorStrInStr(std::string & srcDest, const std::vector<std::string> & vecPatern, const std::string & replace){
335  if(srcDest == "" || vecPatern.size() == 0lu) return;
336  for(std::vector<std::string>::const_iterator it(vecPatern.begin()); it != vecPatern.end(); ++it){
337  srcDest = replaceStrInStr(srcDest, *it, replace);
338  }
339 }
340 
342 
346 void replaceListStrInStr(std::string & srcDest, const std::list<std::string> & listPatern, const std::string & replace){
347  if(srcDest == "" || listPatern.size() == 0lu) return;
348  for(std::list<std::string>::const_iterator it(listPatern.begin()); it != listPatern.end(); ++it){
349  srcDest = replaceStrInStr(srcDest, *it, replace);
350  }
351 }
352 
354 
358 void replaceStrInStr(std::string & out, const std::string & src, const std::map<std::string, std::string> & mapReplace){
359  if(mapReplace.size() == 0lu){
360  out = src;
361  return;
362  }
363  std::string tmpStr(src);
364  for(std::map<std::string, std::string>::const_iterator it(mapReplace.begin()); it != mapReplace.end(); ++it){
365  replaceStrInStr(out, tmpStr, it->first, it->second);
366  tmpStr = out;
367  }
368 }
369 
371 
375 std::string replaceStrInStr(const std::string & src, const std::map<std::string, std::string> & mapReplace){
376  std::string out("");
377  replaceStrInStr(out, src, mapReplace);
378  return out;
379 }
380 
382 
387 std::string replaceVectorStrInStr(const std::string & src, const std::vector<std::string> & vecPatern, const std::string & replace){
388  std::string result(src);
389  replaceVectorStrInStr(result, vecPatern, replace);
390  return result;
391 }
392 
394 
399 std::string replaceListStrInStr(const std::string & src, const std::list<std::string> & vecPatern, const std::string & replace){
400  std::string result(src);
401  replaceListStrInStr(result, vecPatern, replace);
402  return result;
403 }
404 
406 
411 std::string phoenix_escapeStr(const std::string & src, const std::string & strCharToEscape, const std::string & escapeStr){
412  std::string out("");
413  for(size_t i(0lu); i < src.size(); ++i){
414  char ch = src[i];
415  if(findInString(strCharToEscape, ch)){
416  out += escapeStr;
417  }
418  out += ch;
419  }
420  return out;
421 }
422 
424 
428 std::list<std::string> cutStringList(const std::string & str, char separator){
429  std::list<std::string> liste;
430  std::string buffer = "";
431  for(std::string::const_iterator it = str.begin(); it != str.end(); ++it){
432  if(*it != separator){
433  buffer += *it;
434  }else{
435  liste.push_back(buffer);
436  buffer = "";
437  }
438  }
439  if(buffer != ""){liste.push_back(buffer);}
440  return liste;
441 }
442 
444 
448 std::vector<std::string> cutStringVector(const std::string & str, char separator){
449  std::vector<std::string> vec;
450  std::string buffer = "";
451  for(std::string::const_iterator it = str.begin(); it != str.end(); ++it){
452  if(*it != separator){
453  buffer += *it;
454  }else{
455  vec.push_back(buffer);
456  buffer = "";
457  }
458  }
459  if(buffer != ""){vec.push_back(buffer);}
460  return vec;
461 }
462 
464 
467 std::list<std::string> cutStringOnSpacesList(const std::string& str){
468  std::list<std::string> liste;
469  if(str.size() != 0lu){
470  std::string buffer("");
471  for(std::string::const_iterator it(str.begin()); it != str.end(); ++it){
472  if(*it != '\t' && *it != ' ' && *it !='\n'){
473  buffer += *it;
474  }else{
475  if(buffer != ""){
476  liste.push_back(buffer);
477  buffer = "";
478  }
479  }
480  }
481  if(buffer != "") liste.push_back(buffer);
482  }
483  return liste;
484 }
485 
487 
490 std::vector<std::string> cutStringOnSpacesVector(const std::string& str){
491  std::vector<std::string> vec;
492  if(str.size() != 0lu){
493  std::string buffer("");
494  for(std::string::const_iterator it(str.begin()); it != str.end(); ++it){
495  if(*it != '\t' && *it != ' ' && *it !='\n'){
496  buffer += *it;
497  }else{
498  if(buffer != ""){
499  vec.push_back(buffer);
500  buffer = "";
501  }
502  }
503  }
504  if(buffer != "") vec.push_back(buffer);
505  }
506  return vec;
507 }
508 
510 
515 std::string copyStr(const std::string& str, long unsigned int begin, long unsigned int nbCh){
516  if(str.size() < begin) return "";
517  if(str.size() < begin + nbCh) nbCh = str.size() - begin;
518  std::string str2("");
519  for(long unsigned int i(begin); i < begin + nbCh; ++i){
520  str2 += str[i];
521  }
522  return str2;
523 }
524 
525 
527 
531 bool isSameBegining(const std::string & str, const std::string & beginig){
532  if(str.size() < beginig.size()) return false;
533  std::string::const_iterator it = str.begin();
534  std::string::const_iterator it2 = beginig.begin();
535  while(it != str.end() && it2 != beginig.end()){
536  if(*it != *it2){ return false;}
537  it++;
538  it2++;
539  }
540  return true;
541 }
542 
544 
547 std::string phoenix_charToString(const char * ch){
548  if(ch != NULL){
549  std::string str(ch);
550  return str;
551  }else{
552  return "";
553  }
554 }
555 
557 
561 std::string phoenix_getCommonBegining(const std::string & str1, const std::string & str2){
562  std::string out("");
563  std::string::const_iterator it = str1.begin();
564  std::string::const_iterator it2 = str2.begin();
565  while(it != str1.end() && it2 != str2.end()){
566  if(*it == *it2){
567  out += *it;
568  }else{
569  break;
570  }
571  it++;
572  it2++;
573  }
574  return out;
575 }
576 
cutStringOnSpacesVector
std::vector< std::string > cutStringOnSpacesVector(const std::string &str)
Cut a string on white characters ('&#92;t' ou ' ')
Definition: string_function.cpp:490
eraseCharInStr
std::string eraseCharInStr(const std::string &str, char ch)
copie la string str en effaçant le caractère ch
Definition: string_function.cpp:160
findInVectorString
bool findInVectorString(const std::vector< std::string > &vecStr, const std::string &str)
Find a string in a vector of string.
Definition: string_function.cpp:61
eraseFirstCharsInStr
std::string eraseFirstCharsInStr(const std::string &str, const std::string &chars)
Erase first char in a string.
Definition: string_function.cpp:77
replaceListStrInStr
void replaceListStrInStr(std::string &srcDest, const std::list< std::string > &listPatern, const std::string &replace)
Replace all the list patern in the string srcDest by the replace string.
Definition: string_function.cpp:346
phoenix_charToString
std::string phoenix_charToString(const char *ch)
Convert a char pointer into a string (event if the char pointer is NULL)
Definition: string_function.cpp:547
cutStringOnSpacesList
std::list< std::string > cutStringOnSpacesList(const std::string &str)
Cut a string on white characters ('&#92;t' ou ' ')
Definition: string_function.cpp:467
findCharsInString
bool findCharsInString(const std::string &str, const std::string &chars)
Find multiple chars in a string.
Definition: string_function.cpp:29
findInString
bool findInString(const std::string &str, char ch)
Find a char in a string.
Definition: string_function.cpp:15
replaceCharInStr
std::string replaceCharInStr(const std::string &str, char find, char replace)
fonction qui remplace un caractère par un autre dans une string
Definition: string_function.cpp:187
isSameBegining
bool isSameBegining(const std::string &str, const std::string &beginig)
Check if two string start the same way.
Definition: string_function.cpp:531
cutStringList
std::list< std::string > cutStringList(const std::string &str, char separator)
Cut a string the the given separator char.
Definition: string_function.cpp:428
replaceVectorStrInStr
void replaceVectorStrInStr(std::string &srcDest, const std::vector< std::string > &vecPatern, const std::string &replace)
Replace all the vector patern in the string srcDest by the replace string.
Definition: string_function.cpp:334
copyStr
std::string copyStr(const std::string &str, long unsigned int begin, long unsigned int nbCh)
Copy a string of nbCh starting from begin char.
Definition: string_function.cpp:515
eraseCharsInStr
std::string eraseCharsInStr(const std::string &str, const std::string &rmchs)
copie la string str en effaçant les caractères rmchs
Definition: string_function.cpp:173
string_function.h
findInListString
bool findInListString(const std::list< std::string > &listStr, const std::string &str)
Find a string in a list of string.
Definition: string_function.cpp:45
createReleaseCurl.str
str
Definition: createReleaseCurl.py:128
countStrInStr
size_t countStrInStr(const std::string &src, const std::string &patern)
Count the number of patern in string.
Definition: string_function.cpp:237
replaceCharsInStr
std::string replaceCharsInStr(const std::string &str, const std::string &strFind, char replace)
Replace all char in the strFind by char replace.
Definition: string_function.cpp:205
eraseLastCharsInStr
std::string eraseLastCharsInStr(const std::string &str, const std::string &chars)
Erase first and last char in a string.
Definition: string_function.cpp:97
eraseFirstLastChars
std::string eraseFirstLastChars(const std::string &str, const std::string &chars)
Erase first and last char in a string.
Definition: string_function.cpp:122
phoenix_escapeStr
std::string phoenix_escapeStr(const std::string &src, const std::string &strCharToEscape, const std::string &escapeStr)
Escape given string with passed characters.
Definition: string_function.cpp:411
cutStringVector
std::vector< std::string > cutStringVector(const std::string &str, char separator)
Cut a string the the given separator char.
Definition: string_function.cpp:448
replaceStrInStr
void replaceStrInStr(std::string &out, const std::string &src, const std::string &patern, const std::string &replace)
Replace a patern by an other in the input string.
Definition: string_function.cpp:272
phoenix_getCommonBegining
std::string phoenix_getCommonBegining(const std::string &str1, const std::string &str2)
Get the common begining between str1 and str2.
Definition: string_function.cpp:561
countNbChar
size_t countNbChar(const std::string &str, char ch)
Count number of chararacters ch in string.
Definition: string_function.cpp:145