libcommonc++  0.7
CStringBuilder Class Reference

A utility class for constructing C-style strings. More...

#include <CStringBuilder.h++>

Public Member Functions

 CStringBuilder (char *buf, size_t size)
 Construct a new CStringBuilder for the specified character buffer. More...
 
 ~CStringBuilder ()
 Destructor. More...
 
CStringBuilderclear ()
 Clear the buffer. More...
 
CStringBuilderappend (const char *s, size_t length=0)
 Append a C string to the buffer. More...
 
CStringBuilderappend (const CString &s, size_t length=0)
 Append a CString to the buffer. More...
 
CStringBuilderappend (char c)
 Append a character to the buffer. More...
 
CStringBuilderappend (int val, size_t width, bool zeroPad=true)
 Append the string representation of a signed integer to the buffer. More...
 
CStringBuilderappend (uint_t val, size_t width, bool zeroPad=true)
 Append the string representation of an unsigned integer to the buffer. More...
 
char * getPosition () const
 Get a pointer to the current write position for the buffer. More...
 
size_t getRemaining () const
 Get the remaining capacity of the buffer, that is, the number of characters that can still be written. More...
 
bool hasRemaining () const
 Determine if the buffer has space remaining for additional characters. More...
 
size_t getLength () const
 Get the current length of the buffer, that is, the number of characters currently in the buffer. More...
 
size_t length () const
 Get the current length of the buffer, that is, the number of characters currently in the buffer. More...
 
char * c_str () const
 Get a pointer to the beginning of the buffer. More...
 
bool bump (size_t delta)
 Bump the write position by the given number of characters. More...
 
void terminate ()
 NUL-terminate the buffer. More...
 
CStringBuilderoperator+= (const char *s)
 Append operator. More...
 
CStringBuilderoperator+= (char c)
 Append operator. More...
 

Detailed Description

A utility class for constructing C-style strings.

CStringBuilder is a simple wrapper around a raw character buffer and provides some basic append operations which write strings and integer values into the buffer, but which will never write past the end of the buffer. When append operations have been completed, a call to the terminate() method should be made to write the final NUL terminator character, for which the final byte in the buffer is always reserved.

Author
Mark Lindner

Constructor & Destructor Documentation

◆ CStringBuilder()

CStringBuilder ( char *  buf,
size_t  size 
)

Construct a new CStringBuilder for the specified character buffer.

Parameters
bufThe buffer.
sizeThe size of the buffer.

◆ ~CStringBuilder()

Destructor.

Member Function Documentation

◆ append() [1/5]

CStringBuilder & append ( const char *  s,
size_t  length = 0 
)

Append a C string to the buffer.

Parameters
sThe string to append.
lengthThe number of bytes to append, or 0 to append the entire string. If the buffer cannot accommodate the requested number of characters, the method appends as many characters as possible.

◆ append() [2/5]

CStringBuilder & append ( const CString s,
size_t  length = 0 
)

Append a CString to the buffer.

Parameters
sThe string to append.
lengthThe number of bytes to append, or 0 to append the entire string. If the buffer cannot accommodate the requested number of characters, the method appends as many characters as possible.

◆ append() [3/5]

CStringBuilder & append ( char  c)

Append a character to the buffer.

◆ append() [4/5]

CStringBuilder & append ( int  val,
size_t  width,
bool  zeroPad = true 
)

Append the string representation of a signed integer to the buffer.

If the buffer cannot accommodate the data, the request is ignored.

Parameters
valThe value to append.
widthThe formatting width for the value.
zeroPadA flag indicating whether the value should be padded on the right with zeroes (true) or spaces (false).

◆ append() [5/5]

CStringBuilder & append ( uint_t  val,
size_t  width,
bool  zeroPad = true 
)

Append the string representation of an unsigned integer to the buffer.

If the buffer cannot accommodate the data, the request is ignored.

Parameters
valThe value to append.
widthThe formatting width for the value.
zeroPadA flag indicating whether the value should be padded on the right with zeroes (true) or spaces (false).

◆ bump()

bool bump ( size_t  delta)

Bump the write position by the given number of characters.

Parameters
deltaThe number of characters by which to bump the write position. If the delta would move the write position past the end of the buffer, the request is ignored.

◆ c_str()

char* c_str ( ) const
inline

Get a pointer to the beginning of the buffer.

◆ clear()

CStringBuilder & clear ( )

Clear the buffer.

This method resets the write position to the beginning of the buffer.

◆ getLength()

size_t getLength ( ) const
inline

Get the current length of the buffer, that is, the number of characters currently in the buffer.

◆ getPosition()

char* getPosition ( ) const
inline

Get a pointer to the current write position for the buffer.

◆ getRemaining()

size_t getRemaining ( ) const
inline

Get the remaining capacity of the buffer, that is, the number of characters that can still be written.

◆ hasRemaining()

bool hasRemaining ( ) const
inline

Determine if the buffer has space remaining for additional characters.

◆ length()

size_t length ( ) const
inline

Get the current length of the buffer, that is, the number of characters currently in the buffer.

◆ operator+=() [1/2]

CStringBuilder& operator+= ( const char *  s)
inline

Append operator.

◆ operator+=() [2/2]

CStringBuilder& operator+= ( char  c)
inline

Append operator.

◆ terminate()

void terminate ( )

NUL-terminate the buffer.


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