PhoenixSlider  2.0.0
Tool to create HTML presentation automatically
PString.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 "PString.h"
9 
11 
14 PString phoenix_charToString(const char * ch){
15  if(ch != NULL){
16  PString str(ch);
17  return str;
18  }else{
19  return "";
20  }
21 }
22 
24 
28  return (ch >= 65 && ch <= 90);
29 }
30 
32 
36  return (ch >= 97 && ch <= 122);
37 }
38 
40 
43 bool phoenix_isCharNumber(char ch){
44  return (ch >= 48 && ch <= 57);
45 }
46 
48 
52 void eraseFirstLastChar(PVecString & vecOut, const PVecString & vecStr, const PString & vecChar){
53  for(PVecString::const_iterator it(vecStr.begin()); it != vecStr.end(); ++it){
54  vecOut.push_back(it->eraseFirstLastChar(vecChar));
55  }
56 }
57 
59 
63 PVecString eraseFirstLastChar(const PVecString & vecStr, const PString & vecChar){
64  PVecString vecOut;
65  eraseFirstLastChar(vecOut, vecStr, vecChar);
66  return vecOut;
67 }
68 
71  :std::string("")
72 {
74 }
75 
77 
79 PString::PString(const char * str)
80  :std::string(str)
81 {
83 }
84 
86 
88 PString::PString(const std::string & str)
89  :std::string(str)
90 {
92 }
93 
95 
97 PString::PString(const PString & other)
98  :std::string()
99 {
100  copyPString(other);
101 }
102 
105 
106 }
107 
109 
113  copyPString(other);
114  return *this;
115 }
116 
118 
121 PString & PString::operator = (const std::string & other){
122  copyPString(other);
123  return *this;
124 }
125 
127 
131  return add(other);
132 }
133 
135 
138 PString & PString::operator += (const std::string & other){
139  return add(other);
140 }
141 
143 
147  return add(ch);
148 }
149 
151 
154 PString & PString::add(const PString & other){
155  concatenatePString(other);
156  return *this;
157 }
158 
160 
163 PString & PString::add(const std::string & other){
164  concatenatePString(other);
165  return *this;
166 }
167 
169 
173  std::string str;
174  str+= ch;
175  concatenatePString(str);
176  return *this;
177 }
178 
180 
183 PString & PString::add(const char * str){
185  return *this;
186 }
187 
189 
193 PString operator + (const PString & other1, const PString & other2){
194  PString res(other1);
195  res += other2;
196  return res;
197 }
198 
200 
204 size_t PString::findPatternIndex(const PString & pattern, size_t offset) const{
205  size_t sizePatern(pattern.size());
206  const PString & src = *this;
207  if(sizePatern == 0lu || src.size() < offset + sizePatern){return size();}
208 
209  size_t sizeSrc(src.size());
210  size_t beginTest(0lu), nbMatch(0lu);
211  for(size_t i(offset); i < sizeSrc; ++i){
212  if(src[i] == pattern[nbMatch]){ //si le caractère i est le même que le caractère nbMatch
213  if(nbMatch == 0lu){ //c'est le premier qu'on teste
214  beginTest = i; //il faut donc se rappeler où on a commencer à faire le teste
215  }
216  ++nbMatch; //la prochaîne fois on testera le caractère suivant
217  if(nbMatch == sizePatern){ //dans ce cas, on a tout testé et tout les caractères correspondent, donc on sauvegarde
218  return beginTest; //on a trouver le motif pattern, donc on le remplace par le motif replaceStr
219  }
220  }else{ //si le caractère i n'est pas le même caractère que nbMatch
221  if(nbMatch != 0lu){ //si on avais déjà tester des caractères avant
222  i = beginTest; //On revient là où l'on avait fait le premier test
223  }
224  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
225  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
226  }
227  }
228  return sizeSrc;
229 }
230 
232 
236 PString PString::getBetweenDelimiter(const PString & beginPattern, const PString & endPattern) const{
237  size_t indexBeginPattern(findPatternIndex(beginPattern) + beginPattern.size());
238  size_t indexEndPattern(findPatternIndex(endPattern, indexBeginPattern));
239  if(indexBeginPattern != size() && indexEndPattern != size()){
240  return substr(indexBeginPattern, indexEndPattern - indexBeginPattern);
241  }else{
242  return "";
243  }
244 }
245 
247 
251 PString PString::replace(const PString & pattern, const PString & replaceStr) const{
252  size_t sizePatern(pattern.size());
253  const PString & src = *this;
254  if(sizePatern == 0lu || src.size() == 0lu) return *this;
255  PString out(""); //on évite les petits désagréments
256  size_t sizeSrc(src.size());
257  size_t beginTest(0lu), nbMatch(0lu);
258  for(size_t i(0lu); i < sizeSrc; ++i){
259  if(src[i] == pattern[nbMatch]){ //si le caractère i est le même que le caractère nbMatch
260  if(nbMatch == 0lu){ //c'est le premier qu'on teste
261  beginTest = i; //il faut donc se rappeler où on a commencer à faire le teste
262  }
263  ++nbMatch; //la prochaîne fois on testera le caractère suivant
264  if(nbMatch == sizePatern){ //dans ce cas, on a tout testé et tout les caractères correspondent, donc on sauvegarde
265  out += replaceStr; //on a trouver le motif pattern, donc on le remplace par le motif replaceStr
266  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
267  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
268  }
269  }else{ //si le caractère i n'est pas le même caractère que nbMatch
270  if(nbMatch == 0lu){ //si on n'en avait pas trouver de bon avant
271  out += src[i]; //on ne change rien à ce caractère
272  }else{ //si on avais déjà tester des caractères avant
273  out += src[beginTest];
274  i = beginTest;
275  }
276  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
277  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
278  }
279  }
280  //We are potentially at the end of the source, so no more test
281  return out;
282 }
283 
285 
290 PString PString::replace(const PString & pattern, const PString & replaceStr, size_t maxNbReplace) const{
291  size_t sizePatern(pattern.size());
292  const PString & src = *this;
293  if(sizePatern == 0lu || src.size() == 0lu || maxNbReplace == 0lu) return *this;
294  PString out(""); //on évite les petits désagréments
295  size_t sizeSrc(src.size());
296  size_t beginTest(0lu), nbMatch(0lu), nbReplace(0lu);
297  for(size_t i(0lu); i < sizeSrc; ++i){
298  if(src[i] == pattern[nbMatch] && nbReplace < maxNbReplace){ //si le caractère i est le même que le caractère nbMatch
299  if(nbMatch == 0lu){ //c'est le premier qu'on teste
300  beginTest = i; //il faut donc se rappeler où on a commencer à faire le teste
301  }
302  ++nbMatch; //la prochaîne fois on testera le caractère suivant
303  if(nbMatch == sizePatern){ //dans ce cas, on a tout testé et tout les caractères correspondent, donc on sauvegarde
304  out += replaceStr; //on a trouver le motif pattern, donc on le remplace par le motif replaceStr
305  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
306  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
307  ++nbReplace;
308  }
309  }else{ //si le caractère i n'est pas le même caractère que nbMatch
310  if(nbMatch == 0lu){ //si on n'en avait pas trouver de bon avant
311  out += src[i]; //on ne change rien à ce caractère
312  }else{ //si on avais déjà tester des caractères avant
313  out += src[beginTest];
314  i = beginTest;
315  }
316  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
317  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
318  }
319  }
320  //We are potentially at the end of the source, so no more test
321  return out;
322 }
323 
325 
329 PString PString::replaceChar(const PString & vecChar, const PString & replaceStr) const{
330  PString out;
331  for(PString::const_iterator it(begin()); it != end(); ++it){
332  if(vecChar.find(*it)){
333  out += replaceStr;
334  }else{
335  out += *it;
336  }
337  }
338  return out;
339 }
340 
342 
345 PString PString::format(const PString & arg) const{
346  return replace("{}", arg, 1lu);
347 }
348 
350 
353 bool PString::isSameBegining(const PString & beginStr) const{
354  const PString & src = *this;
355  if(src.size() < beginStr.size()) return false;
356  std::string::const_iterator it = src.begin();
357  std::string::const_iterator it2 = beginStr.begin();
358  while(it != src.end() && it2 != beginStr.end()){
359  if(*it != *it2){ return false;}
360  it++;
361  it2++;
362  }
363  return true;
364 }
365 
367 
370 size_t PString::count(char ch) const{
371  const PString & str(*this);
372  size_t nbChar(0lu);
373  std::string::const_iterator it(str.begin());
374  while(it != str.end()){
375  if(*it == ch) nbChar++;
376  it++;
377  }
378  return nbChar;
379 }
380 
382 
385 size_t PString::count(const PString & patern) const{
386  const PString & src = *this;
387  long unsigned int sizePatern(patern.size()), sizeSrc(src.size());
388  if(sizePatern == 0lu || sizeSrc == 0lu){return 0lu;}
389  size_t nbPaternFound(0lu);
390 
391  long unsigned int beginTest(0lu), nbMatch(0lu);
392  for(long unsigned int i(0lu); i < sizeSrc; ++i){
393  if(src[i] == patern[nbMatch]){ //si le caractère i est le même que le caractère nbMatch
394  if(nbMatch == 0lu){ //c'est le premier qu'on teste
395  beginTest = i; //il faut donc se rappeler où on a commencer à faire le teste
396  }
397  ++nbMatch; //la prochaîne fois on testera le caractère suivant
398  if(nbMatch == sizePatern){ //dans ce cas, on a tout testé et tout les caractères correspondent, donc on sauvegarde
399  ++nbPaternFound;
400  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
401  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
402  }
403  }else{ //si le caractère i n'est pas le même caractère que nbMatch
404  if(nbMatch != 0lu){ //si on avais déjà tester des caractères avant
405  i = beginTest;
406  }
407  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
408  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
409  }
410  }
411  return nbPaternFound;
412 }
413 
415 
418 bool PString::find(char ch) const{
419  PString::const_iterator it = begin();
420  while(it != end()){
421  if(*it == ch) return true;
422  ++it;
423  }
424  return false;
425 }
426 
428 
431 bool PString::find(const PString & listChar) const{
432  if(size() == 0lu || listChar.size() == 0lu){return false;}
433  bool foundChar = false;
434  long unsigned int i(0lu), size(listChar.size());
435  while(!foundChar && i < size){
436  foundChar = find(listChar[i]);
437  ++i;
438  }
439  return foundChar;
440 }
441 
443 
446 PString PString::keepChar(const PString & listChar) const{
447  if(listChar.size() == 0lu){return "";}
448  PString out;
449  for(PString::const_iterator it = begin(); it != end(); ++it){
450  if(listChar.find(*it)){
451  out += *it;
452  }
453  }
454  return out;
455 }
456 
458 
462  PString out("");
463  const PString & str1(*this);
464  PString::const_iterator it = str1.begin();
465  PString::const_iterator it2 = other.begin();
466  while(it != str1.end() && it2 != other.end()){
467  if(*it == *it2){
468  out += *it;
469  }else{
470  break;
471  }
472  it++;
473  it2++;
474  }
475  return out;
476 }
477 
479 
482 std::vector<PString> PString::split(char separator) const{
483  std::vector<PString> vec;
484  PString buffer = "";
485  for(PString::const_iterator it = begin(); it != end(); ++it){
486  if(*it != separator){
487  buffer += *it;
488  }else{
489  vec.push_back(buffer);
490  buffer = "";
491  }
492  }
493  if(buffer != ""){vec.push_back(buffer);}
494  return vec;
495 }
496 
498 
501 std::vector<PString> PString::split(const PString & vecSeparator) const{
502  std::vector<PString> vec;
503  if(size() != 0lu && vecSeparator.size() != 0lu){
504  PString buffer("");
505  for(PString::const_iterator it(begin()); it != end(); ++it){
506  if(!vecSeparator.find(*it)){
507  buffer += *it;
508  }else{
509  if(buffer != ""){
510  vec.push_back(buffer);
511  buffer = "";
512  }
513  }
514  }
515  if(buffer != "") vec.push_back(buffer);
516  }
517  return vec;
518 }
519 
520 
522 
526 PString & PString::merge(const std::vector<PString> & vecStr, const PString & separator){
527  PString out(""), comma("");;
528  for(std::vector<PString>::const_iterator it(vecStr.begin()); it != vecStr.end(); ++it){
529  out += comma + (*it);
530  comma = separator;
531  }
532  *this = out;
533  return *this;
534 }
535 
537 
541  PString buffer = "";
542  for(PString::const_iterator it = begin(); it != end(); it++){
543  if(*it != ch) buffer += *it;
544  }
545  return buffer;
546 }
547 
549 
552 PString PString::eraseChar(const PString & vecChar) const{
553  PString buffer(*this);
554  for(PString::const_iterator it = vecChar.begin(); it != vecChar.end(); it++){
555  buffer = buffer.eraseChar(*it);
556  }
557  return buffer;
558 }
559 
561 
564 PString PString::eraseFirstChar(const PString & vecChar) const{
565  PString buffer(*this);
566  bool continuer = true;
567  PString::iterator it = buffer.begin();
568  //Let's remove the first chars
569  while(it != buffer.end() && continuer){
570  if(vecChar.find(*it)){it = buffer.erase(it);}
571  else{
572  continuer = false;
573  it++;
574  }
575  }
576  return buffer;
577 }
578 
580 
583 PString PString::eraseLastChar(const PString & vecChar) const{
584  if(size() > 0lu){
585  size_t nbCharToRemove(0lu);
586  PString::const_reverse_iterator it(rbegin());
587  while(vecChar.find(*it)){
588  ++it;
589  ++nbCharToRemove;
590  }
591 
592  if(nbCharToRemove == 0lu){
593  return *this;
594  }else{
595  PString buffer(substr(0, size() - nbCharToRemove));
596  return buffer;
597  }
598  }else{
599  return *this;
600  }
601 }
602 
604 
608  PString buffer(eraseFirstChar(vecChar));
609  return buffer.eraseLastChar(vecChar);
610 }
611 
613 
615 bool PString::isUpperCase() const{
616  if(size() == 0lu){return false;}
617  const PString & str = *this;
618  bool isUpper(true);
619  size_t i(0lu);
620  while(i < str.size() && isUpper){
621  isUpper = phoenix_isCharUpperCase(str[i]);
622  ++i;
623  }
624  return isUpper;
625 }
626 
628 
630 bool PString::isLowerCase() const{
631  if(size() == 0lu){return false;}
632  const PString & str = *this;
633  bool isLower(true);
634  size_t i(0lu);
635  while(i < str.size() && isLower){
636  isLower = phoenix_isCharLowerCase(str[i]);
637  ++i;
638  }
639  return isLower;
640 }
641 
643 
645 bool PString::isNumber() const{
646  if(size() == 0lu){return false;}
647  const PString & str = *this;
648  bool isNumber(true);
649  size_t i(0lu);
650  while(i < str.size() && isNumber){
651  isNumber = phoenix_isCharNumber(str[i]);
652  ++i;
653  }
654  return isNumber;
655 }
656 
658 
661  if(size() == 0lu){return *this;}
662  const PString & str = *this;
663  std::string strOut("");
664  char currentChar;
665  long unsigned int size(str.size());
666  for(long unsigned int i(0lu); i < size; ++i){
667  currentChar = str[i];
668  if(phoenix_isCharUpperCase(currentChar)){
669  strOut += currentChar + (char)32;
670  }else{
671  strOut += currentChar;
672  }
673  }
674  return strOut;
675 }
676 
678 
681  if(size() == 0lu){return *this;}
682  const PString & str = *this;
683  std::string strOut("");
684  char currentChar;
685  long unsigned int size(str.size());
686  for(long unsigned int i(0lu); i < size; ++i){
687  currentChar = str[i];
688  if(phoenix_isCharUpperCase(currentChar)){
689  strOut += currentChar + (char)32;
690  }else{
691  if(currentChar == ' '){strOut += '_';}
692  else{strOut += currentChar;}
693  }
694  }
695  return strOut;
696 }
697 
699 
702  if(size() == 0lu){return *this;}
703  const PString & str = *this;
704  std::string strOut("");
705  char currentChar;
706  long unsigned int size(str.size());
707  for(long unsigned int i(0); i < size; ++i){
708  currentChar = str[i];
709  if(phoenix_isCharLowerCase(currentChar)){
710  strOut += currentChar - (char)32;
711  }else{
712  strOut += currentChar;
713  }
714  }
715  return strOut;
716 }
717 
719 
722  if(size() == 0lu){return *this;}
723  const PString & str = *this;
724  std::string strOut(str);
725  char currentChar = strOut[0lu];
726  if(phoenix_isCharUpperCase(currentChar)){
727  strOut[0lu] = currentChar + (char)32;
728  }
729  return strOut;
730 }
731 
733 
736  if(size() == 0lu){return *this;}
737  const PString & str = *this;
738  std::string strOut(str);
739  char currentChar = strOut[0lu];
740  if(phoenix_isCharLowerCase(currentChar)){
741  strOut[0lu] = currentChar - (char)32;
742  }
743  return strOut;
744 }
745 
747 
751 PString PString::escapeStr(const PString & strCharToEscape, const PString & escapeSeq) const{
752  if(size() == 0lu || strCharToEscape.size() == 0lu || escapeSeq.size() == 0lu){return *this;}
753  const PString & src = *this;
754  std::string out("");
755  for(size_t i(0lu); i < src.size(); ++i){
756  char ch = src[i];
757  if(strCharToEscape.find(ch)){
758  out += escapeSeq;
759  }
760  out += ch;
761  }
762  return out;
763 }
764 
766 
768 void PString::copyPString(const PString & other){
769  resize(other.size());
770  memcpy((char*)data(), other.data(), other.size());
771 }
772 
774 
776 void PString::copyPString(const std::string & other){
777  resize(other.size());
778  memcpy((char*)data(), other.data(), other.size());
779 }
780 
782 
785  std::string tmp(*this);
786  resize(tmp.size() + other.size());
787  memcpy((char*)data(), tmp.data(), tmp.size());
788  memcpy((char*)data() + tmp.size(), other.data(), other.size());
789 }
790 
792 
794 void PString::concatenatePString(const std::string & other){
795  std::string tmp(*this);
796  resize(tmp.size() + other.size());
797  memcpy((char*)data(), tmp.data(), tmp.size());
798  memcpy((char*)data() + tmp.size(), other.data(), other.size());
799 }
800 
803 
804 }
805 
806 
807 
808 
809 
bool phoenix_isCharNumber(char ch)
Tels if the character is a number or not.
Definition: PString.cpp:43
PString phoenix_charToString(const char *ch)
Convert a char pointer into a string (event if the char pointer is NULL)
Definition: PString.cpp:14
void eraseFirstLastChar(PVecString &vecOut, const PVecString &vecStr, const PString &vecChar)
Erase first and last characters of all PString in given vector.
Definition: PString.cpp:52
bool phoenix_isCharUpperCase(char ch)
Tels if the character is upper case letter.
Definition: PString.cpp:27
bool phoenix_isCharLowerCase(char ch)
Tels if the character is lower case letter.
Definition: PString.cpp:35
PString operator+(const PString &other1, const PString &other2)
Concatenate 2 PString together.
Definition: PString.cpp:193
std::vector< PString > PVecString
Definition: PString.h:99
Extends the std::string.
Definition: PString.h:16
PString & operator=(const PString &other)
Definition of equal operator of PString.
Definition: PString.cpp:112
PString & add(const PString &other)
Add a PString to an other.
Definition: PString.cpp:154
bool isLowerCase() const
Say if the given PString is in lowercase.
Definition: PString.cpp:630
PString toLowerUnderscore() const
Convert std::string in lower case and space in '_'.
Definition: PString.cpp:680
PString getCommonBegining(const PString &other) const
Get the common begining between the current PString and other.
Definition: PString.cpp:461
size_t findPatternIndex(const PString &pattern, size_t offset=0lu) const
Get the index of the first character of the given pattern.
Definition: PString.cpp:204
virtual ~PString()
Destructeur of PString.
Definition: PString.cpp:104
PString replace(const PString &pattern, const PString &replaceStr) const
Replace a PString into an other PString.
Definition: PString.cpp:251
PString eraseChar(char ch) const
Erase char ch of current string.
Definition: PString.cpp:540
PString keepChar(const PString &listChar) const
Keep only the characters in the given listChar.
Definition: PString.cpp:446
PString format(const PString &arg) const
Replace first {} with arg.
Definition: PString.cpp:345
std::vector< PString > split(char separator) const
Cut a PString on the given separator char.
Definition: PString.cpp:482
PString toLower() const
Convert PString in lower case.
Definition: PString.cpp:660
PString toUpper() const
Convert std::string in upper case.
Definition: PString.cpp:701
PString replaceChar(const PString &vecChar, const PString &replaceStr) const
Replace characters in vecChar by replaceStr.
Definition: PString.cpp:329
bool find(char ch) const
Find a char in a string.
Definition: PString.cpp:418
PString firstToLower() const
Convert first letter of the PString in lower case.
Definition: PString.cpp:721
PString escapeStr(const PString &strCharToEscape, const PString &escapeSeq) const
Escape given string with passed characters.
Definition: PString.cpp:751
PString & operator+=(const PString &other)
Add a PString to an other.
Definition: PString.cpp:130
bool isNumber() const
Say if the given PString is composed of numbers.
Definition: PString.cpp:645
bool isSameBegining(const PString &beginStr) const
Say if the current PString has the same begining of beginStr.
Definition: PString.cpp:353
size_t count(char ch) const
Count the number of char ch in the current PString.
Definition: PString.cpp:370
PString & merge(const std::vector< PString > &vecStr, const PString &separator="")
Merge a set of PString.
Definition: PString.cpp:526
PString eraseFirstLastChar(const PString &vecChar) const
Erase first and last char in a string.
Definition: PString.cpp:607
void copyPString(const PString &other)
Copy function of PString.
Definition: PString.cpp:768
void concatenatePString(const PString &other)
Concatenate a PString into the current PString.
Definition: PString.cpp:784
PString eraseFirstChar(const PString &vecChar) const
Erase first char in a string.
Definition: PString.cpp:564
PString getBetweenDelimiter(const PString &beginPattern, const PString &endPattern) const
Get the PString between delimiter.
Definition: PString.cpp:236
PString()
Default constructor of PString.
Definition: PString.cpp:70
bool isUpperCase() const
Say if the given PString is in uppercase.
Definition: PString.cpp:615
void initialisationPString()
Initialisation function of the class PString.
Definition: PString.cpp:802
PString firstToUpper() const
Convert first letter of the PString in upper case.
Definition: PString.cpp:735
PString eraseLastChar(const PString &vecChar) const
Erase first and last char in a string.
Definition: PString.cpp:583