PhoenixInkscape  2.0.0
Generate multiple png files with svg inkscape files
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 PString PString::replace(const PString & pattern, const PString & replaceStr) const{
205  size_t sizePatern(pattern.size());
206  const PString & src = *this;
207  if(sizePatern == 0lu || src.size() == 0lu) return *this;
208  PString out(""); //on évite les petits désagréments
209  size_t sizeSrc(src.size());
210  size_t beginTest(0lu), nbMatch(0lu);
211  for(size_t i(0lu); 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  out += replaceStr; //on a trouver le motif pattern, donc on le remplace par le motif replaceStr
219  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
220  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
221  }
222  }else{ //si le caractère i n'est pas le même caractère que nbMatch
223  if(nbMatch == 0lu){ //si on n'en avait pas trouver de bon avant
224  out += src[i]; //on ne change rien à ce caractère
225  }else{ //si on avais déjà tester des caractères avant
226  out += src[beginTest];
227  i = beginTest;
228  }
229  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
230  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
231  }
232  }
233  //We are potentially at the end of the source, so no more test
234  return out;
235 }
236 
238 
243 PString PString::replace(const PString & pattern, const PString & replaceStr, size_t maxNbReplace) const{
244  size_t sizePatern(pattern.size());
245  const PString & src = *this;
246  if(sizePatern == 0lu || src.size() == 0lu || maxNbReplace == 0lu) return *this;
247  PString out(""); //on évite les petits désagréments
248  size_t sizeSrc(src.size());
249  size_t beginTest(0lu), nbMatch(0lu), nbReplace(0lu);
250  for(size_t i(0lu); i < sizeSrc; ++i){
251  if(src[i] == pattern[nbMatch] && nbReplace < maxNbReplace){ //si le caractère i est le même que le caractère nbMatch
252  if(nbMatch == 0lu){ //c'est le premier qu'on teste
253  beginTest = i; //il faut donc se rappeler où on a commencer à faire le teste
254  }
255  ++nbMatch; //la prochaîne fois on testera le caractère suivant
256  if(nbMatch == sizePatern){ //dans ce cas, on a tout testé et tout les caractères correspondent, donc on sauvegarde
257  out += replaceStr; //on a trouver le motif pattern, donc on le remplace par le motif replaceStr
258  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
259  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
260  ++nbReplace;
261  }
262  }else{ //si le caractère i n'est pas le même caractère que nbMatch
263  if(nbMatch == 0lu){ //si on n'en avait pas trouver de bon avant
264  out += src[i]; //on ne change rien à ce caractère
265  }else{ //si on avais déjà tester des caractères avant
266  out += src[beginTest];
267  i = beginTest;
268  }
269  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
270  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
271  }
272  }
273  //We are potentially at the end of the source, so no more test
274  return out;
275 }
276 
278 
282 PString PString::replaceChar(const PString & vecChar, const PString & replaceStr) const{
283  PString out;
284  for(PString::const_iterator it(begin()); it != end(); ++it){
285  if(vecChar.find(*it)){
286  out += replaceStr;
287  }else{
288  out += *it;
289  }
290  }
291  return out;
292 }
293 
295 
298 PString PString::format(const PString & arg) const{
299  return replace("{}", arg, 1lu);
300 }
301 
303 
306 bool PString::isSameBegining(const PString & beginStr) const{
307  const PString & src = *this;
308  if(src.size() < beginStr.size()) return false;
309  std::string::const_iterator it = src.begin();
310  std::string::const_iterator it2 = beginStr.begin();
311  while(it != src.end() && it2 != beginStr.end()){
312  if(*it != *it2){ return false;}
313  it++;
314  it2++;
315  }
316  return true;
317 }
318 
320 
323 size_t PString::count(char ch) const{
324  const PString & str(*this);
325  size_t nbChar(0lu);
326  std::string::const_iterator it(str.begin());
327  while(it != str.end()){
328  if(*it == ch) nbChar++;
329  it++;
330  }
331  return nbChar;
332 }
333 
335 
338 size_t PString::count(const PString & patern) const{
339  const PString & src = *this;
340  long unsigned int sizePatern(patern.size()), sizeSrc(src.size());
341  if(sizePatern == 0lu || sizeSrc == 0lu){return 0lu;}
342  size_t nbPaternFound(0lu);
343 
344  long unsigned int beginTest(0lu), nbMatch(0lu);
345  for(long unsigned int i(0lu); i < sizeSrc; ++i){
346  if(src[i] == patern[nbMatch]){ //si le caractère i est le même que le caractère nbMatch
347  if(nbMatch == 0lu){ //c'est le premier qu'on teste
348  beginTest = i; //il faut donc se rappeler où on a commencer à faire le teste
349  }
350  ++nbMatch; //la prochaîne fois on testera le caractère suivant
351  if(nbMatch == sizePatern){ //dans ce cas, on a tout testé et tout les caractères correspondent, donc on sauvegarde
352  ++nbPaternFound;
353  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
354  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
355  }
356  }else{ //si le caractère i n'est pas le même caractère que nbMatch
357  if(nbMatch != 0lu){ //si on avais déjà tester des caractères avant
358  i = beginTest;
359  }
360  beginTest = 0lu; //on remet le début des tests à 0 (pour évité les dépassements, on ne sait jamais)
361  nbMatch = 0lu; //on remet ne nombre des tests à 0, comme on n'a pas trouver de nouveau le motif
362  }
363  }
364  return nbPaternFound;
365 }
366 
368 
371 bool PString::find(char ch) const{
372  PString::const_iterator it = begin();
373  while(it != end()){
374  if(*it == ch) return true;
375  ++it;
376  }
377  return false;
378 }
379 
381 
384 bool PString::find(const PString & listChar) const{
385  if(size() == 0lu || listChar.size() == 0lu){return false;}
386  bool foundChar = false;
387  long unsigned int i(0lu), size(listChar.size());
388  while(!foundChar && i < size){
389  foundChar = find(listChar[i]);
390  ++i;
391  }
392  return foundChar;
393 }
394 
396 
400  PString out("");
401  const PString & str1(*this);
402  PString::const_iterator it = str1.begin();
403  PString::const_iterator it2 = other.begin();
404  while(it != str1.end() && it2 != other.end()){
405  if(*it == *it2){
406  out += *it;
407  }else{
408  break;
409  }
410  it++;
411  it2++;
412  }
413  return out;
414 }
415 
417 
420 std::vector<PString> PString::split(char separator) const{
421  std::vector<PString> vec;
422  PString buffer = "";
423  for(PString::const_iterator it = begin(); it != end(); ++it){
424  if(*it != separator){
425  buffer += *it;
426  }else{
427  vec.push_back(buffer);
428  buffer = "";
429  }
430  }
431  if(buffer != ""){vec.push_back(buffer);}
432  return vec;
433 }
434 
436 
439 std::vector<PString> PString::split(const PString & vecSeparator) const{
440  std::vector<PString> vec;
441  if(size() != 0lu && vecSeparator.size() != 0lu){
442  PString buffer("");
443  for(PString::const_iterator it(begin()); it != end(); ++it){
444  if(!vecSeparator.find(*it)){
445  buffer += *it;
446  }else{
447  if(buffer != ""){
448  vec.push_back(buffer);
449  buffer = "";
450  }
451  }
452  }
453  if(buffer != "") vec.push_back(buffer);
454  }
455  return vec;
456 }
457 
458 
460 
464 PString & PString::merge(const std::vector<PString> & vecStr, const PString & separator){
465  PString out(""), comma("");;
466  for(std::vector<PString>::const_iterator it(vecStr.begin()); it != vecStr.end(); ++it){
467  out += comma + (*it);
468  comma = separator;
469  }
470  *this = out;
471  return *this;
472 }
473 
475 
479  PString buffer = "";
480  for(PString::const_iterator it = begin(); it != end(); it++){
481  if(*it != ch) buffer += *it;
482  }
483  return buffer;
484 }
485 
487 
490 PString PString::eraseChar(const PString & vecChar) const{
491  PString buffer(*this);
492  for(PString::const_iterator it = vecChar.begin(); it != vecChar.end(); it++){
493  buffer = buffer.eraseChar(*it);
494  }
495  return buffer;
496 }
497 
499 
502 PString PString::eraseFirstChar(const PString & vecChar) const{
503  PString buffer(*this);
504  bool continuer = true;
505  PString::iterator it = buffer.begin();
506  //Let's remove the first chars
507  while(it != buffer.end() && continuer){
508  if(vecChar.find(*it)){it = buffer.erase(it);}
509  else{
510  continuer = false;
511  it++;
512  }
513  }
514  return buffer;
515 }
516 
518 
521 PString PString::eraseLastChar(const PString & vecChar) const{
522  if(size() > 0lu){
523  size_t nbCharToRemove(0lu);
524  PString::const_reverse_iterator it(rbegin());
525  while(vecChar.find(*it)){
526  ++it;
527  ++nbCharToRemove;
528  }
529 
530  if(nbCharToRemove == 0lu){
531  return *this;
532  }else{
533  PString buffer(substr(0, size() - nbCharToRemove));
534  return buffer;
535  }
536  }else{
537  return *this;
538  }
539 }
540 
542 
546  PString buffer(eraseFirstChar(vecChar));
547  return buffer.eraseLastChar(vecChar);
548 }
549 
551 
553 bool PString::isUpperCase() const{
554  if(size() == 0lu){return false;}
555  const PString & str = *this;
556  bool isUpper(true);
557  size_t i(0lu);
558  while(i < str.size() && isUpper){
559  isUpper = phoenix_isCharUpperCase(str[i]);
560  ++i;
561  }
562  return isUpper;
563 }
564 
566 
568 bool PString::isLowerCase() const{
569  if(size() == 0lu){return false;}
570  const PString & str = *this;
571  bool isLower(true);
572  size_t i(0lu);
573  while(i < str.size() && isLower){
574  isLower = phoenix_isCharLowerCase(str[i]);
575  ++i;
576  }
577  return isLower;
578 }
579 
581 
583 bool PString::isNumber() const{
584  if(size() == 0lu){return false;}
585  const PString & str = *this;
586  bool isNumber(true);
587  size_t i(0lu);
588  while(i < str.size() && isNumber){
589  isNumber = phoenix_isCharNumber(str[i]);
590  ++i;
591  }
592  return isNumber;
593 }
594 
596 
599  if(size() == 0lu){return *this;}
600  const PString & str = *this;
601  std::string strOut("");
602  char currentChar;
603  long unsigned int size(str.size());
604  for(long unsigned int i(0lu); i < size; ++i){
605  currentChar = str[i];
606  if(phoenix_isCharUpperCase(currentChar)){
607  strOut += currentChar + (char)32;
608  }else{
609  strOut += currentChar;
610  }
611  }
612  return strOut;
613 }
614 
616 
619  if(size() == 0lu){return *this;}
620  const PString & str = *this;
621  std::string strOut("");
622  char currentChar;
623  long unsigned int size(str.size());
624  for(long unsigned int i(0lu); i < size; ++i){
625  currentChar = str[i];
626  if(phoenix_isCharUpperCase(currentChar)){
627  strOut += currentChar + (char)32;
628  }else{
629  if(currentChar == ' '){strOut += '_';}
630  else{strOut += currentChar;}
631  }
632  }
633  return strOut;
634 }
635 
637 
640  if(size() == 0lu){return *this;}
641  const PString & str = *this;
642  std::string strOut("");
643  char currentChar;
644  long unsigned int size(str.size());
645  for(long unsigned int i(0); i < size; ++i){
646  currentChar = str[i];
647  if(phoenix_isCharLowerCase(currentChar)){
648  strOut += currentChar - (char)32;
649  }else{
650  strOut += currentChar;
651  }
652  }
653  return strOut;
654 }
655 
657 
660  if(size() == 0lu){return *this;}
661  const PString & str = *this;
662  std::string strOut(str);
663  char currentChar = strOut[0lu];
664  if(phoenix_isCharUpperCase(currentChar)){
665  strOut[0lu] = currentChar + (char)32;
666  }
667  return strOut;
668 }
669 
671 
674  if(size() == 0lu){return *this;}
675  const PString & str = *this;
676  std::string strOut(str);
677  char currentChar = strOut[0lu];
678  if(phoenix_isCharLowerCase(currentChar)){
679  strOut[0lu] = currentChar - (char)32;
680  }
681  return strOut;
682 }
683 
685 
689 PString PString::escapeStr(const PString & strCharToEscape, const PString & escapeSeq) const{
690  if(size() == 0lu || strCharToEscape.size() == 0lu || escapeSeq.size() == 0lu){return *this;}
691  const PString & src = *this;
692  std::string out("");
693  for(size_t i(0lu); i < src.size(); ++i){
694  char ch = src[i];
695  if(strCharToEscape.find(ch)){
696  out += escapeSeq;
697  }
698  out += ch;
699  }
700  return out;
701 }
702 
704 
706 void PString::copyPString(const PString & other){
707  resize(other.size());
708  memcpy((char*)data(), other.data(), other.size());
709 }
710 
712 
714 void PString::copyPString(const std::string & other){
715  resize(other.size());
716  memcpy((char*)data(), other.data(), other.size());
717 }
718 
720 
723  std::string tmp(*this);
724  resize(tmp.size() + other.size());
725  memcpy((char*)data(), tmp.data(), tmp.size());
726  memcpy((char*)data() + tmp.size(), other.data(), other.size());
727 }
728 
730 
732 void PString::concatenatePString(const std::string & other){
733  std::string tmp(*this);
734  resize(tmp.size() + other.size());
735  memcpy((char*)data(), tmp.data(), tmp.size());
736  memcpy((char*)data() + tmp.size(), other.data(), other.size());
737 }
738 
741 
742 }
743 
744 
745 
746 
747 
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:96
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:568
PString toLowerUnderscore() const
Convert std::string in lower case and space in '_'.
Definition: PString.cpp:618
PString getCommonBegining(const PString &other) const
Get the common begining between the current PString and other.
Definition: PString.cpp:399
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:204
PString eraseChar(char ch) const
Erase char ch of current string.
Definition: PString.cpp:478
PString format(const PString &arg) const
Replace first {} with arg.
Definition: PString.cpp:298
std::vector< PString > split(char separator) const
Cut a PString on the given separator char.
Definition: PString.cpp:420
PString toLower() const
Convert PString in lower case.
Definition: PString.cpp:598
PString toUpper() const
Convert std::string in upper case.
Definition: PString.cpp:639
PString replaceChar(const PString &vecChar, const PString &replaceStr) const
Replace characters in vecChar by replaceStr.
Definition: PString.cpp:282
bool find(char ch) const
Find a char in a string.
Definition: PString.cpp:371
PString firstToLower() const
Convert first letter of the PString in lower case.
Definition: PString.cpp:659
PString escapeStr(const PString &strCharToEscape, const PString &escapeSeq) const
Escape given string with passed characters.
Definition: PString.cpp:689
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:583
bool isSameBegining(const PString &beginStr) const
Say if the current PString has the same begining of beginStr.
Definition: PString.cpp:306
size_t count(char ch) const
Count the number of char ch in the current PString.
Definition: PString.cpp:323
PString & merge(const std::vector< PString > &vecStr, const PString &separator="")
Merge a set of PString.
Definition: PString.cpp:464
PString eraseFirstLastChar(const PString &vecChar) const
Erase first and last char in a string.
Definition: PString.cpp:545
void copyPString(const PString &other)
Copy function of PString.
Definition: PString.cpp:706
void concatenatePString(const PString &other)
Concatenate a PString into the current PString.
Definition: PString.cpp:722
PString eraseFirstChar(const PString &vecChar) const
Erase first char in a string.
Definition: PString.cpp:502
PString()
Default constructor of PString.
Definition: PString.cpp:70
bool isUpperCase() const
Say if the given PString is in uppercase.
Definition: PString.cpp:553
void initialisationPString()
Initialisation function of the class PString.
Definition: PString.cpp:740
PString firstToUpper() const
Convert first letter of the PString in upper case.
Definition: PString.cpp:673
PString eraseLastChar(const PString &vecChar) const
Erase first and last char in a string.
Definition: PString.cpp:521