str.hpp
Go to the documentation of this file.
1 /*
2  * SocialLedge.com - Copyright (C) 2013
3  *
4  * This file is part of free software framework for embedded processors.
5  * You can use it and/or distribute it as long as this copyright header
6  * remains unmodified. The code is free for personal use and requires
7  * permission to use in a commercial product.
8  *
9  * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
10  * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
12  * I SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
13  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
14  *
15  * You can reach the author of this software at :
16  * p r e e t . w i k i @ g m a i l . c o m
17  */
18 
33 #ifndef STR_HPP__
34 #define STR_HPP__
35 
36 
37 
48 #define STR_ON_STACK(name, size) \
49  char __##name##buffer[size]; \
50  str name((__##name##buffer), sizeof(__##name##buffer))
51 
52 
53 
100 class str
101 {
102  public:
107  static int toInt(const char* pString);
108  static int toInt(str& s) { return str::toInt(s()); }
109  static float toFloat(const char* pString);
110  static float toFloat(str& s) { return str::toFloat(s()); }
120  str();
121  str(int capacity);
122  str(const char* pString);
123  str(char *buff, int size);
124  str(const str& s);
125  ~str();
126 
130  int getLen() const;
131  int getCapacity() const;
132  bool reserve(int n);
133  void clear();
134  void clearAll();
135 
136  void toLower();
137  void toUpper();
138 
143  int printf(const char* pFormat, ...);
144 
159  int scanf(const char* pFormat, ...);
160 
174  int tokenize(const char* delimators, int char_ptr_count, ...);
175 
179  bool insertAtBeg(const char* pString);
180  bool insertAtBeg(const str& s) { return insertAtBeg(s()); }
181  bool insertAtEnd(const char* pString);
182  bool insertAtEnd(const str& s) { return insertAtEnd(s()); }
183  bool insertAt(const int index, const char* pString);
184  bool insertAt(const int index, const str& s) { return insertAt(index, s()); }
192  void append(const char* pString);
193  void append(const str& s) { append(s()); }
194  void append(int x);
195  void append(float x);
196  void appendAsHex(unsigned int num);
197 
205  bool compareTo(const char* pString) const;
206  bool compareTo(const str& s) const { return compareTo(s.c_str()); }
207  bool compareToIgnoreCase(const char* pString) const;
208  bool compareToIgnoreCase(const str& s) const { return compareToIgnoreCase(s()); }
216  int firstIndexOfIgnoreCase(const char* pString) const;
217  int firstIndexOfIgnoreCase(const str& s) const { return firstIndexOfIgnoreCase(s()); }
218 
219  int firstIndexOf(const char* pString) const;
220  int firstIndexOf(const str& s) const { return firstIndexOf(s()); }
221  int lastIndexOf(const char* pString) const;
222  int lastIndexOf(const str& s) const { return lastIndexOf(s()); }
230  bool contains(const char* pString) const;
231  bool contains(const str& s) const { return contains(s()); }
232  bool containsIgnoreCase(const char* pString) const;
233  bool containsIgnoreCase(const str& s) const { return containsIgnoreCase(s()); }
234 
235  int countOf(const char* pString) const;
236  int countOf(const str& s) const { return countOf(s()); }
237 
238  bool beginsWith(const char* pString) const;
239  bool beginsWith(const str& s) const { return endsWith(s()); }
240  bool beginsWithIgnoreCase(const char* pString) const;
241  bool beginsWithIgnoreCase(const str& s) const { return beginsWithIgnoreCase(s()); }
242 
249  bool beginsWithWholeWord(const char* pString, char seperator=' ') const;
250  bool beginsWithWholeWordIgnoreCase(const char* pString, char seperator=' ') const;
251 
252  bool endsWith(const char* pString) const;
253  bool endsWith(const str& s) const { return endsWith(s()); }
254  bool endsWithIgnoreCase(const char* pString) const;
255  bool endsWithIgnoreCase(const str& s) const { return endsWithIgnoreCase(s()); }
263  bool erase(const char* pString);
264  bool erase(const str& s) { return erase(s()); }
265  bool eraseFirst(int nChars);
266  bool eraseLast(int nChars);
267  bool eraseCharAt(int index);
268  bool eraseAllAfter(int index);
269  bool eraseAfter(int index, int nChars);
270  bool eraseFirstWords(int words, char separator = ' ');
271  int eraseAllSpecialChars();
272 
281  void trimStart(const char* pChars);
282  void trimStart(const str& s) { trimStart(s()); }
283  void trimEnd(const char* pChars);
284  void trimEnd(const str& s) { trimEnd(s()); }
292  bool replaceFirst(const char* pFind, const char* pWith);
293  bool replaceLast(const char* pFind, const char* pWith);
294  int replaceAll(const char* pFind, const char* pWith);
305  const str& subString(int fromIndex);
306  const str& subString(int fromIndex, int charCount);
307  const str& subString(char fromFirstChar);
308  const str& subString(const char* fromStr);
309  const str& subString(char fromFirstChar, int charCount);
310  const str& subString(const char* fromStr, int charCount);
311  const str& subString(char fromFirstChar, char toLastChar);
332  const str* getToken(const char* pSplitter = " ", bool restart=false);
333 
334 
335 
339  bool isAllAlpha() const;
340  bool isAlphaNumeric() const;
341  bool isFloat() const;
342  bool isUint() const;
343  bool isInt() const;
344 
350  unsigned int checksum_Get();
351  void checksum_Append();
352  void checksum_Remove();
353  bool checksum_Verify();
354 
360  void operator=(const char* pString);
361  void operator=(int num);
362  void operator=(float num);
363  str& operator=(const str& rhs);
364 
370  void operator++();
371  void operator++(int unused);
372  void operator--();
373  void operator--(int unused);
374 
380  void operator+=(const char singleChar);
381  void operator+=(const char* pString);
382  void operator+=(const str& s);
383  void operator+=(int n);
384  void operator-=(const char* pString);
385  void operator-=(const str& s);
386  void operator-=(int n);
387  void operator+=(float n);
388  void operator-=(float n);
389 
395  bool operator==(const char* pString) const;
396  bool operator==(const str& rhs) const;
397  bool operator==(int) const;
398  bool operator!=(const char* pString) const;
399  bool operator!=(const str& rhs) const;
400  bool operator!=(int) const;
401  // Note: Floats cannot be compared using == operator, they require delta comparison
412  bool operator<(const char* pString) const;
413  bool operator<(const str& s) const { return (*this) < s(); }
414  bool operator<(int) const;
415 
416  bool operator>(const char* pString) const;
417  bool operator>(int) const;
418  bool operator>(const str& s) const { return (*this) > s(); }
419 
420  bool operator<=(const char* pString) const;
421  bool operator<=(int) const;
422  bool operator<=(const str& s) const { return (*this) <= s(); }
423 
424  bool operator>=(const char* pString) const;
425  bool operator>=(int) const;
426  bool operator>=(const str& s) const { return (*this) >= s(); }
427  bool operator<(float) const;
428  bool operator>=(float) const;
429  bool operator>(float) const;
430  bool operator<=(float) const;
434  const char* operator()() const;
435  const char* c_str() const { return (*this)(); };
436  operator float() const;
437  operator int() const;
438  char& operator[](int pos);
439 
440 
441  private:
442  bool mStackMem;
443  int mCapacity;
444  char* mpStr;
445  str* mpTempStr;
446  char* mpTokenPtr;
447  static const int mInvalidIndex = -1;
448  static const int mAllocSize = 16;
449 
451  void init(int initialLength=mAllocSize)
452  {
453  mStackMem = false;
454  mCapacity = 0;
455  mpStr = 0;
456  mpTempStr = 0;
457  mpTokenPtr = 0;
458 
459  reAllocateMem(initialLength);
460  }
461 
464  bool ensureMemoryToInsertNChars(const int nChars);
465 
468  bool reAllocateMem(const int size);
469 
471  void copyFrom(const char* pString);
472 
474  int singleHexCharToInt(unsigned char theChar);
475 
477  int hexStrDigitsToInt(char* pString);
478 
480  friend bool test_str(void);
481 };
482 
483 #endif /* STR_HPP__ */
bool insertAt(const int index, const str &s)
Definition: str.hpp:184
bool endsWithIgnoreCase(const char *pString) const
Definition: str.cpp:389
const char * operator()() const
() Operator (without a name): Ex: puts(myCStr());
Definition: str.cpp:965
void operator-=(const char *pString)
Remove all instances of pString using -= Operator.
Definition: str.cpp:901
int countOf(const str &s) const
Definition: str.hpp:236
int countOf(const char *pString) const
Definition: str.cpp:320
bool insertAtBeg(const char *pString)
Definition: str.cpp:179
int lastIndexOf(const char *pString) const
Definition: str.cpp:302
bool compareToIgnoreCase(const str &s) const
Definition: str.hpp:208
bool beginsWithWholeWordIgnoreCase(const char *pString, char seperator=' ') const
Definition: str.cpp:363
str()
Default constructor.
Definition: str.cpp:31
void clear()
Clears the string by putting NULL terminator at 1st char.
Definition: str.cpp:85
void operator--()
Pre-Decrement if String is an integer.
Definition: str.cpp:856
int lastIndexOf(const str &s) const
Definition: str.hpp:222
bool operator!=(const char *pString) const
Definition: str.cpp:928
bool eraseFirst(int nChars)
Definition: str.cpp:417
bool contains(const char *pString) const
Definition: str.cpp:248
bool compareToIgnoreCase(const char *pString) const
Definition: str.cpp:244
void toUpper()
Make every alphabet character uppercase.
Definition: str.cpp:102
bool operator>(const char *pString) const
Definition: str.cpp:954
int printf(const char *pFormat,...)
Definition: str.cpp:111
bool endsWith(const char *pString) const
Definition: str.cpp:376
bool endsWithIgnoreCase(const str &s) const
Definition: str.hpp:255
bool replaceFirst(const char *pFind, const char *pWith)
Definition: str.cpp:559
bool eraseLast(int nChars)
Definition: str.cpp:428
bool operator<=(const str &s) const
Definition: str.hpp:422
bool isFloat() const
Definition: str.cpp:729
static float toFloat(const char *pString)
Static method to convert char* string to float.
Definition: str.cpp:29
bool replaceLast(const char *pFind, const char *pWith)
Definition: str.cpp:570
const str * getToken(const char *pSplitter=" ", bool restart=false)
Definition: str.cpp:662
int getCapacity() const
Definition: str.cpp:71
int tokenize(const char *delimators, int char_ptr_count,...)
Definition: str.cpp:153
bool operator>(const str &s) const
Definition: str.hpp:418
char & operator[](int pos)
Index Operator to get and set value @ Index.
Definition: str.cpp:980
bool insertAt(const int index, const char *pString)
Definition: str.cpp:192
~str()
Destructor.
Definition: str.cpp:55
friend bool test_str(void)
For tests :
int firstIndexOfIgnoreCase(const char *pString) const
Definition: str.cpp:254
bool beginsWithIgnoreCase(const char *pString) const
Definition: str.cpp:345
bool reserve(int n)
reserves memory to hold n characters
Definition: str.cpp:75
void checksum_Remove()
Removes checksum characters: Ex: 123:0A becomes 123.
Definition: str.cpp:802
bool compareTo(const char *pString) const
Definition: str.cpp:240
bool eraseAllAfter(int index)
Definition: str.cpp:443
Definition: str.hpp:100
bool isAlphaNumeric() const
Definition: str.cpp:711
bool beginsWithIgnoreCase(const str &s) const
Definition: str.hpp:241
bool endsWith(const str &s) const
Definition: str.hpp:253
bool erase(const char *pString)
Definition: str.cpp:408
void toLower()
Make every alphabet character lowercase.
Definition: str.cpp:94
void checksum_Append()
Appends checksum characters: Ex: 123 becomes: 123:0A.
Definition: str.cpp:795
int firstIndexOf(const char *pString) const
Definition: str.cpp:297
bool eraseFirstWords(int words, char separator= ' ')
Definition: str.cpp:469
bool operator>=(const str &s) const
Definition: str.hpp:426
int firstIndexOf(const str &s) const
Definition: str.hpp:220
static int toInt(const char *pString)
Static method to convert char* string to integer.
Definition: str.cpp:28
bool containsIgnoreCase(const str &s) const
Definition: str.hpp:233
bool containsIgnoreCase(const char *pString) const
Definition: str.cpp:293
bool operator>=(const char *pString) const
Definition: str.cpp:958
void append(const char *pString)
Appends constant string pointer.
Definition: str.cpp:214
bool beginsWithWholeWord(const char *pString, char seperator=' ') const
Definition: str.cpp:352
int replaceAll(const char *pFind, const char *pWith)
Definition: str.cpp:581
const char * c_str() const
Definition: str.hpp:435
void append(const str &s)
Appends another str.
Definition: str.hpp:193
void trimStart(const str &s)
Definition: str.hpp:282
int eraseAllSpecialChars()
Erase all characters except alphabets and numerals.
Definition: str.cpp:487
static float toFloat(str &s)
Definition: str.hpp:110
bool insertAtBeg(const str &s)
Definition: str.hpp:180
unsigned int checksum_Get()
Get integer value of XOR checksum of this string.
Definition: str.cpp:785
bool eraseCharAt(int index)
Definition: str.cpp:439
bool compareTo(const str &s) const
Definition: str.hpp:206
bool operator<(const char *pString) const
Definition: str.cpp:952
bool operator==(const char *pString) const
Definition: str.cpp:916
int firstIndexOfIgnoreCase(const str &s) const
Definition: str.hpp:217
bool insertAtEnd(const char *pString)
Definition: str.cpp:183
To use the FreeRTOS so play with it a little *uses roughly of and uses roughly of RAM *The larger the size
Definition: readme.txt:4
bool checksum_Verify()
Definition: str.cpp:807
void trimEnd(const char *pChars)
Definition: str.cpp:535
void trimEnd(const str &s)
Definition: str.hpp:284
bool operator<(const str &s) const
Definition: str.hpp:413
bool beginsWith(const str &s) const
Definition: str.hpp:239
bool beginsWith(const char *pString) const
Definition: str.cpp:338
void clearAll()
Zeroes out ALL memory that belongs to this str.
Definition: str.cpp:89
bool operator<=(const char *pString) const
Definition: str.cpp:956
void operator=(const char *pString)
Assign a string: myCStr = "123";.
Definition: str.cpp:829
bool insertAtEnd(const str &s)
Definition: str.hpp:182
void trimStart(const char *pChars)
Definition: str.cpp:507
static int toInt(str &s)
Definition: str.hpp:108
bool isAllAlpha() const
Definition: str.cpp:695
bool isUint() const
Definition: str.cpp:750
void operator++()
Pre-Increment if String is an integer.
Definition: str.cpp:847
bool contains(const str &s) const
Definition: str.hpp:231
int getLen() const
Definition: str.cpp:67
void appendAsHex(unsigned int num)
Appends as hexadecimal ie: DEADBEEF.
Definition: str.cpp:232
bool isInt() const
Definition: str.cpp:767
bool eraseAfter(int index, int nChars)
Definition: str.cpp:453
void operator+=(const char singleChar)
Append char using += Operator.
Definition: str.cpp:875
const str & subString(int fromIndex)
Definition: str.cpp:625
int scanf(const char *pFormat,...)
Definition: str.cpp:143
bool erase(const str &s)
Definition: str.hpp:264