str Class Reference

#include <str.hpp>

Public Member Functions

int getLen () const
 
int getCapacity () const
 
bool reserve (int n)
 reserves memory to hold n characters More...
 
void clear ()
 Clears the string by putting NULL terminator at 1st char. More...
 
void clearAll ()
 Zeroes out ALL memory that belongs to this str. More...
 
void toLower ()
 Make every alphabet character lowercase. More...
 
void toUpper ()
 Make every alphabet character uppercase. More...
 
int printf (const char *pFormat,...)
 
int scanf (const char *pFormat,...)
 
int tokenize (const char *delimators, int char_ptr_count,...)
 
const strgetToken (const char *pSplitter=" ", bool restart=false)
 
void operator+= (const char singleChar)
 Append char using += Operator. More...
 
void operator+= (const char *pString)
 Append String using += Operator. More...
 
void operator+= (const str &s)
 Append str to us. More...
 
void operator+= (int n)
 Subtract Integer using += Operator if string is an integer value. More...
 
void operator-= (const char *pString)
 Remove all instances of pString using -= Operator. More...
 
void operator-= (const str &s)
 Remove (erase) str from us. More...
 
void operator-= (int n)
 Subtract Integer String using if string is an integer value. More...
 
void operator+= (float n)
 Subtract Float using += Operator if string is an float value. More...
 
void operator-= (float n)
 Subtract Float String using -= Operator if string is an float value. More...
 
const char * operator() () const
 () Operator (without a name): Ex: puts(myCStr()); More...
 
const char * c_str () const
 
 operator float () const
 Get c-string pointer (calls operator above) More...
 
 operator int () const
 (int) Cast Operator: Ex: int x = (int)myCStr; More...
 
char & operator[] (int pos)
 Index Operator to get and set value @ Index. More...
 
Constructors (and destructor)

External memory constructor will be unable to allocate more memory and will fail to grow the string greater than the size

 str ()
 Default constructor. More...
 
 str (int capacity)
 Constructor with initial capacity. More...
 
 str (const char *pString)
 Construct from char* pointer. More...
 
 str (char *buff, int size)
 Construct to use external memory. More...
 
 str (const str &s)
 Copy Constructor. More...
 
 ~str ()
 Destructor. More...
 
Insertion functions
bool insertAtBeg (const char *pString)
 
bool insertAtBeg (const str &s)
 
bool insertAtEnd (const char *pString)
 
bool insertAtEnd (const str &s)
 
bool insertAt (const int index, const char *pString)
 
bool insertAt (const int index, const str &s)
 
Append functions
void append (const char *pString)
 Appends constant string pointer. More...
 
void append (const str &s)
 Appends another str. More...
 
void append (int x)
 Appends integer as characters. More...
 
void append (float x)
 Appends float as characters. More...
 
void appendAsHex (unsigned int num)
 Appends as hexadecimal ie: DEADBEEF. More...
 
Comparison functions
See also
See also the overloaded == and != operators
bool compareTo (const char *pString) const
 
bool compareTo (const str &s) const
 
bool compareToIgnoreCase (const char *pString) const
 
bool compareToIgnoreCase (const str &s) const
 
Get Word Index Functions
int firstIndexOfIgnoreCase (const char *pString) const
 
int firstIndexOfIgnoreCase (const str &s) const
 
int firstIndexOf (const char *pString) const
 
int firstIndexOf (const str &s) const
 
int lastIndexOf (const char *pString) const
 
int lastIndexOf (const str &s) const
 
Functions to check a contained string
bool contains (const char *pString) const
 
bool contains (const str &s) const
 
bool containsIgnoreCase (const char *pString) const
 
bool containsIgnoreCase (const str &s) const
 
int countOf (const char *pString) const
 
int countOf (const str &s) const
 
bool beginsWith (const char *pString) const
 
bool beginsWith (const str &s) const
 
bool beginsWithIgnoreCase (const char *pString) const
 
bool beginsWithIgnoreCase (const str &s) const
 
bool beginsWithWholeWord (const char *pString, char seperator=' ') const
 
bool beginsWithWholeWordIgnoreCase (const char *pString, char seperator=' ') const
 
bool endsWith (const char *pString) const
 
bool endsWith (const str &s) const
 
bool endsWithIgnoreCase (const char *pString) const
 
bool endsWithIgnoreCase (const str &s) const
 
Erase Functions
bool erase (const char *pString)
 
bool erase (const str &s)
 
bool eraseFirst (int nChars)
 
bool eraseLast (int nChars)
 
bool eraseCharAt (int index)
 
bool eraseAllAfter (int index)
 
bool eraseAfter (int index, int nChars)
 
bool eraseFirstWords (int words, char separator= ' ')
 
int eraseAllSpecialChars ()
 Erase all characters except alphabets and numerals. More...
 
Trimming Functions to remove leading or trailing character sets

Example: str s("...Hello..;;''"); s.trimEnd(".;'"); –> s is now: "...Hello"

void trimStart (const char *pChars)
 
void trimStart (const str &s)
 
void trimEnd (const char *pChars)
 
void trimEnd (const str &s)
 
Find & Replace Functions
bool replaceFirst (const char *pFind, const char *pWith)
 
bool replaceLast (const char *pFind, const char *pWith)
 
int replaceAll (const char *pFind, const char *pWith)
 
Sub-string Functions
Note
These sub-string functions will allocate an internal str object and each subString() function will return reference to this object to minimize dynamic memory allocation and avoid copy constructors.
const strsubString (int fromIndex)
 
const strsubString (int fromIndex, int charCount)
 
const strsubString (char fromFirstChar)
 
const strsubString (const char *fromStr)
 
const strsubString (char fromFirstChar, int charCount)
 
const strsubString (const char *fromStr, int charCount)
 
const strsubString (char fromFirstChar, char toLastChar)
 
Datatype detection Functions
bool isAllAlpha () const
 
bool isAlphaNumeric () const
 
bool isFloat () const
 
bool isUint () const
 
bool isInt () const
 
Checksum Functions
unsigned int checksum_Get ()
 Get integer value of XOR checksum of this string. More...
 
void checksum_Append ()
 Appends checksum characters: Ex: 123 becomes: 123:0A. More...
 
void checksum_Remove ()
 Removes checksum characters: Ex: 123:0A becomes 123. More...
 
bool checksum_Verify ()
 
Assignment Operators
void operator= (const char *pString)
 Assign a string: myCStr = "123";. More...
 
void operator= (int num)
 Assign an int : myCStr = 123;. More...
 
void operator= (float num)
 Assign a float: myCStr = 1.23;. More...
 
stroperator= (const str &rhs)
 Assign Operator for str a = str b. More...
 
Increment & Decrement Operators if the string consist of integer value
void operator++ ()
 Pre-Increment if String is an integer. More...
 
void operator++ (int unused)
 Post-Increment if String is an integer. More...
 
void operator-- ()
 Pre-Decrement if String is an integer. More...
 
void operator-- (int unused)
 Post-Decrement if String is an integer. More...
 
Equality Operators
bool operator== (const char *pString) const
 
bool operator== (const str &rhs) const
 
bool operator== (int) const
 
bool operator!= (const char *pString) const
 
bool operator!= (const str &rhs) const
 
bool operator!= (int) const
 
Comparison Operators to perform comparisons, such as:

str s; s = "hello"; if(s < "helloo") ... s = 123; if(s < 1234) ... s = 1.23; if(s >= 1.22) ...

bool operator< (const char *pString) const
 
bool operator< (const str &s) const
 
bool operator< (int) const
 
bool operator> (const char *pString) const
 
bool operator> (int) const
 
bool operator> (const str &s) const
 
bool operator<= (const char *pString) const
 
bool operator<= (int) const
 
bool operator<= (const str &s) const
 
bool operator>= (const char *pString) const
 
bool operator>= (int) const
 
bool operator>= (const str &s) const
 
bool operator< (float) const
 
bool operator>= (float) const
 
bool operator> (float) const
 
bool operator<= (float) const
 

Static Public Member Functions

Static conversion functions from string to int and float
Note
To convert this string to int or float, use cast operator: int x = (int)myStr;
static int toInt (const char *pString)
 Static method to convert char* string to integer. More...
 
static int toInt (str &s)
 
static float toFloat (const char *pString)
 Static method to convert char* string to float. More...
 
static float toFloat (str &s)
 

Friends

bool test_str (void)
 For tests : More...
 

Detailed Description

Custom string class

str class can provide many string manipulation functions while maintaining internal memory and allocating more memory when needed.

str s;
s = "Hello World...";
s.trimEnd(".");
printf("%s", s()); // str becomes: "Hello World"
s.erase("World");
printf("%s", s()); // str becomes: "Hello "
s += "World";
printf("%s", s()); // str back to: "Hello World"
// Use String class as an integer:
str myInt;
myInt = 123;
myInt++; if(myInt == 124) // true
myInt--; if(myInt == "123") // true
int n = (int)myInt; // Cast back from string to int, n = 123

You can also printf to this str object:

str s;
s.printf("Hello World %u\n", 123);

Parsing (Tokenize) Example:

str s;
s = "Hello,World.Parse Example!";
puts(s.getToken(",", true)->c_str()); // Prints: Hello
puts(s.getToken(".")->c_str()); // Prints: World
puts(s.getToken()->c_str()); // Prints: Parse
puts(s.getToken()->c_str()); // Prints Example!
assert(0 == s.getToken()); // No more tokens -> NULL Pointer

Note that the original str s is not destroyed during tokenize operations

Constructor & Destructor Documentation

str::str ( )

Default constructor.

str::str ( int  capacity)

Constructor with initial capacity.

str::str ( const char *  pString)

Construct from char* pointer.

str::str ( char *  buff,
int  size 
)

Construct to use external memory.

Cannot call init() for this constructor.

str::str ( const str s)

Copy Constructor.

str::~str ( )

Destructor.

Member Function Documentation

void str::append ( const char *  pString)

Appends constant string pointer.

void str::append ( const str s)
inline

Appends another str.

void str::append ( int  x)

Appends integer as characters.

void str::append ( float  x)

Appends float as characters.

void str::appendAsHex ( unsigned int  num)

Appends as hexadecimal ie: DEADBEEF.

bool str::beginsWith ( const char *  pString) const
bool str::beginsWith ( const str s) const
inline
bool str::beginsWithIgnoreCase ( const char *  pString) const
bool str::beginsWithIgnoreCase ( const str s) const
inline
bool str::beginsWithWholeWord ( const char *  pString,
char  seperator = ' ' 
) const

Checks if the string begins with a whole word. If str = "Hello World", then str.beginsWithWholdWord("Hello") will be true But if str = "HelloWorld" then this will be false because this function will check whole word separated by space char unless the separator itself is provided.

bool str::beginsWithWholeWordIgnoreCase ( const char *  pString,
char  seperator = ' ' 
) const
const char* str::c_str ( ) const
inline
void str::checksum_Append ( )

Appends checksum characters: Ex: 123 becomes: 123:0A.

unsigned int str::checksum_Get ( )

Get integer value of XOR checksum of this string.

void str::checksum_Remove ( )

Removes checksum characters: Ex: 123:0A becomes 123.

bool str::checksum_Verify ( )
Returns
TRUE if for example: actual checksum 0A matches calculated 0A of string "123"
void str::clear ( void  )

Clears the string by putting NULL terminator at 1st char.

void str::clearAll ( )

Zeroes out ALL memory that belongs to this str.

bool str::compareTo ( const char *  pString) const
bool str::compareTo ( const str s) const
inline
bool str::compareToIgnoreCase ( const char *  pString) const
bool str::compareToIgnoreCase ( const str s) const
inline
bool str::contains ( const char *  pString) const
bool str::contains ( const str s) const
inline
bool str::containsIgnoreCase ( const char *  pString) const
bool str::containsIgnoreCase ( const str s) const
inline
int str::countOf ( const char *  pString) const
int str::countOf ( const str s) const
inline
bool str::endsWith ( const char *  pString) const
bool str::endsWith ( const str s) const
inline
bool str::endsWithIgnoreCase ( const char *  pString) const
bool str::endsWithIgnoreCase ( const str s) const
inline
bool str::erase ( const char *  pString)
bool str::erase ( const str s)
inline
bool str::eraseAfter ( int  index,
int  nChars 
)
bool str::eraseAllAfter ( int  index)
int str::eraseAllSpecialChars ( )

Erase all characters except alphabets and numerals.

bool str::eraseCharAt ( int  index)
bool str::eraseFirst ( int  nChars)
bool str::eraseFirstWords ( int  words,
char  separator = ' ' 
)
bool str::eraseLast ( int  nChars)
int str::firstIndexOf ( const char *  pString) const
int str::firstIndexOf ( const str s) const
inline
int str::firstIndexOfIgnoreCase ( const char *  pString) const
int str::firstIndexOfIgnoreCase ( const str s) const
inline
int str::getCapacity ( ) const
Returns
the current allocated capacity of str
int str::getLen ( ) const
Returns
Number of characters in the string
const str * str::getToken ( const char *  pSplitter = " ",
bool  restart = false 
)

Tokenize function

Parameters
pSplitterThe tokens that mark the end of the token to get.
restartRestarts tokenize operation on contents of this str. This optional parameter is not needed after restarting 1st tokenize operation.
Returns
Pointer to substring, or NULL if no more tokens remain
Note
Unlike strtok(), the contents of this str is not destroyed.
Warning
The returned substring pointer is temporary, and further calls to this function will re-use the same substr, so copy the returned substring if it is to be used later.

Example:

str s = "Hello,World tokentest";
str* t1 = s.getToken(",", true); // t1 == "Hello"
str* t2 = s.getToken(" "); // t2 == "World"
str* t3 = s.getToken(); // t3 == "tokentest"
bool str::insertAt ( const int  index,
const char *  pString 
)
bool str::insertAt ( const int  index,
const str s 
)
inline
bool str::insertAtBeg ( const char *  pString)
bool str::insertAtBeg ( const str s)
inline
bool str::insertAtEnd ( const char *  pString)
bool str::insertAtEnd ( const str s)
inline
bool str::isAllAlpha ( ) const
Returns
TRUE if the string is all alphabetical characters only
bool str::isAlphaNumeric ( ) const
Returns
TRUE if the string is all alpha or numerical characters only
bool str::isFloat ( ) const
Returns
TRUE if the string is a floating point number
bool str::isInt ( ) const
Returns
TRUE if the string is a number
bool str::isUint ( ) const
Returns
TRUE if the string is an unsigned number
int str::lastIndexOf ( const char *  pString) const
int str::lastIndexOf ( const str s) const
inline
str::operator float ( ) const

Get c-string pointer (calls operator above)

(float) Cast Operator: Ex: float x = (float)myCStr;

str::operator int ( ) const

(int) Cast Operator: Ex: int x = (int)myCStr;

bool str::operator!= ( const char *  pString) const
bool str::operator!= ( const str rhs) const
bool str::operator!= ( int  n) const
const char * str::operator() ( ) const

() Operator (without a name): Ex: puts(myCStr());

void str::operator++ ( )

Pre-Increment if String is an integer.

void str::operator++ ( int  unused)

Post-Increment if String is an integer.

void str::operator+= ( const char  singleChar)

Append char using += Operator.

Add/Subtract Operators:

void str::operator+= ( const char *  pString)

Append String using += Operator.

void str::operator+= ( const str s)

Append str to us.

void str::operator+= ( int  n)

Subtract Integer using += Operator if string is an integer value.

void str::operator+= ( float  n)

Subtract Float using += Operator if string is an float value.

void str::operator-- ( )

Pre-Decrement if String is an integer.

void str::operator-- ( int  unused)

Post-Decrement if String is an integer.

void str::operator-= ( const char *  pString)

Remove all instances of pString using -= Operator.

void str::operator-= ( const str s)

Remove (erase) str from us.

void str::operator-= ( int  n)

Subtract Integer String using if string is an integer value.

void str::operator-= ( float  n)

Subtract Float String using -= Operator if string is an float value.

bool str::operator< ( const char *  pString) const
bool str::operator< ( const str s) const
inline
bool str::operator< ( int  x) const
bool str::operator< ( float  x) const
bool str::operator<= ( const char *  pString) const
bool str::operator<= ( int  x) const
bool str::operator<= ( const str s) const
inline
bool str::operator<= ( float  x) const
void str::operator= ( const char *  pString)

Assign a string: myCStr = "123";.

void str::operator= ( int  num)

Assign an int : myCStr = 123;.

void str::operator= ( float  num)

Assign a float: myCStr = 1.23;.

str & str::operator= ( const str rhs)

Assign Operator for str a = str b.

bool str::operator== ( const char *  pString) const
bool str::operator== ( const str rhs) const
bool str::operator== ( int  n) const
bool str::operator> ( const char *  pString) const
bool str::operator> ( int  x) const
bool str::operator> ( const str s) const
inline
bool str::operator> ( float  x) const
bool str::operator>= ( const char *  pString) const
bool str::operator>= ( int  x) const
bool str::operator>= ( const str s) const
inline
bool str::operator>= ( float  x) const
char & str::operator[] ( int  pos)

Index Operator to get and set value @ Index.

int str::printf ( const char *  pFormat,
  ... 
)

Similar to printf, but will print data into the string

Returns
The number of characters printed
int str::replaceAll ( const char *  pFind,
const char *  pWith 
)
bool str::replaceFirst ( const char *  pFind,
const char *  pWith 
)
bool str::replaceLast ( const char *  pFind,
const char *  pWith 
)
bool str::reserve ( int  n)

reserves memory to hold n characters

int str::scanf ( const char *  pFormat,
  ... 
)

Similar to scanf but scans from this str

Returns
The number of parameters successfully parsed
Note
Using %*s will scan a string and ignore it.
Deliminator by default is a space, read "sscanf()" documentation online for more references.

Example:

str s = "Something 10:12 pm";
unsigned hour, min;
char amPm[5] = { 0 };
int parsed = str.scanf("%*s %u:%u %2s", &hour, &min, amPm);
const str & str::subString ( int  fromIndex)
const str & str::subString ( int  fromIndex,
int  charCount 
)
const str & str::subString ( char  fromFirstChar)
const str & str::subString ( const char *  fromStr)
const str & str::subString ( char  fromFirstChar,
int  charCount 
)
const str & str::subString ( const char *  fromStr,
int  charCount 
)
const str & str::subString ( char  fromFirstChar,
char  toLastChar 
)
float str::toFloat ( const char *  pString)
static

Static method to convert char* string to float.

static float str::toFloat ( str s)
inlinestatic
int str::toInt ( const char *  pString)
static

Static method to convert char* string to integer.

static int str::toInt ( str s)
inlinestatic
int str::tokenize ( const char *  delimators,
int  char_ptr_count,
  ... 
)

Perform string tokenization (original copy is destroyed) If you want to get pointers separating the string such as "hello world 123", then you can use this function, however, your original string will be destroyed.

str myStr = "123 45 6789";
char *one, *two, *three;
if (myStr.tokenize(" ", 3, &one, &two, &three) == 3) {
// This will print "123 45 6789"
printf("%s %s %s\n", one, two, three);
}
void str::toLower ( )

Make every alphabet character lowercase.

void str::toUpper ( )

Make every alphabet character uppercase.

void str::trimEnd ( const char *  pChars)
void str::trimEnd ( const str s)
inline
void str::trimStart ( const char *  pChars)
void str::trimStart ( const str s)
inline

Friends And Related Function Documentation

bool test_str ( void  )
friend

For tests :


The documentation for this class was generated from the following files:
  • /var/www/html/SJSU-DEV-Linux/firmware/default/lib/L3_Utils/str.hpp
  • /var/www/html/SJSU-DEV-Linux/firmware/default/lib/L3_Utils/src/str.cpp