My Project
Public Types | Public Member Functions | List of all members
ParaEngine::StringBuilderT< UserAllocator > Class Template Reference

A NON-thread-safe, mutable sequence of characters(Binary is also possible). More...

#include <StringBuilder.h>

Public Types

typedef std::string string_type
 The standard string object which correspond to the builder. More...
 
typedef UserAllocator user_allocator
 
typedef char Char
 

Public Member Functions

 StringBuilderT (const string_type &sz)
 Creates a new builder with the content of the given string. More...
 
 StringBuilderT (const Char *sz)
 Creates a new builder with the content of the given character array. More...
 
 StringBuilderT (Char ch, size_t count)
 Creates a new builder with the given character repeated multiple times over the array. More...
 
 StringBuilderT (size_t reserved)
 Creates a new builder with an empty buffer. More...
 
 StringBuilderT ()
 Creates a new builder with an empty buffer. More...
 
 ~StringBuilderT ()
 Deletes the builder. More...
 
void reserve (size_t length)
 Reserves a given number of character slots. More...
 
void resize (size_t length)
 resize the buffer
 
size_t length () const
 Retrieves the length of the content within the builder. More...
 
size_t size () const
 
void clear ()
 Clears the content of the builder. More...
 
bool empty () const
 Retrieves whether the builder is empty. More...
 
void append (Char c)
 Appends a character to the content of the builder. More...
 
void append (const string_type &sz)
 Appends a string to the content of the builder. More...
 
void append (const Char *sz)
 Appends a character array to the content of the builder. More...
 
void append (const Char *sz, size_t len)
 Appends a character array to the content of the builder. More...
 
void append (const StringBuilderT< UserAllocator > &b)
 Appends the content of a builder to the content of this builder. More...
 
void append (int32 i)
 Appends the integer value, after converting it to a string, to the content of the builder. More...
 
void append (uint32 i)
 See above. More...
 
void append (uint64 i)
 See above. More...
 
void appendBinary (uint16 i)
 
void appendBinary (uint32 i)
 See above. More...
 
void appendHex (uint8 i)
 Appends the integer value, after converting it to a fm::string, in hexadecimal, to the content of the builder. More...
 
template<class T >
void appendHex (const T &i)
 See above. More...
 
void append (long i)
 See above. More...
 
void append (float f)
 Appends the floating-point value, after converting it to a string, to the content of the builder. More...
 
void append (double f)
 See above. More...
 
template<typename TYPE >
void WriteAt (int nIndex, const TYPE &val)
 this is useful for writing to a previous cached location. More...
 
void WriteAt (int nIndex, const Char *data, size_t nSize)
 
template<typename TYPE >
StringBuilderToperator+= (const TYPE &val)
 Appends a value to the content of the builder. More...
 
void appendLine (const Char *sz)
 Appends a character array to the content of the builder. More...
 
void remove (int32 start)
 Removes a section of the content of the builder. More...
 
void remove (int32 start, int32 end)
 Removes a section of the content of the builder. More...
 
void pop_back ()
 Removes the last character of the content of the builder. More...
 
template<typename TYPE >
void set (const TYPE &val)
 Sets the content of the builder to a given value. More...
 
template<typename TYPE >
StringBuilderToperator= (const TYPE &val)
 See above. More...
 
string_type ToString () const
 Converts the content of the builder to a standard string. More...
 
const Char * c_str () const
 Converts the content of the builder to a character array. More...
 
 operator const Char * () const
 See above. More...
 
Char * str ()
 get raw string
 
Char & operator[] (const int nIndex)
 overload the operator []. More...
 
int32 index (Char c) const
 Retrieves the index of the first character within the content of the builder that is equivalent to the given character. More...
 
int32 rindex (Char c) const
 Retrieves the index of the last character within the content of the builder that is equivalent to the given character. More...
 
Char back () const
 Retrieves the last character within the content of the builder. More...
 

Detailed Description

template<typename UserAllocator>
class ParaEngine::StringBuilderT< UserAllocator >

A NON-thread-safe, mutable sequence of characters(Binary is also possible).

A string buffer is like a std::string, but does not have SSO(16 bytes small string optimization); use ParaEngine::NPLString if one wants SSO support. A string buffer is usually associated with a memory pool allocator such as CNPLPool_Char_alloc<char>. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String buffers are NOT safe for use by multiple threads. One should use a mutex if the object is shared by multiple thread.

The principal operations on a StringBuffer are the append, which are overloaded so as to accept data of many types. Each effectively converts a given datum to a string and then appends the characters of that string to the string buffer.

Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.

: c_str() is NOT null terminated, unless one manually append('\0'); so always use a size() to retrieve it.

Member Typedef Documentation

§ string_type

template<typename UserAllocator>
typedef std::string ParaEngine::StringBuilderT< UserAllocator >::string_type

The standard string object which correspond to the builder.

Constructor & Destructor Documentation

§ StringBuilderT() [1/5]

template<typename UserAllocator >
ParaEngine::StringBuilderT< UserAllocator >::StringBuilderT ( const string_type sz)

Creates a new builder with the content of the given string.

Parameters
szA string. Its content will be copied within the builder.

§ StringBuilderT() [2/5]

template<typename UserAllocator >
ParaEngine::StringBuilderT< UserAllocator >::StringBuilderT ( const Char *  sz)

Creates a new builder with the content of the given character array.

Parameters
szA character array. Its content will be copied within the builder. It must terminate with an element containing the 'zero' value.

§ StringBuilderT() [3/5]

template<typename UserAllocator >
ParaEngine::StringBuilderT< UserAllocator >::StringBuilderT ( Char  ch,
size_t  count 
)

Creates a new builder with the given character repeated multiple times over the array.

Parameters
chA character to repeat.
countThe number of times to repeat the given character.

§ StringBuilderT() [4/5]

template<typename UserAllocator >
ParaEngine::StringBuilderT< UserAllocator >::StringBuilderT ( size_t  reserved)

Creates a new builder with an empty buffer.

See also
reserve
Parameters
reservedThe number of character slots to reserve within the empty buffer.

§ StringBuilderT() [5/5]

template<typename UserAllocator >
ParaEngine::StringBuilderT< UserAllocator >::StringBuilderT ( )

Creates a new builder with an empty buffer.

§ ~StringBuilderT()

template<typename UserAllocator >
ParaEngine::StringBuilderT< UserAllocator >::~StringBuilderT ( )

Deletes the builder.

Its buffer will be cleared. Any pointers to its data will be dangling.

Member Function Documentation

§ append() [1/11]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::append ( Char  c)

Appends a character to the content of the builder.

Parameters
cA character. May not be the 'zero' value.

§ append() [2/11]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::append ( const string_type sz)

Appends a string to the content of the builder.

Parameters
szA string.

§ append() [3/11]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::append ( const Char *  sz)

Appends a character array to the content of the builder.

Parameters
szA character array. It must terminate with an element containing the 'zero' value.

§ append() [4/11]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::append ( const Char *  sz,
size_t  len 
)

Appends a character array to the content of the builder.

Parameters
szA character array. It should not contain any 'zero' characters before 'len'
lenThe number of characters to read from sz.

§ append() [5/11]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::append ( const StringBuilderT< UserAllocator > &  b)

Appends the content of a builder to the content of this builder.

Parameters
bA string builder.

§ append() [6/11]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::append ( int32  i)

Appends the integer value, after converting it to a string, to the content of the builder.

Parameters
iAn integer value.

§ append() [7/11]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::append ( uint32  i)

See above.

§ append() [8/11]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::append ( uint64  i)

See above.

§ append() [9/11]

template<typename UserAllocator>
void ParaEngine::StringBuilderT< UserAllocator >::append ( long  i)
inline

See above.

§ append() [10/11]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::append ( float  f)

Appends the floating-point value, after converting it to a string, to the content of the builder.

If the floating-point value is the special token that represents infinity, the string "INF" is appended. If it represents the negative infinity, the string "-INF" is appended. If it represents the impossibility, the string "NaN" is appended.

Parameters
fA floating-point value.

§ append() [11/11]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::append ( double  f)

See above.

§ appendBinary()

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::appendBinary ( uint32  i)

See above.

§ appendHex() [1/2]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::appendHex ( uint8  i)

Appends the integer value, after converting it to a fm::string, in hexadecimal, to the content of the builder.

The size of the integer will determine the number of characters used.

Parameters
iAn unsigned integer value.

§ appendHex() [2/2]

template<typename UserAllocator>
template<class T >
void ParaEngine::StringBuilderT< UserAllocator >::appendHex ( const T &  i)
inline

See above.

§ appendLine()

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::appendLine ( const Char *  sz)

Appends a character array to the content of the builder.

A newline character will be appended after the character array.

Parameters
szA character array. It must terminate with an element containing the 'zero' value.

§ back()

template<typename UserAllocator>
Char ParaEngine::StringBuilderT< UserAllocator >::back ( ) const
inline

Retrieves the last character within the content of the builder.

Returns
The last character of the builder.

§ c_str()

template<typename UserAllocator >
const StringBuilderT< UserAllocator >::Char * ParaEngine::StringBuilderT< UserAllocator >::c_str ( ) const

Converts the content of the builder to a character array.

Please note the char array is not null terminated.

Returns
A character array with the content of the builder. This pointer is valid for the lifetime of the buffer of the builder, so do not keep it around. This character array should not be modified.

§ clear()

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::clear ( )

Clears the content of the builder.

This does not re-allocate a new buffer.

§ empty()

template<typename UserAllocator>
bool ParaEngine::StringBuilderT< UserAllocator >::empty ( ) const
inline

Retrieves whether the builder is empty.

A builder is considered empty when it has no content, regardless of the size or allocation status of its buffer.

Returns
Whether the builder is empty.

§ index()

template<typename UserAllocator >
int32 ParaEngine::StringBuilderT< UserAllocator >::index ( Char  c) const

Retrieves the index of the first character within the content of the builder that is equivalent to the given character.

Parameters
cThe character to match.
Returns
The index of the first equivalent character. -1 is returned if no character matches the given character.

§ length()

template<typename UserAllocator>
size_t ParaEngine::StringBuilderT< UserAllocator >::length ( void  ) const
inline

Retrieves the length of the content within the builder.

Returns
The length of the string.

§ operator const Char *()

template<typename UserAllocator>
ParaEngine::StringBuilderT< UserAllocator >::operator const Char * ( ) const
inline

See above.

§ operator+=()

template<typename UserAllocator>
template<typename TYPE >
StringBuilderT& ParaEngine::StringBuilderT< UserAllocator >::operator+= ( const TYPE &  val)
inline

Appends a value to the content of the builder.

This is a shortcut for the append function.

See also
append
Parameters
valA value. This may be numerical, a character, a character array or a string.

§ operator=()

template<typename UserAllocator>
template<typename TYPE >
StringBuilderT& ParaEngine::StringBuilderT< UserAllocator >::operator= ( const TYPE &  val)
inline

See above.

§ operator[]()

template<typename UserAllocator>
Char& ParaEngine::StringBuilderT< UserAllocator >::operator[] ( const int  nIndex)
inline

overload the operator [].

You are at your own risk if nIndex is out of risk. There is no check for performance reasons.

§ pop_back()

template<typename UserAllocator>
void ParaEngine::StringBuilderT< UserAllocator >::pop_back ( )
inline

Removes the last character of the content of the builder.

§ remove() [1/2]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::remove ( int32  start)

Removes a section of the content of the builder.

Every character that occurs after the given index will be removed, resulting in a shrunk string.

Parameters
startAn index within the content of the builder.

§ remove() [2/2]

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::remove ( int32  start,
int32  end 
)

Removes a section of the content of the builder.

The substring defined by the 'start' and 'end' indices will be removed. The 'start' character is removed and is replaced by the 'end' character.

Parameters
startThe index of the first character of the substring to remove.
endThe index of the first character after the removed substring.

§ reserve()

template<typename UserAllocator >
void ParaEngine::StringBuilderT< UserAllocator >::reserve ( size_t  length)

Reserves a given number of character slots.

Internally it uses buffer 32, 64, 128, 256, 512, 1024, 2048. Larger than 2048, length is used etc.

Parameters
lengthThe number of character slots to reserve.

§ rindex()

template<typename UserAllocator >
int32 ParaEngine::StringBuilderT< UserAllocator >::rindex ( Char  c) const

Retrieves the index of the last character within the content of the builder that is equivalent to the given character.

Parameters
cThe character to match.
Returns
The index of the last equivalent character. -1 is returned if no character matches the given character.

§ set()

template<typename UserAllocator>
template<typename TYPE >
void ParaEngine::StringBuilderT< UserAllocator >::set ( const TYPE &  val)
inline

Sets the content of the builder to a given value.

This clears the builder of all its content and appends the given value.

Parameters
valA value. This may be numerical, a character, a character array or a string.

§ ToString()

template<typename UserAllocator>
string_type ParaEngine::StringBuilderT< UserAllocator >::ToString ( ) const
inline

Converts the content of the builder to a standard string.

Returns
A string with the content of the builder.

§ WriteAt()

template<typename UserAllocator>
template<typename TYPE >
void ParaEngine::StringBuilderT< UserAllocator >::WriteAt ( int  nIndex,
const TYPE &  val 
)
inline

this is useful for writing to a previous cached location.


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