C++ main module for gpm Package  1.0
CORE_String.h
Go to the documentation of this file.
1 #ifndef CORE_String_H
2 #define CORE_String_H
3 
4 
5 #include "CORE_Object.h"
6 #include "CORE_Pointers.h"
7 
8 #include "CORE_Integer.h"
9 #include "CORE_Real.h"
10 #include "CORE_Complex.h"
11 
57 
58 
59 class CORE_String : public CORE_Object {
60 
62 
63  // ATTRIBUTES
64  private:
65  tString mString;
66  int mTokenIndex;
67  vector<tString> mSeparators;
68  vector<tString> mTokenizer;
69 
70  //ASSOCIATIONS
71 
72 private:
73 
74 
75 
76  //CONSTRUCTOR
77 public:
80  CORE_String();
83  CORE_String(const tString& str);
84  //DESTRUCTOR
87  ~CORE_String();
88 
89 protected:
90 
91 
92  // NEW METHODS
93 public:
96  static inline SP::CORE_String New() {
97  SP::CORE_String ret(new CORE_String(""),CORE_String::Delete());
98  //ret->mThis=WP::CORE_String(ret);
99  return ret;
100  };
103  static inline SP::CORE_String New(const tString& str) {
104  SP::CORE_String p(new CORE_String(str),CORE_String::Delete());
105  return p;
106  };
107 
108 
109  // SET METHODS
110 public:
115  const char& operator[](int i) const {
116  return mString[i];
117  };
118 
123  char& operator[](int i) {
124  return mString[i];
125  };
126 
129  inline void setString(const tString& str) {mString=str;};
132  inline void setString(const char* str) {
133  mString.clear();
134  mString.append(str);
135  };
136 
137 
138  // GET METHODS
139 public:
143  tString getString() const {return mString;};
144 
145  // OTHERS METHODS
146 public:
150  tString toString() const {return mString;};
151 
152 
153 
154 
157  char * toCharArray() const {
158  return stringToCharArray(mString);
159  };
164  static char * stringToCharArray(const tString& str);
165 
168  inline void tokenize(const tString& separator) {
169  int n=separator.length();
170  mSeparators.resize(n);
171  for (int i=0;i<n;i++) {
172  mSeparators[i]=separator.substr(i,1);
173  }
174  tokenize();
175  }
178  void tokenize();
179 
182  inline int getTokensCount() const {
183  return mTokenizer.size();
184  }
187  inline int getTokensNumber() const {
188  return mTokenizer.size();
189  }
190 
191  inline void begin() {
192  mTokenIndex=0;
193  }
194 
197  inline tBoolean hasNextToken() const {
198  return (mTokenIndex<((int)mTokenizer.size()));
199  }
200 
203  inline tString nextToken() {
204  return mTokenizer[mTokenIndex++];
205  };
206 
209  inline tString getToken(const int& index) {
210  return mTokenizer[index];
211  };
212 
215  inline void replaceAll(const tString& strToReplace,
216  const tString& str) {
217  replaceAll(mString,strToReplace,str);
218  };
221  inline void replaceFirst(const tString& strToReplace,
222  const tString& str) {
223  replaceFirst(mString,strToReplace,str);
224  };
227  inline void replaceLast(const tString& strToReplace,
228  const tString& str) {
229  replaceLast(mString,strToReplace,str);
230  };
233  static void replaceAll(tString& inOutValue,
234  const tString& strToReplace,
235  const tString& str);
238  static void replaceFirst(tString& inOutValue,
239  const tString& strToReplace,
240  const tString& str);
243  static void replaceLast(tString& inOutValue,
244  const tString& strToReplace,
245  const tString& str);
246 
247 
250  static int getOccurencesNumber(const tString& str,
251  const tString& occ);
252 
253 
254 
258  static tString toString(const tString& str,const int& len) {
259  tString ret(str);
260  int n=str.size();
261  for (int i=n;i<len;i++) ret+=" ";
262  return ret;
263  };
264 
265 
268  inline static void toUpper(tString& s) {
269  std::transform(s.begin(), s.end(),
270  s.begin(), ::toupper);
271  };
272 
273 
276  inline void toUpper() {
277  toUpper(mString);
278  };
281  static inline tString toUpper(const tString& str) {
282  tString upper(str);
283  toUpper(upper);
284  return upper;
285  };
286 
289  static inline void toLower(tString& s) {
290  std::transform(s.begin(), s.end(),
291  s.begin(), ::tolower);
292  };
295  inline void toLower() {
296  toLower(mString);
297  };
300  static inline tString toLower(const tString& str) {
301  tString lower(str);
302  toLower(lower);
303  return lower;
304  };
305 
306 
307 
310  inline static tString toString(const tFlag& c) {
311  return CORE_Integer::toString(c);
312  };
315  inline static tString toString(const float& c) {
316  return CORE_Real::toString(c,6);
317  };
320  inline static tString toString(const double& c) {
321  return CORE_Real::toString(c,6);
322  };
325  inline static tString toString(const long double& c) {
326  return CORE_Real::toString(c,6);
327  };
330  inline static tString toString(const int& c) {
331  return CORE_Integer::toString(c);
332  };
335  inline static tString toString(const long& c) {
336  return CORE_Integer::toString(c);
337  };
340  inline static tString toString(const long long& c) {
341  return CORE_Integer::toString(c);
342  };
345  inline static tString toString(const unsigned int& c) {
346  return CORE_Integer::toString(c);
347  };
350  inline static tString toString(const unsigned long& c) {
351  return CORE_Integer::toString(c);
352  };
355  inline static tString toString(const unsigned long long& c) {
356  return CORE_Integer::toString(c);
357  };
360  inline static tString toString(const tBoolean& c) {
361  return toString((unsigned char) c);
362  };
365  inline static tString boolean2String(const tBoolean& c) {
366  if (c) return "true";
367  else return "false";
368  };
371  inline static tString booleanToString(const tBoolean& c) {
372  return boolean2String(c);
373  };
376  inline static tBoolean string2Boolean(const tString& c) {
377  if (c.compare("true")==0) return true;
378  else if (c.compare("1")==0) return true;
379  else return false;
380  };
383  inline static tBoolean stringToBoolean(const tString& c) {
384  return string2Boolean(c);
385  };
388  inline static tString toString(tCharacter c) {
389 
390  char *cs=new char[2];
391  cs[0]=c;
392  cs[1]='\0';
393  tString ret(cs);
394  delete[] cs;
395  return ret;
396  };
399  inline static tString toString(tComplex c) {
400  return CORE_Complex::toString(c);
401  };
404  inline static tString toString(tString c) {
405  return c;
406  };
409  inline static tString toString(const int &n, const long double* c) {
410  tString ret="{";
411  for (int i=0;i<n-1;i++) ret+=CORE_Real::toString(c[i],10)+",";
412  if (n>0)ret+=CORE_Real::toString(c[n-1],10);
413  ret+="}";
414  return ret;
415  };
418  inline static void parse(const tString& str,unsigned char& c) {
419  c=(unsigned char) CORE_Integer::parseInt(str);
420  };
423  inline static void parse(const tString& str,short int& c) {
424  c=(short) CORE_Integer::parseInt(str);
425  };
428  inline static void parse(const tString& str,unsigned short int& c) {
429  c=(short) CORE_Integer::parseInt(str);
430  };
433  inline static void parse(const tString& str,int& c) {
434  c=(int) CORE_Integer::parseInt(str);
435  };
438  inline static void parse(const tString& str,long& c) {
439  c=(long) CORE_Integer::parseInt(str);
440  };
443  inline static void parse(const tString& str,long long& c) {
444  c=(long long) CORE_Integer::parseInt(str);
445  };
448  inline static void parse(const tString& str,unsigned int& c) {
449  c=(unsigned int) CORE_Integer::parseInt(str);
450  };
453  inline static void parse(const tString& str,unsigned long& c) {
454  c=(unsigned long) CORE_Integer::parseInt(str);
455  };
458  inline static void parse(const tString& str,unsigned long long& c) {
459  c=(unsigned long long) CORE_Integer::parseInt(str);
460  };
463  inline static void parse(const tString& str,bool& c) {
464  if (str.compare("true")==0) c=true;
465  else if (str.compare("false")==0) c=false;
466  else c=(bool) CORE_Integer::parseInt(str);
467  };
470  inline static void parse(const tString& str,char& c) {
471  c=str[0];
472  };
475  inline static void parse(const tString& str,float& c) {
476  c=(float) CORE_Real::parseReal(str);
477  };
480  inline static void parse(const tString& str,double& c) {
481  c=(double) CORE_Real::parseReal(str);
482  };
485  inline static void parse(const tString& str,long double& c) {
486  c=(long double) CORE_Real::parseReal(str);
487  };
490  inline static void parse(const tString& str,tString& c) {
491  c=tString(str);
492  };
495  inline static void parse(const tString& str,tComplex& c) {
497  };
498 
501  inline tString::size_type indexOf(const tString& v) const {
502  return mString.find(v);
503 
504  };
507  inline tString::size_type lastIndexOf(const tString& v) const {
508  return mString.rfind(v);
509 
510  };
513  inline tString::size_type getLastIndexOf(const tString& v) const {
514  return mString.rfind(v);
515 
516  };
519  static inline tString::size_type getLastIndexOf(const tString& str,const tString& v) {
520  return str.rfind(v);
521 
522  };
525  inline tString::size_type indexOf(const tString& v,
526  const tString::size_type& fromIndex) const {
527  return mString.find(v,fromIndex);
528 
529  };
530 
536  static tString keepOnlyFirstLines(const tString& str,
537  const int& nLines,tBoolean& isTruncated);
540  inline void remove(const tString::size_type& from,
541  const tString::size_type& to) {
542  mString.erase(from,to-from+1);
543  };
546  inline void remove(const tString::size_type& index) {
547  mString=mString.erase(index,1);
548  };
551  static void removeAll(tString& inOutStr,const tString& strToRemove);
552 
555  static void truncate(tString& text,const int &nLines,const int& nCharsByLine);
556 
560  inline static tString substring(const tString& str,
561  const tString::size_type& from,
562  const tString::size_type& to) {
563  return str.substr(from,to-from);
564  };
568  inline tString substring(const tString::size_type& from,
569  const tString::size_type& to) const {
570  return mString.substr(from,to-from);
571  };
574  inline tString substring(const tString::size_type& from) const {
575  return mString.substr(from);
576  };
577 
578 
581  inline char charAt(const int& index) const {
582  return mString[index];
583  };
586  inline int length() const {
587  return mString.length();
588  };
591  inline void append(const tString& v) {
592  mString+=v;
593  }
596  inline void append(const char& v) {
597  mString+=toString(v);
598  }
601  static void trim(tString& str);
602 
605  inline void trim() {
606  trim(mString);
607  }
608 
611  static int readInteger(const tString& v);
615  static tReal readReal(const tString& v);
616 
619  inline static tBoolean isLetter(const char& v) {
620  return ( ((v>='a') && (v<='z')) ||
621  ((v>='A') && (v<='Z'))
622  );
623  }
626  inline static tBoolean isDigit(const char& v) {
627  return ((v>='0') && (v<='9'));
628 
629  }
630 
631  // VIRTUAl METHODS
632 public:
633 
634  // PRIVATE METHODS
635 private:
636 
637 
638 };
639 
640 
641 #endif // end of ifndef
this class describes a string
Definition: CORE_String.h:59
static tString toString(const unsigned long &c)
return the string representation of c
Definition: CORE_String.h:350
void begin()
Definition: CORE_String.h:191
static int getOccurencesNumber(const tString &str, const tString &occ)
get the number of occurence of the string
Definition: CORE_String.cpp:129
tBoolean hasNextToken() const
return true if there is another token
Definition: CORE_String.h:197
static tString boolean2String(const tBoolean &c)
return the string representation true or false of boolean c
Definition: CORE_String.h:365
static void parse(const tString &str, float &c)
parse float c in str
Definition: CORE_String.h:475
void replaceAll(const tString &strToReplace, const tString &str)
replace all instances of strToReplace by str
Definition: CORE_String.h:215
tString::size_type lastIndexOf(const tString &v) const
return the last index of v fin this->mString
Definition: CORE_String.h:507
static tString toString(tComplex c)
return the string representation of complex c
Definition: CORE_String.h:399
static tComplex parseComplex(tString str)
return the complex associated to the string
Definition: CORE_Complex.cpp:35
static tString toString(const tBoolean &c)
return the string representation of boolean c
Definition: CORE_String.h:360
static void parse(const tString &str, tString &c)
parse tString c in str
Definition: CORE_String.h:490
tString toString() const
return the string associated to the string
Definition: CORE_String.h:150
static tString toLower(const tString &str)
turn the string to upper case
Definition: CORE_String.h:300
static tString toString(tCharacter c)
return the string representation of char c
Definition: CORE_String.h:388
static tString toUpper(const tString &str)
turn the string to upper case
Definition: CORE_String.h:281
static long int parseInt(tString str)
return the integer associated to the string
Definition: CORE_Integer.cpp:120
static void truncate(tString &text, const int &nLines, const int &nCharsByLine)
truncate the message with nLines of nChars
Definition: CORE_String.cpp:243
#define tCharacter
Definition: types.h:40
static tString toString(const int &c)
return the string representation of int c
Definition: CORE_String.h:330
static void parse(const tString &str, unsigned long long &c)
parse unsigned long long c in str
Definition: CORE_String.h:458
static tBoolean string2Boolean(const tString &c)
return the booleazn corresponding to string
Definition: CORE_String.h:376
int length() const
get the size of the size
Definition: CORE_String.h:586
#define tComplex
Definition: types.h:37
DEFINE_SPTR(CORE_String)
static void parse(const tString &str, unsigned short int &c)
parse short c in str
Definition: CORE_String.h:428
void toLower()
turn the string to lower case
Definition: CORE_String.h:295
void trim()
trim
Definition: CORE_String.h:605
void replaceFirst(const tString &strToReplace, const tString &str)
replace first instance of strToReplace by str
Definition: CORE_String.h:221
static tString toString(const float &c)
return the string representation of float c
Definition: CORE_String.h:315
#define tBoolean
Definition: types.h:35
static tString toString(const long long &c)
return the string representation of long long c
Definition: CORE_String.h:340
static tString keepOnlyFirstLines(const tString &str, const int &nLines, tBoolean &isTruncated)
keep only the first lines of parameter str
Definition: CORE_String.cpp:223
static void parse(const tString &str, char &c)
parse char c in str
Definition: CORE_String.h:470
CORE_String()
create a string
Definition: CORE_String.cpp:12
static tString toString(const tFlag &c)
return the string representation of short c
Definition: CORE_String.h:310
char * toCharArray() const
turn the string into char array
Definition: CORE_String.h:157
tString getToken(const int &index)
get the token at index
Definition: CORE_String.h:209
void toUpper()
turn the string to upper case
Definition: CORE_String.h:276
static tString substring(const tString &str, const tString::size_type &from, const tString::size_type &to)
return the string between from & to indices from index is included, to is not
Definition: CORE_String.h:560
static void parse(const tString &str, unsigned char &c)
parse unsigned char c in str
Definition: CORE_String.h:418
~CORE_String()
deleter
Definition: CORE_String.cpp:19
tString nextToken()
return the next token
Definition: CORE_String.h:203
static void parse(const tString &str, long long &c)
parse long long c in str
Definition: CORE_String.h:443
void replaceLast(const tString &strToReplace, const tString &str)
replace last instance of strToReplace by str
Definition: CORE_String.h:227
static void parse(const tString &str, double &c)
parse double c in str
Definition: CORE_String.h:480
static tString toString(const unsigned long long &c)
return the string representation of unsigned long long c
Definition: CORE_String.h:355
static void parse(const tString &str, bool &c)
parse boolean c in str
Definition: CORE_String.h:463
static void parse(const tString &str, tComplex &c)
parse tComplex c in str
Definition: CORE_String.h:495
const char & operator[](int i) const
get the i-th element Assert in (i>-1) Assert in (i
Definition: CORE_String.h:115
int getTokensCount() const
get the number of tokens
Definition: CORE_String.h:182
#define SP_OBJECT(X)
Definition: CORE_Pointers.h:176
static SP::CORE_String New(const tString &str)
build an integer object
Definition: CORE_String.h:103
static tString toString(const long &c)
return the string representation of long c
Definition: CORE_String.h:335
tString::size_type indexOf(const tString &v) const
return the first index of v fin this->mString
Definition: CORE_String.h:501
static void parse(const tString &str, short int &c)
parse short c in str
Definition: CORE_String.h:423
static tString toString(const int &n, const long double *c)
return the string representation of long double c
Definition: CORE_String.h:409
void tokenize(const tString &separator)
tokenize the string with the separator
Definition: CORE_String.h:168
static tReal readReal(const tString &v)
read only real characters if not a real return 0
Definition: CORE_String.cpp:178
char & operator[](int i)
get the i-th element Assert in (i>-1) Assert in (i
Definition: CORE_String.h:123
static void toUpper(tString &s)
to upper
Definition: CORE_String.h:268
static int readInteger(const tString &v)
read only integer characters
Definition: CORE_String.cpp:144
void append(const tString &v)
append the string v to the string
Definition: CORE_String.h:591
static tString booleanToString(const tBoolean &c)
return the string representation true or false of boolean c
Definition: CORE_String.h:371
static void parse(const tString &str, unsigned int &c)
parse unsigned int c in str
Definition: CORE_String.h:448
tString toString() const
return the string associated to the integer
Definition: CORE_Integer.h:142
void setString(const char *str)
set the integer to i
Definition: CORE_String.h:132
void setString(const tString &str)
set the integer to i
Definition: CORE_String.h:129
abstract base class for most classes.
Definition: CORE_Object.h:30
static void parse(const tString &str, int &c)
parse int c in str
Definition: CORE_String.h:433
static tString toString(const double &c)
return the string representation of double c
Definition: CORE_String.h:320
#define tString
Definition: types.h:36
tString toString() const
return the string associated to the real
Definition: CORE_Real.h:89
void append(const char &v)
append the char v to the string
Definition: CORE_String.h:596
static long double parseReal(const tString &str)
return the real associated to the string
Definition: CORE_Real.cpp:78
static void parse(const tString &str, long double &c)
parse long double c in str
Definition: CORE_String.h:485
tString substring(const tString::size_type &from) const
return the string from index
Definition: CORE_String.h:574
static tBoolean stringToBoolean(const tString &c)
return the booleazn corresponding to string
Definition: CORE_String.h:383
static tString toString(const unsigned int &c)
return the string representation of unsigned int c
Definition: CORE_String.h:345
static void parse(const tString &str, long &c)
parse long c in str
Definition: CORE_String.h:438
tString substring(const tString::size_type &from, const tString::size_type &to) const
return the string between from & to indices from index is included, to is not
Definition: CORE_String.h:568
static SP::CORE_String New()
create a class String
Definition: CORE_String.h:96
tString::size_type indexOf(const tString &v, const tString::size_type &fromIndex) const
return the index of char v in this->mString from fromIndex index
Definition: CORE_String.h:525
tString getString() const
get the string
Definition: CORE_String.h:143
static void toLower(tString &s)
to lower
Definition: CORE_String.h:289
tString toString() const
return the string associated to the real
Definition: CORE_Complex.h:91
static tString toString(const long double &c)
return the string representation of long double c
Definition: CORE_String.h:325
static tString toString(tString c)
return the string representation of string c
Definition: CORE_String.h:404
static tString toString(const tString &str, const int &len)
return the string associated to the string
Definition: CORE_String.h:258
static tString::size_type getLastIndexOf(const tString &str, const tString &v)
return the last index of v fin this->mString
Definition: CORE_String.h:519
static char * stringToCharArray(const tString &str)
turn the string into char array
Definition: CORE_String.cpp:23
static void removeAll(tString &inOutStr, const tString &strToRemove)
remove the char at index
Definition: CORE_String.cpp:86
static tBoolean isDigit(const char &v)
return if the char is a digit
Definition: CORE_String.h:626
static void parse(const tString &str, unsigned long &c)
parse unsigned long c in str
Definition: CORE_String.h:453
#define tReal
Definition: types.h:18
void tokenize()
tokenize the string
Definition: CORE_String.cpp:31
int getTokensNumber() const
get the number of tokens
Definition: CORE_String.h:187
char charAt(const int &index) const
return the char at index
Definition: CORE_String.h:581
tString::size_type getLastIndexOf(const tString &v) const
return the last index of v fin this->mString
Definition: CORE_String.h:513
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:106
#define tFlag
Definition: types.h:14
static tBoolean isLetter(const char &v)
return if the char is a letter
Definition: CORE_String.h:619