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"
static const size_type npos
Definition: String.h:111
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() [1/3]

String ( )

Create empty string object

◆ String() [2/3]

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() [3/3]

String ( const String str)

Create a new string with copy of another string

Parameters
stris object of type String

◆ ~String()

~String ( )

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

Member Function Documentation

◆ append() [1/4]

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)

◆ append() [2/4]

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

◆ append() [3/4]

String & append ( const String str)

Appends content of String object to the end of current string

Returns
itself
Parameters
stra object of type String

◆ append() [4/4]

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

◆ assign() [1/3]

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)

◆ assign() [2/3]

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

◆ assign() [3/3]

String & assign ( const String str)

Assing content of String object

Returns
itself
Parameters
stra object of type String

◆ c_str() [1/2]

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(), TempFile::name(), String::operator!=(), String::operator<(), String::operator<=(), String::operator==(), String::operator>(), String::operator>=(), and EdbusObjectPath::path().

◆ c_str() [2/2]

const char * c_str ( void  ) const
inline

Return data formated as c-like string

◆ capacity()

size_type capacity ( void  ) const
inline

Retrun size of internal buffer

◆ clear()

void clear ( void  )

Clear all elements of current string

◆ data()

const char * data ( void  ) const
inline

Retrun pointer to internal buffer

Do not use this function as input for C functions.

◆ empty()

bool empty ( void  ) const
inline

Checks if string is empty

Referenced by TempFile::name().

◆ find() [1/3]

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

◆ find() [2/3]

size_type find ( const char *  str) const

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

◆ find() [3/3]

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

◆ length()

size_type length ( void  ) const
inline

Retrun size of character data

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

◆ operator+=() [1/3]

String & operator+= ( const char &  ch)

Same as append(1, ch)

◆ operator+=() [2/3]

String & operator+= ( const char *  str)

Same as append(str)

◆ operator+=() [3/3]

String & operator+= ( const String str)

Same as append(String type)

◆ operator=() [1/2]

String & operator= ( const char *  str)

Same as assign(str)

◆ operator=() [2/2]

String & operator= ( const String str)

Same as assign(String type)

◆ operator[]() [1/2]

char & operator[] ( size_type  index)

Returns character at given index

◆ operator[]() [2/2]

char operator[] ( size_type  index) const

Returns character at given index

◆ printf()

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

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

◆ replace()

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

◆ reserve()

void reserve ( size_type  len)

Set size of internal buffer

Parameters
lenis size we want

◆ substr()

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().

◆ swap()

void swap ( String from)

Exchange the elements of current string with given

Parameters
fromis replacement target

◆ trim()

void trim ( void  )

Remove starting and ending spaces

◆ trim_left()

void trim_left ( void  )

Remove starting spaces

◆ trim_right()

void trim_right ( void  )

Remove ending spaces

Friends And Related Function Documentation

◆ operator!=() [1/3]

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

Check if cstring and String are not equal

References String::c_str().

◆ operator!=() [2/3]

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

Check if String and cstring are not equal

References String::c_str().

◆ operator!=() [3/3]

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

Check if two String's are not equal

References String::c_str().

◆ operator+() [1/3]

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

Concat cstring and String object

◆ operator+() [2/3]

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

Concat String and cstring

◆ operator+() [3/3]

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

Concat two String objects

◆ operator<() [1/3]

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

Check if cstring is less than String

References String::c_str().

◆ operator<() [2/3]

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

Check if String is less than cstring

References String::c_str().

◆ operator<() [3/3]

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

Check if first String is less than the second

References String::c_str().

◆ operator<=() [1/3]

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

Check if cstring is less or equal to the String

References String::c_str().

◆ operator<=() [2/3]

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

Check if String is less or equal to cstring

References String::c_str().

◆ operator<=() [3/3]

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

Check if first String is less or equal than the second

References String::c_str().

◆ operator==() [1/3]

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

Check if cstring and String are equal

References String::c_str().

◆ operator==() [2/3]

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

Check if String and cstring are equal

References String::c_str().

◆ operator==() [3/3]

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

Check if two String's are equal

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

◆ operator>() [1/3]

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

Check if cstring is larger than String

References String::c_str().

◆ operator>() [2/3]

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

Check if String is larger than cstring

References String::c_str().

◆ operator>() [3/3]

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

Check if first String is larger than the second

References String::c_str().

◆ operator>=() [1/3]

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

Check if cstring is larger or equal to String

References String::c_str().

◆ operator>=() [2/3]

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

Check if String is larger or equal to the cstring

References String::c_str().

◆ operator>=() [3/3]

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

◆ npos

const size_type npos
static

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

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

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