edelib  2.1.0
Public Member Functions | Static Public Attributes | Related Functions | List of all members
String Class Reference

A (relatively simple) string implementation. More...

#include <edelib/String.h>

Public Member Functions

 String ()
 
 String (const char *str)
 
 String (const String &str)
 
 ~String ()
 
Stringassign (const char *str, size_type len)
 
Stringassign (const char *str)
 
Stringassign (const String &str)
 
Stringappend (const char *str, size_type len)
 
Stringappend (const char *str)
 
Stringappend (const String &str)
 
Stringappend (size_type num, const char &ch)
 
void reserve (size_type len)
 
void swap (String &from)
 
String substr (size_type index, size_type num=npos) const
 
size_type find (const char *str, size_type offset) const
 
size_type find (char ch, size_type offset) const
 
size_type find (const char *str) const
 
void clear (void)
 
void printf (const char *fmt,...)
 
void trim_left (void)
 
void trim_right (void)
 
void trim (void)
 
const char * c_str (void)
 
const char * c_str (void) const
 
const char * data (void) const
 
size_type length (void) const
 
size_type capacity (void) const
 
bool empty (void) const
 
Stringreplace (char c1, char c2)
 
char & operator[] (size_type index)
 
char operator[] (size_type index) const
 
Stringoperator= (const char *str)
 
Stringoperator= (const String &str)
 
Stringoperator+= (const char *str)
 
Stringoperator+= (const String &str)
 
Stringoperator+= (const char &ch)
 

Static Public Attributes

static const size_type npos
 

Related Functions

(Note that these are not member functions.)

String operator+ (const String &s1, const String &s2)
 
String operator+ (const char *s1, const String &s2)
 
String operator+ (const String &s1, const char *s2)
 
bool operator== (const String &str1, const char *str2)
 
bool operator!= (const String &str1, const char *str2)
 
bool operator> (const String &str1, const char *str2)
 
bool operator>= (const String &str1, const char *str2)
 
bool operator< (const String &str1, const char *str2)
 
bool operator<= (const String &str1, const char *str2)
 
bool operator== (const char *str1, const String &str2)
 
bool operator!= (const char *str1, const String &str2)
 
bool operator> (const char *str1, const String &str2)
 
bool operator>= (const char *str1, const String &str2)
 
bool operator< (const char *str1, const String &str2)
 
bool operator<= (const char *str1, const String &str2)
 
bool operator== (const String &str1, const String &str2)
 
bool operator!= (const String &str1, const String &str2)
 
bool operator> (const String &str1, const String &str2)
 
bool operator>= (const String &str1, const String &str2)
 
bool operator< (const String &str1, const String &str2)
 
bool operator<= (const String &str1, const String &str2)
 

Detailed Description

A (relatively simple) string implementation.

This implementation tries to be compatible with std::string implementation, althought it does not implement all gory details from std::string. There are few reasons why this class exists:

This class does not provide find_xxx(), insert() and erase methods and iterators.

Some methods, like printf() does not exist in std::string. Also, the behaviour of capacity() differs from std::string like:

* String s = "foo";
* std::string s1 = "baz";
* s.capacity() != s1.capacity() // implementation specific, according to the Standard
*
* // but...
* s.reserve(20);
* s1.reserve(20);
* s.capacity() == s1.capacity() // same sizes
*

If you are not familiar with std::string, following things with this class can be done:

* String s = "sample string";
* s += " yes"; // gives "sample string yes"
* s.clear(); // clear content
* s.assign("foo") // same as: s = "foo"
* s.append("baz") // append some data, gives "foobaz"
* if(s == "foobaz") ... // comparison
* if(s > "bla") ... // check if content of s is grater than "bla"
* s += "xxx"; // same as s.append("xxx");
* s.find("baz"); // return position of "baz" in "foobaz"
* s.find("demo"); // return String::npos, and can be checked like:
*
* if(s.find("demo") == String::npos)
* // do something smart
*
* // not in std::string
* s.printf("%s = %i", "num", 4); // will be "num = 4"
*
Note
Since String increase internal buffer's size when is needed, some things should be considered to minimize reallocations:
  • use reserve() if you know the length
  • prefer operator+= than operator+
Todo:
COW would be nice

Constructor & Destructor Documentation

String ( )

Create empty string object

String ( const char *  str)

Create a new string with copy of pointer to characters

Parameters
stra pointer to c-like string (it should not be NULL)
String ( const String str)

Create a new string with copy of another string

Parameters
stris object of type String
~String ( )

Clears all internal data. All possible external pointers to internal buffer will be invalidated

Member Function Documentation

String& append ( const char *  str,
size_type  len 
)

Appends content of c-like string, with given length to the end of current string

Returns
itself
Parameters
stra pointer to c-like string (it should not be NULL)
lena number of character that will be appended
String& append ( const char *  str)

Appends content of c-like string with it's full length to the end of current string

Returns
itself
Parameters
stra pointer to c-like string (it should not be NULL)
String& append ( const String str)

Appends content of String object to the end of current string

Returns
itself
Parameters
stra object of type String
String& append ( size_type  num,
const char &  ch 
)

Appends given character num times at the end of character string

Returns
itself
Parameters
numis number of given character
chis character to append
String& assign ( const char *  str,
size_type  len 
)

Assign content of c-like string, with given size. This method will destroy the previous content of the string

Returns
itself
Parameters
stra pointer to c-like string (it should not be NULL)
lena number of character that will be assigned
String& assign ( const char *  str)

Assign content of c-like string with it's full length.

Returns
itself
Parameters
stra pointer to c-like string (it should not be NULL)
String& assign ( const String str)

Assing content of String object

Returns
itself
Parameters
stra object of type String
const char* c_str ( void  )
inline

Return data formated as c-like string

Can be used as input for C functions, like:

* if(strcmp(s.c_str(), "my smart string") == 0)
* ...
*

Referenced by TiXmlPrinter::CStr(), TiXmlPrinter::Indent(), TiXmlPrinter::LineBreak(), String::operator!=(), String::operator<(), String::operator<=(), String::operator==(), String::operator>(), and String::operator>=().

const char* c_str ( void  ) const
inline

Return data formated as c-like string

size_type capacity ( void  ) const
inline

Retrun size of internal buffer

void clear ( void  )

Clear all elements of current string

const char* data ( void  ) const
inline

Retrun pointer to internal buffer

Do not use this function as input for C functions.

bool empty ( void  ) const
inline

Checks if string is empty

size_type find ( const char *  str,
size_type  offset 
) const

Returns starting position of str starting at offset. If str is not found, String::npos will be returned

Returns
position of str, or String::npos if not found
Parameters
stris string we are looking for
offsetposition to start looking from
size_type find ( char  ch,
size_type  offset 
) const

Returns starting position of given character starting at the given offset. If character is not found, String::npos will be returned

Returns
position of given character, or String::npos if not found
Parameters
chcharacter we are looking for
offsetposition to start looking from
size_type find ( const char *  str) const

Returns start of given string. Behaves same as find(str, 0)

size_type length ( void  ) const
inline

Retrun size of character data

Referenced by String::operator==(), TiXmlPrinter::Size(), and edelib::stringtok().

String& operator+= ( const char *  str)

Same as append(str)

String& operator+= ( const String str)

Same as append(String type)

String& operator+= ( const char &  ch)

Same as append(1, ch)

String& operator= ( const char *  str)

Same as assign(str)

String& operator= ( const String str)

Same as assign(String type)

char& operator[] ( size_type  index)

Returns character at given index

char operator[] ( size_type  index) const

Returns character at given index

void printf ( const char *  fmt,
  ... 
)

Assign data in printf form. All previous content will be deleted.

String& replace ( char  c1,
char  c2 
)

Replace every occurence of c1 with the c2

Returns
itself
Parameters
c1is character that will be replaced
c2is character used for replacement
void reserve ( size_type  len)

Set size of internal buffer

Parameters
lenis size we want
String substr ( size_type  index,
size_type  num = npos 
) const

Returns a substring of the current string starting at the index with num characters long. If num is not specified, returned will be remain data starting from index

Returns
substring
Parameters
indexstarting position for substring
numending position for substring

Referenced by edelib::stringtok().

void swap ( String from)

Exchange the elements of current string with given

Parameters
fromis replacement target
void trim ( void  )

Remove starting and ending spaces

void trim_left ( void  )

Remove starting spaces

void trim_right ( void  )

Remove ending spaces

Friends And Related Function Documentation

bool operator!= ( const String str1,
const char *  str2 
)
related

Check if String and cstring are not equal

References String::c_str().

bool operator!= ( const char *  str1,
const String str2 
)
related

Check if cstring and String are not equal

References String::c_str().

bool operator!= ( const String str1,
const String str2 
)
related

Check if two String's are not equal

References String::c_str().

String operator+ ( const String s1,
const String s2 
)
related

Concat two String objects

String operator+ ( const char *  s1,
const String s2 
)
related

Concat cstring and String object

String operator+ ( const String s1,
const char *  s2 
)
related

Concat String and cstring

bool operator< ( const String str1,
const char *  str2 
)
related

Check if String is less than cstring

References String::c_str().

bool operator< ( const char *  str1,
const String str2 
)
related

Check if cstring is less than String

References String::c_str().

bool operator< ( const String str1,
const String str2 
)
related

Check if first String is less than the second

References String::c_str().

bool operator<= ( const String str1,
const char *  str2 
)
related

Check if String is less or equal to cstring

References String::c_str().

bool operator<= ( const char *  str1,
const String str2 
)
related

Check if cstring is less or equal to the String

References String::c_str().

bool operator<= ( const String str1,
const String str2 
)
related

Check if first String is less or equal than the second

References String::c_str().

bool operator== ( const String str1,
const char *  str2 
)
related

Check if String and cstring are equal

References String::c_str().

bool operator== ( const char *  str1,
const String str2 
)
related

Check if cstring and String are equal

References String::c_str().

bool operator== ( const String str1,
const String str2 
)
related

Check if two String's are equal

References String::c_str(), and String::length().

bool operator> ( const String str1,
const char *  str2 
)
related

Check if String is larger than cstring

References String::c_str().

bool operator> ( const char *  str1,
const String str2 
)
related

Check if cstring is larger than String

References String::c_str().

bool operator> ( const String str1,
const String str2 
)
related

Check if first String is larger than the second

References String::c_str().

bool operator>= ( const String str1,
const char *  str2 
)
related

Check if String is larger or equal to the cstring

References String::c_str().

bool operator>= ( const char *  str1,
const String str2 
)
related

Check if cstring is larger or equal to String

References String::c_str().

bool operator>= ( const String str1,
const String str2 
)
related

Check if first String is larger or equal than the second

References String::c_str().

Member Data Documentation

const size_type npos
static

This will be returned when find() method fails. If is meant to be used in form:

* String s;
* if(s.find("this does not exist") == String::npos)
* // do something smart
*

The documentation for this class was generated from the following file: