UCommon
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes
ucommon::String Class Reference

A copy-on-write string class that operates by reference count. More...

#include <string.h>

Inheritance diagram for ucommon::String:
Inheritance graph
[legend]
Collaboration diagram for ucommon::String:
Collaboration graph
[legend]

Public Types

enum  { SENSITIVE = 0x00, INSENSITIVE = 0x01 }
 This is an internal class which contains the actual string data along with some control fields. More...
 

Public Member Functions

int __isoc99_scanf (const char *format,...)
 Scan input items from a string object. More...
 
int __isoc99_vscanf (const char *format, va_list args)
 Scan input items from a string object. More...
 
void add (const char *text)
 Append null terminated text to our string buffer. More...
 
void add (char character)
 Append a single character to our string buffer. More...
 
char at (int position) const
 Return character found at a specific position in the string. More...
 
const char * begin (void) const
 Get pointer to first character in string for iteration. More...
 
char * c_mem ()
 
const char * c_str (void) const
 Get character text buffer of string object. More...
 
size_t ccount (const char *list) const
 Count number of occurrences of characters in string. More...
 
void chop (const char *list)
 Chop trailing characters from the string. More...
 
void chop (size_t count=1)
 Chop trailing characters from text. More...
 
const char * chr (char character) const
 Find pointer in string where specified character appears. More...
 
void clear (size_t offset)
 Clear a field of a filled string with filler. More...
 
void clear (void)
 Clear string by setting to empty.
 
virtual int compare (const char *string) const
 Compare the values of two string. More...
 
String copy (size_t offset, size_t size) const
 Convenience method for substring extraction. More...
 
size_t count (void) const
 Count all characters in the string (strlen). More...
 
void cut (size_t offset, size_t size=0)
 Cut (remove) text from string. More...
 
char * data (void)
 Get memory text buffer of string object. More...
 
const char * end (void) const
 Get pointer to last character in string for iteration. More...
 
void erase (void)
 Erase string memory.
 
void fill (size_t size, char fill)
 
const char * find (const char *list, size_t offset=0) const
 Find a character in the string. More...
 
bool full (void) const
 Test if the string's allocated space is all used up. More...
 
String get (size_t offset, size_t size=0) const
 Get a new string object as a substring of the current object. More...
 
String left (size_t size) const
 Convenience method for left of string. More...
 
size_t len (void) const
 Get length of string. More...
 
void lower (void)
 Convert string to lower case.
 
size_t offset (const char *pointer) const
 Find offset of a pointer into our string buffer. More...
 
Stringoperator & (const char *text)
 Concatenate null terminated text to our object. More...
 
const char * operator * () const
 Reference raw text buffer by pointer operator. More...
 
bool operator *= (const char *substring)
 
bool operator *= (regex &expr)
 
Stringoperator *= (size_t number)
 Delete a specified number of characters from start of string. More...
 
 operator bool () const
 Test if string has data. More...
 
 operator const char * () const
 Casting reference to raw text string. More...
 
bool operator! () const
 Test if string is empty. More...
 
bool operator!= (const char *text) const
 Compare our object with null terminated text. More...
 
Stringoperator% (short &value)
 Parse short integer value from a string. More...
 
Stringoperator% (unsigned short &value)
 Parse short integer value from a string. More...
 
Stringoperator% (long &value)
 Parse long integer value from a string. More...
 
Stringoperator% (unsigned long &value)
 Parse long integer value from a string. More...
 
Stringoperator% (double &value)
 Parse double value from a string. More...
 
Stringoperator% (const char *text)
 Parse text from a string in a scan expression. More...
 
Stringoperator&= (const char *text)
 
String operator() (int offset, size_t size) const
 Get a new substring through object expression. More...
 
const char * operator() (int offset) const
 Reference a string in the object by relative offset. More...
 
const String operator+ (const char *text) const
 Concatenate null terminated text to our object. More...
 
Stringoperator++ (void)
 Delete first character from string.
 
Stringoperator+= (const char *text)
 Concatenate text to an existing string object. More...
 
Stringoperator+= (size_t number)
 Delete a specified number of characters from start of string. More...
 
Stringoperator-- (void)
 Delete last character from string.
 
Stringoperator-= (size_t number)
 Delete a specified number of characters from end of string. More...
 
bool operator< (const char *text) const
 Compare our object with null terminated text. More...
 
Stringoperator<< (const char *text)
 
Stringoperator<< (char code)
 
bool operator<= (const char *text) const
 Compare our object with null terminated text. More...
 
Stringoperator= (const String &object)
 Assign our string with the cstring of another object. More...
 
Stringoperator= (const char *text)
 Assign text to our existing buffer. More...
 
bool operator== (const char *text) const
 Compare our object with null terminated text. More...
 
bool operator> (const char *text) const
 Compare our object with null terminated text. More...
 
bool operator>= (const char *text) const
 Compare our object with null terminated text. More...
 
const char operator[] (int offset) const
 Reference a single character in string object by array offset. More...
 
Stringoperator^= (const String &object)
 Create new cow instance and assign value from another string object. More...
 
Stringoperator^= (const char *text)
 Create new cow instance and assign value from null terminated text. More...
 
Stringoperator| (const char *text)
 Concatenate null terminated text to our object. More...
 
Stringoperator|= (const char *text)
 Concatenate text to an existing string object. More...
 
void paste (size_t offset, const char *text, size_t size=0)
 Insert (paste) text into string. More...
 
size_t printf (const char *format,...)
 Print items into a string object. More...
 
const char * rchr (char character) const
 Find pointer in string where specified character last appears. More...
 
unsigned replace (const char *string, const char *text=NULL, unsigned flags=0)
 
unsigned replace (regex &expr, const char *text=NULL, unsigned flags=0)
 
virtual bool resize (size_t size)
 Resize and re-allocate string memory. More...
 
const char * rfind (const char *list, size_t offset=npos) const
 Find last occurrence of character in the string. More...
 
String right (size_t offset) const
 Convenience method for right of string. More...
 
void rset (const char *text, char overflow, size_t offset, size_t size=0)
 Set a text field within our string object offset from the end of buffer. More...
 
const char * rskip (const char *list, size_t offset=npos) const
 Skip trailing characters in the string. More...
 
void rsplit (const char *pointer)
 Split the string by a pointer position. More...
 
void rsplit (size_t offset)
 Split the string at a specific offset. More...
 
const char * search (const char *string, unsigned instance=0, unsigned flags=0) const
 Search for a substring in the string. More...
 
const char * search (regex &expr, unsigned instance=0, unsigned flags=0) const
 
void set (const char *text)
 Set string object to text of a null terminated string. More...
 
void set (size_t offset, const char *text, size_t size=0)
 Set a portion of the string object at a specified offset to a text string. More...
 
void set (const char *text, char overflow, size_t offset, size_t size=0)
 Set a text field within our string object. More...
 
size_t size (void) const
 Get the size of currently allocated space for string. More...
 
const char * skip (const char *list, size_t offset=0) const
 Skip lead characters in the string. More...
 
void split (const char *pointer)
 Split the string by a pointer position. More...
 
void split (size_t offset)
 Split the string at a specific offset. More...
 
 String ()
 Create a new empty string object.
 
 String (size_t size)
 Create an empty string with a buffer pre-allocated to a specified size. More...
 
 String (size_t size, const char *format,...)
 Create a string by printf-like formating into a pre-allocated space of a specified size. More...
 
 String (const char *text)
 Create a string from null terminated text. More...
 
 String (const char *text, size_t size)
 Create a string from null terminated text up to a maximum specified size. More...
 
 String (const char *text, const char *end)
 Create a string for a substring. More...
 
 String (const String &existing)
 Construct a copy of a string object. More...
 
void strip (const char *list)
 Strip lead and trailing characters from the string. More...
 
double tod (char **pointer=NULL)
 Convert string to a double value. More...
 
char * token (char **last, const char *list, const char *quote=NULL, const char *end=NULL)
 A thread-safe token parsing routine for strings objects. More...
 
long tol (char **pointer=NULL)
 Convert string to a long value. More...
 
void trim (const char *list)
 Trim lead characters from the string. More...
 
void trim (size_t count=1)
 Trim lead characters from text. More...
 
bool unquote (const char *quote)
 Unquote a quoted string. More...
 
void upper (void)
 Convert string to upper case.
 
size_t vprintf (const char *format, va_list args)
 Print items into a string object. More...
 
virtual ~String ()
 Destroy string. More...
 
- Public Member Functions inherited from ucommon::ObjectProtocol
ObjectProtocolcopy (void)
 Retain (increase retention of) object when copying.
 
void operator++ (void)
 Increase retention operator.
 
void operator-- (void)
 Decrease retention operator.
 
virtual ~ObjectProtocol ()
 Required virtual destructor.
 

Static Public Member Functions

static char * add (char *buffer, size_t size, const char *text)
 Safely append a null terminated string into an existing string in memory. More...
 
static char * add (char *buffer, size_t size, const char *text, size_t max)
 Safely append a null terminated string into an existing string in memory. More...
 
static String b64 (const uint8_t *binary, size_t size)
 Standard radix 64 string encoding. More...
 
static size_t b64count (const char *str, bool ws=false)
 
static size_t b64decode (uint8_t *binary, const char *string, size_t size, bool ws=false)
 Standard radix 64 decoding. More...
 
static size_t b64encode (char *string, const uint8_t *binary, size_t size, size_t width=0)
 Standard radix 64 encoding. More...
 
static size_t b64size (size_t size)
 
static unsigned ccount (const char *text, const char *list)
 Count instances of characters in a list in a text buffer. More...
 
static bool check (const char *string, size_t maximum, size_t minimum=0)
 Check if string is valid and in specific constraints. More...
 
static char * chop (char *text, const char *list)
 Strip trailing characters from the text string. More...
 
static int collate (const char *text1, const char *text2)
 
static int compare (const char *text1, const char *text2)
 Safe string collation function. More...
 
static int compare (const char *text1, const char *text2, size_t size)
 Depreciated string comparison function. More...
 
static char * copy (const char *text, size_t offset, size_t len)
 
static size_t count (const char *text)
 Safe version of strlen function. More...
 
static uint16_t crc16 (uint8_t *binary, size_t size)
 ccitt 16 bit crc for binary data. More...
 
static uint32_t crc24 (uint8_t *binary, size_t size)
 24 bit crc as used in openpgp. More...
 
static void cut (char *text, size_t offset, size_t len)
 
static char * dup (const char *text)
 Duplicate null terminated text into the heap. More...
 
static bool eq_case (const char *text1, const char *text2)
 Simple case insensitive equal test for strings. More...
 
static bool eq_case (const char *text1, const char *text2, size_t size)
 Simple case insensitive equal test for strings. More...
 
static bool equal (const char *text1, const char *text2)
 Simple equal test for strings. More...
 
static bool equal (const char *text1, const char *text2, size_t size)
 Simple equal test for strings. More...
 
static void erase (char *text)
 Erase string memory. More...
 
static char * fill (char *text, size_t size, char character)
 Fill a section of memory with a fixed text character. More...
 
static const char * find (const char *text, const char *key, const char *optional)
 Find position of substring within a string. More...
 
static char * find (char *text, const char *list)
 Find the first occurrence of a character in a text buffer. More...
 
static void fix (String &object)
 Fix and reset string object filler. More...
 
static String hex (const uint8_t *binary, size_t size)
 Convert binary data buffer into hex string. More...
 
static size_t hex2bin (const char *string, uint8_t *binary, size_t maxsize, bool wsflag=false)
 
static size_t hexcount (const char *str, bool ws=false)
 
static size_t hexdump (const uint8_t *binary, char *string, const char *format)
 Dump hex data to a string buffer. More...
 
static size_t hexpack (uint8_t *binary, const char *string, const char *format)
 Pack hex data from a string buffer. More...
 
static size_t hexsize (const char *format)
 
static const char * ifind (const char *text, const char *key, const char *optional)
 Find position of case insensitive substring within a string. More...
 
static char * left (const char *text, size_t size)
 Duplicate null terminated text of specific size to heap. More...
 
static void lower (char *text)
 Convert null terminated text to lower case. More...
 
static void paste (char *text, size_t max, size_t offset, const char *data, size_t len=0)
 
static const char * pos (const char *text, ssize_t offset)
 Compute position in string. More...
 
static char * rfind (char *text, const char *list)
 Find the last occurrence of a character in a text buffer. More...
 
static char * right (const char *text, size_t size)
 
static char * rset (char *buffer, size_t size, const char *text)
 Set a field in a null terminated string relative to the end of text. More...
 
static char * rskip (char *text, const char *list)
 Skip before trailing characters in a null terminated string. More...
 
static size_t seek (char *text, const char *list)
 Offset until next occurrence of character in a text or length. More...
 
static char * set (char *buffer, size_t size, const char *text)
 Safely set a null terminated string buffer in memory. More...
 
static char * set (char *buffer, size_t size, const char *text, size_t max)
 Safely set a null terminated string buffer in memory. More...
 
static char * skip (char *text, const char *list)
 Skip after lead characters in a null terminated string. More...
 
static char * strip (char *text, const char *list)
 Skip lead and remove trailing characters from a text string. More...
 
static void swap (String &object1, String &object2)
 Swap the cstring references between two strings. More...
 
static double tod (const char *text, char **pointer=NULL)
 Convert text to a double value. More...
 
static char * token (char *text, char **last, const char *list, const char *quote=NULL, const char *end=NULL)
 A thread-safe token parsing routine for null terminated strings. More...
 
static long tol (const char *text, char **pointer=NULL)
 Convert text to a long value. More...
 
static char * trim (char *text, const char *list)
 Return start of string after characters to trim from beginning. More...
 
static char * unquote (char *text, const char *quote)
 Unquote a quoted null terminated string. More...
 
static void upper (char *text)
 Convert null terminated text to upper case. More...
 

Static Public Attributes

static const char eos = '\0'
 
static const size_t npos = ((size_t)-1)
 

Protected Member Functions

virtual cstring * c_copy (void) const
 Return cstring to use in copy constructors. More...
 
virtual void cow (size_t size=0)
 Copy on write operation for cstring. More...
 
cstring * create (size_t size) const
 Factory create a cstring object of specified size. More...
 
bool equal (const char *string) const
 Test if two string values are equal. More...
 
size_t getStringSize (void) const
 
virtual void release (void)
 Decrease retention of our reference counted cstring. More...
 
virtual void retain (void)
 Increase retention of our reference counted cstring. More...
 

Protected Attributes

cstring * str
 cstring instance our object references. More...
 

Detailed Description

A copy-on-write string class that operates by reference count.

This string class anchors a counted object that is managed as a copy-on-write instance of the string data. This means that multiple instances of the string class can refer to the same string in memory if it has not been modifed, which reduces heap allocation. The string class offers functions to manipulate both the string object, and generic safe string functions to manipulate ordinary null terminated character arrays directly in memory.

Author
David Sugar dyfet.nosp@m.@gnu.nosp@m.telep.nosp@m.hony.nosp@m..org
Examples
datetime.cpp, shell.cpp, and string.cpp.

Definition at line 78 of file string.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum

This is an internal class which contains the actual string data along with some control fields.

The string can be either NULL terminated or filled like a Pascal-style string, but with a user selected fill character. The cstring object is an overdraft object, as the actual string text which is of unknown size follows immediately after the class control data. This class is primarily for internal use.

Author
David Sugar dyfet.nosp@m.@gnu.nosp@m.telep.nosp@m.hony.nosp@m..org

Definition at line 93 of file string.h.

Constructor & Destructor Documentation

◆ String() [1/6]

ucommon::String::String ( size_t  size)

Create an empty string with a buffer pre-allocated to a specified size.

Parameters
sizeof buffer to allocate.

◆ String() [2/6]

ucommon::String::String ( size_t  size,
const char *  format,
  ... 
)

Create a string by printf-like formating into a pre-allocated space of a specified size.

A typical use might be in a concat function like String x = (String)something + (String){10, "%ud", var}.

Parameters
sizeof buffer to allocate.
formatcontrol for string.

◆ String() [3/6]

ucommon::String::String ( const char *  text)

Create a string from null terminated text.

Parameters
textto use for string.

◆ String() [4/6]

ucommon::String::String ( const char *  text,
size_t  size 
)

Create a string from null terminated text up to a maximum specified size.

Parameters
textto use for string.
sizelimit of new string.

◆ String() [5/6]

ucommon::String::String ( const char *  text,
const char *  end 
)

Create a string for a substring.

The end of the substring is a pointer within the substring itself.

Parameters
textto use for string.
endof text in substring.

◆ String() [6/6]

ucommon::String::String ( const String existing)

Construct a copy of a string object.

Our copy inherets the same reference counted instance of cstring as in the original.

Parameters
existingstring to copy from.

◆ ~String()

virtual ucommon::String::~String ( )
virtual

Destroy string.

De-reference cstring. If last reference to cstring, then also remove cstring from heap.

Member Function Documentation

◆ __isoc99_scanf()

int ucommon::String::__isoc99_scanf ( const char *  format,
  ... 
)

Scan input items from a string object.

Parameters
formatstring of input to scan.
Returns
number of items scanned.

◆ __isoc99_vscanf()

int ucommon::String::__isoc99_vscanf ( const char *  format,
va_list  args 
)

Scan input items from a string object.

Parameters
formatstring of input to scan.
argslist to scan into.
Returns
number of items scanned.

◆ add() [1/4]

void ucommon::String::add ( const char *  text)

Append null terminated text to our string buffer.

Parameters
textto append.

◆ add() [2/4]

void ucommon::String::add ( char  character)

Append a single character to our string buffer.

Parameters
characterto append.

◆ add() [3/4]

static char* ucommon::String::add ( char *  buffer,
size_t  size,
const char *  text 
)
static

Safely append a null terminated string into an existing string in memory.

If the resulting string is too large to fit into the buffer, it is truncated to the size.

Parameters
bufferto set.
sizeof buffer. Includes null byte at end of string.
textto set in buffer.
Returns
pointer to text buffer.

◆ add() [4/4]

static char* ucommon::String::add ( char *  buffer,
size_t  size,
const char *  text,
size_t  max 
)
static

Safely append a null terminated string into an existing string in memory.

If the resulting string is too large to fit into the buffer, it is truncated to the size.

Parameters
bufferto set.
sizeof buffer. Includes null byte at end of string.
textto set in buffer.
maxsize of text to append.
Returns
pointer to text buffer.

◆ at()

char ucommon::String::at ( int  position) const

Return character found at a specific position in the string.

Parameters
positionin string, negative values computed from end.
Returns
character code at specified position in string.

◆ b64()

static String ucommon::String::b64 ( const uint8_t *  binary,
size_t  size 
)
static

Standard radix 64 string encoding.

Parameters
binarydata to encode.
sizeof binary data to encode.
Returns
encoded string.

◆ b64decode()

static size_t ucommon::String::b64decode ( uint8_t *  binary,
const char *  string,
size_t  size,
bool  ws = false 
)
static

Standard radix 64 decoding.

Parameters
binarydata to save.
stringof encoded text.
sizeof destination buffer.
wsflag to skip whitespaces.
Returns
number of characters actually decoded.

◆ b64encode()

static size_t ucommon::String::b64encode ( char *  string,
const uint8_t *  binary,
size_t  size,
size_t  width = 0 
)
static

Standard radix 64 encoding.

Parameters
stringof encoded text save into.
binarydata to encode.
sizeof binary data to encode.
widthof string buffer for data if partial supported.
Returns
number of bytes encoded.

◆ begin()

const char* ucommon::String::begin ( void  ) const

Get pointer to first character in string for iteration.

Returns
first character pointer or NULL if empty.

◆ c_copy()

virtual cstring* ucommon::String::c_copy ( void  ) const
protectedvirtual

Return cstring to use in copy constructors.

Is virtual for memstring.

Returns
cstring for copy constructor.

Reimplemented in ucommon::memstring.

◆ c_str()

const char* ucommon::String::c_str ( void  ) const

Get character text buffer of string object.

Returns
character text buffer.

◆ ccount() [1/2]

size_t ucommon::String::ccount ( const char *  list) const

Count number of occurrences of characters in string.

Parameters
listof characters to find.
Returns
count of instances of characters in string.

◆ ccount() [2/2]

static unsigned ucommon::String::ccount ( const char *  text,
const char *  list 
)
static

Count instances of characters in a list in a text buffer.

Parameters
textbuffer to examine.
listof characters to count in buffer.
Returns
number of instances of the characters in buffer.

◆ check()

static bool ucommon::String::check ( const char *  string,
size_t  maximum,
size_t  minimum = 0 
)
static

Check if string is valid and in specific constraints.

Parameters
stringto check.
maximumsize allowed.
minimumsize required.
Returns
true if string is valid.
Examples
string.cpp.

◆ chop() [1/3]

void ucommon::String::chop ( const char *  list)

Chop trailing characters from the string.

Parameters
listof characters to remove.

◆ chop() [2/3]

void ucommon::String::chop ( size_t  count = 1)
inline

Chop trailing characters from text.

Parameters
countof characters to remove.

Definition at line 454 of file string.h.

◆ chop() [3/3]

static char* ucommon::String::chop ( char *  text,
const char *  list 
)
static

Strip trailing characters from the text string.

This function will modify memory.

Parameters
textbuffer to examine.
listof characters to chop from trailing end of string.
Returns
pointer to text buffer.

◆ chr()

const char* ucommon::String::chr ( char  character) const

Find pointer in string where specified character appears.

Parameters
characterto find.
Returns
string pointer for character if found, NULL if not.

◆ clear()

void ucommon::String::clear ( size_t  offset)

Clear a field of a filled string with filler.

Parameters
offsetto start of field to clear.

◆ compare() [1/3]

virtual int ucommon::String::compare ( const char *  string) const
virtual

Compare the values of two string.

This is a virtual so that it can be overridden for example if we want to create strings which ignore case, or which have special ordering rules.

Parameters
stringto compare with.
Returns
0 if equal, <0 if less than, 0> if greater than.

◆ compare() [2/3]

static int ucommon::String::compare ( const char *  text1,
const char *  text2 
)
static

Safe string collation function.

Parameters
text1to compare.
text2to compare.
Returns
0 if equal, >0 if text1 > text2, <0 if text1 < text2.

◆ compare() [3/3]

static int ucommon::String::compare ( const char *  text1,
const char *  text2,
size_t  size 
)
static

Depreciated string comparison function.

Parameters
text1to compare.
text2to compare.
sizelimit of strings to compare.
Returns
0 if equal, >0 if text1 > text2, <0 if text1 < text2.

◆ copy()

String ucommon::String::copy ( size_t  offset,
size_t  size 
) const
inline

Convenience method for substring extraction.

Parameters
offsetinto string.
sizeof string to return.
Returns
string object holding substring.

Definition at line 712 of file string.h.

◆ count() [1/2]

size_t ucommon::String::count ( void  ) const

Count all characters in the string (strlen).

Returns
count of characters.

◆ count() [2/2]

static size_t ucommon::String::count ( const char *  text)
static

Safe version of strlen function.

Accepts NULL as 0 length strings.

Parameters
textstring.
Returns
length of string.

◆ cow()

virtual void ucommon::String::cow ( size_t  size = 0)
protectedvirtual

Copy on write operation for cstring.

This always creates a new unique copy for write/modify operations and is a virtual for memstring to disable.

Parameters
sizeto add to allocated space when creating new cstring.

◆ crc16()

static uint16_t ucommon::String::crc16 ( uint8_t *  binary,
size_t  size 
)
static

ccitt 16 bit crc for binary data.

Parameters
binarydata to sum.
sizeof binary data to sum.
Returns
16 bit crc.

◆ crc24()

static uint32_t ucommon::String::crc24 ( uint8_t *  binary,
size_t  size 
)
static

24 bit crc as used in openpgp.

Parameters
binarydata to sum.
sizeof binary data to sum.
Returns
24 bit crc of data.

◆ create()

cstring* ucommon::String::create ( size_t  size) const
protected

Factory create a cstring object of specified size.

Parameters
sizeof allocated space for string buffer.
Returns
new cstring object.

◆ cut()

void ucommon::String::cut ( size_t  offset,
size_t  size = 0 
)

Cut (remove) text from string.

Parameters
offsetto start of text field to remove.
sizeof text field to remove or 0 to remove to end of string.

◆ data()

char* ucommon::String::data ( void  )

Get memory text buffer of string object.

Returns
writable string buffer.

◆ dup()

static char* ucommon::String::dup ( const char *  text)
static

Duplicate null terminated text into the heap.

Parameters
textto duplicate.
Returns
duplicate copy of text allocated from heap.

◆ end()

const char* ucommon::String::end ( void  ) const

Get pointer to last character in string for iteration.

Returns
last character pointer or NULL if empty.

◆ eq_case() [1/2]

static bool ucommon::String::eq_case ( const char *  text1,
const char *  text2 
)
static

Simple case insensitive equal test for strings.

Parameters
text1to test.
text2to test.
Returns
true if equal.

◆ eq_case() [2/2]

static bool ucommon::String::eq_case ( const char *  text1,
const char *  text2,
size_t  size 
)
static

Simple case insensitive equal test for strings.

Parameters
text1to test.
text2to test.
sizelimit of strings to compare.
Returns
true if equal.

◆ equal() [1/3]

bool ucommon::String::equal ( const char *  string) const
protected

Test if two string values are equal.

Parameters
stringto compare with.
Returns
true if equal.

◆ equal() [2/3]

static bool ucommon::String::equal ( const char *  text1,
const char *  text2 
)
static

Simple equal test for strings.

Parameters
text1to test.
text2to test.
Returns
true if equal and case is same.

◆ equal() [3/3]

static bool ucommon::String::equal ( const char *  text1,
const char *  text2,
size_t  size 
)
static

Simple equal test for strings.

Parameters
text1to test.
text2to test.
sizelimit of strings to compare.
Returns
true if equal and case is same.

◆ erase()

static void ucommon::String::erase ( char *  text)
static

Erase string memory.

Often used to clear out passwords.

Parameters
textstring to erase.

◆ fill()

static char* ucommon::String::fill ( char *  text,
size_t  size,
char  character 
)
static

Fill a section of memory with a fixed text character.

Adds a null byte at the end.

Parameters
textbuffer to fill.
sizeof text buffer with null terminated byte.
characterto fill with.
Returns
pointer to text buffer.

◆ find() [1/3]

const char* ucommon::String::find ( const char *  list,
size_t  offset = 0 
) const

Find a character in the string.

Parameters
listof characters to search for.
offsetto start of search.
Returns
pointer to first occurrence of character.

◆ find() [2/3]

static const char* ucommon::String::find ( const char *  text,
const char *  key,
const char *  optional 
)
static

Find position of substring within a string.

Parameters
textto search in.
keystring to locate.
optionalseparator chars if formatted as list of keys.
Returns
substring position if found, or NULL.

◆ find() [3/3]

static char* ucommon::String::find ( char *  text,
const char *  list 
)
static

Find the first occurrence of a character in a text buffer.

Parameters
textbuffer to examine.
listof characters to search for.
Returns
pointer to first instance found or NULL.

◆ fix()

static void ucommon::String::fix ( String object)
static

Fix and reset string object filler.

Parameters
objectto fix.

◆ full()

bool ucommon::String::full ( void  ) const

Test if the string's allocated space is all used up.

Returns
true if no more room for append.

◆ get()

String ucommon::String::get ( size_t  offset,
size_t  size = 0 
) const

Get a new string object as a substring of the current object.

Parameters
offsetof substring.
sizeof substring or 0 if to end.
Returns
string object holding substring.

◆ hex()

static String ucommon::String::hex ( const uint8_t *  binary,
size_t  size 
)
static

Convert binary data buffer into hex string.

Parameters
binarydata to convert.
sizeof data.
Returns
string from data.
Examples
string.cpp.

◆ hexdump()

static size_t ucommon::String::hexdump ( const uint8_t *  binary,
char *  string,
const char *  format 
)
static

Dump hex data to a string buffer.

Parameters
binarymemory to dump.
stringto save into.
formatstring to convert with.
Returns
number of bytes processed.
Examples
string.cpp.

◆ hexpack()

static size_t ucommon::String::hexpack ( uint8_t *  binary,
const char *  string,
const char *  format 
)
static

Pack hex data from a string buffer.

Parameters
binarymemory to pack.
stringto save into.
formatstring to convert with.
whitespaceflag to ignore.
Returns
number of bytes processed.
Examples
string.cpp.

◆ ifind()

static const char* ucommon::String::ifind ( const char *  text,
const char *  key,
const char *  optional 
)
static

Find position of case insensitive substring within a string.

Parameters
textto search in.
keystring to locate.
optionalseparator chars if formatted as list of keys.
Returns
substring position if found, or NULL.

◆ left() [1/2]

String ucommon::String::left ( size_t  size) const
inline

Convenience method for left of string.

Parameters
sizeof substring to gather.
Returns
string object holding substring.

Definition at line 693 of file string.h.

◆ left() [2/2]

static char* ucommon::String::left ( const char *  text,
size_t  size 
)
static

Duplicate null terminated text of specific size to heap.

Parameters
textto duplicate.
sizeof text, maximum space allocated.
Returns
duplicate copy of text allocated on heap.

◆ len()

size_t ucommon::String::len ( void  ) const

Get length of string.

Returns
length of string.
Examples
string.cpp.

◆ lower()

static void ucommon::String::lower ( char *  text)
static

Convert null terminated text to lower case.

Parameters
textto convert.

◆ offset()

size_t ucommon::String::offset ( const char *  pointer) const

Find offset of a pointer into our string buffer.

This can be used to find the offset position of a pointer returned by find, for example. This is used when one needs to convert a member function that returns a pointer to call a member function that operates by a offset value. If the pointer is outside the range of the string then npos is returned.

Parameters
pointerinto our object's string buffer.

◆ operator &()

String& ucommon::String::operator & ( const char *  text)

Concatenate null terminated text to our object.

This directly appends the text to the string buffer and does not resize the object if the existing cstring allocation space is fully used.

Parameters
textto concatenate.

◆ operator *()

const char* ucommon::String::operator * ( ) const
inline

Reference raw text buffer by pointer operator.

Returns
null terminated text of string.

Definition at line 670 of file string.h.

◆ operator *=()

String& ucommon::String::operator *= ( size_t  number)

Delete a specified number of characters from start of string.

Parameters
numberof characters to delete.

◆ operator bool()

ucommon::String::operator bool ( ) const

Test if string has data.

Returns
true if string has data.

◆ operator const char *()

ucommon::String::operator const char * ( ) const
inline

Casting reference to raw text string.

Returns
null terminated text of string.

Definition at line 662 of file string.h.

◆ operator!()

bool ucommon::String::operator! ( ) const

Test if string is empty.

Returns
true if string is empty.

◆ operator!=()

bool ucommon::String::operator!= ( const char *  text) const

Compare our object with null terminated text.

Compare method is used.

Parameters
textto compare with.
Returns
true if we are not equal.

◆ operator%() [1/6]

String& ucommon::String::operator% ( short &  value)

Parse short integer value from a string.

Parameters
valueto store.
Returns
object in expression.

◆ operator%() [2/6]

String& ucommon::String::operator% ( unsigned short &  value)

Parse short integer value from a string.

Parameters
valueto store.
Returns
object in expression.

◆ operator%() [3/6]

String& ucommon::String::operator% ( long &  value)

Parse long integer value from a string.

Parameters
valueto store.
Returns
object in expression.

◆ operator%() [4/6]

String& ucommon::String::operator% ( unsigned long &  value)

Parse long integer value from a string.

Parameters
valueto store.
Returns
object in expression.

◆ operator%() [5/6]

String& ucommon::String::operator% ( double &  value)

Parse double value from a string.

Parameters
valueto store.
Returns
object in expression.

◆ operator%() [6/6]

String& ucommon::String::operator% ( const char *  text)

Parse text from a string in a scan expression.

Parameters
textto scan and bypass.
Returns
object in expression.

◆ operator()() [1/2]

String ucommon::String::operator() ( int  offset,
size_t  size 
) const

Get a new substring through object expression.

Parameters
offsetof substring.
sizeof substring or 0 if to end.
Returns
string object holding substring.

◆ operator()() [2/2]

const char* ucommon::String::operator() ( int  offset) const

Reference a string in the object by relative offset.

Positive offsets are from the start of the string, negative from the end.

Parameters
offsetto string position.
Returns
pointer to string data or NULL if invalid offset.

◆ operator+()

const String ucommon::String::operator+ ( const char *  text) const

Concatenate null terminated text to our object.

Parameters
textto concatenate.

◆ operator+=() [1/2]

String& ucommon::String::operator+= ( const char *  text)

Concatenate text to an existing string object.

Parameters
textto add.
Returns
our object for expression use.

◆ operator+=() [2/2]

String& ucommon::String::operator+= ( size_t  number)

Delete a specified number of characters from start of string.

Parameters
numberof characters to delete.

◆ operator-=()

String& ucommon::String::operator-= ( size_t  number)

Delete a specified number of characters from end of string.

Parameters
numberof characters to delete.

◆ operator<()

bool ucommon::String::operator< ( const char *  text) const

Compare our object with null terminated text.

Compare method is used.

Parameters
textto compare with.
Returns
true if we are less than text.

◆ operator<=()

bool ucommon::String::operator<= ( const char *  text) const

Compare our object with null terminated text.

Compare method is used.

Parameters
textto compare with.
Returns
true if we are less than or equal to text.

◆ operator=() [1/2]

String& ucommon::String::operator= ( const String object)

Assign our string with the cstring of another object.

If we had an active string reference, it is released. The object now has a duplicate reference to the cstring of the other object.

Parameters
objectto assign from.

◆ operator=() [2/2]

String& ucommon::String::operator= ( const char *  text)

Assign text to our existing buffer.

This performs a set method.

Parameters
textto assign from.

◆ operator==()

bool ucommon::String::operator== ( const char *  text) const

Compare our object with null terminated text.

Parameters
textto compare with.
Returns
true if we are equal.

◆ operator>()

bool ucommon::String::operator> ( const char *  text) const

Compare our object with null terminated text.

Compare method is used.

Parameters
textto compare with.
Returns
true if we are greater than text.

◆ operator>=()

bool ucommon::String::operator>= ( const char *  text) const

Compare our object with null terminated text.

Compare method is used.

Parameters
textto compare with.
Returns
true if we are greater than or equal to text.

◆ operator[]()

const char ucommon::String::operator[] ( int  offset) const

Reference a single character in string object by array offset.

Parameters
offsetto character.
Returns
character value at offset.

◆ operator^=() [1/2]

String& ucommon::String::operator^= ( const String object)

Create new cow instance and assign value from another string object.

Parameters
objectto assign from.
Returns
our object for expression use.

◆ operator^=() [2/2]

String& ucommon::String::operator^= ( const char *  text)

Create new cow instance and assign value from null terminated text.

Parameters
textto assign from.
Returns
our object for expression use.

◆ operator|()

String& ucommon::String::operator| ( const char *  text)

Concatenate null terminated text to our object.

This creates a new copy-on-write instance to hold the concatenated string. This will eventually replace '+' when + creates a new string instance instead.

Parameters
textto concatenate.

◆ operator|=()

String& ucommon::String::operator|= ( const char *  text)

Concatenate text to an existing string object.

This will use the old behavior when +/= updated.

◆ paste()

void ucommon::String::paste ( size_t  offset,
const char *  text,
size_t  size = 0 
)

Insert (paste) text into string.

Parameters
offsetto start paste.
textto paste.
sizeof text to paste.
Examples
string.cpp.

◆ pos()

static const char* ucommon::String::pos ( const char *  text,
ssize_t  offset 
)
static

Compute position in string.

Parameters
textof string.
offsetfrom start, negative values from end.
Returns
pointer to string position.

◆ printf()

size_t ucommon::String::printf ( const char *  format,
  ... 
)

Print items into a string object.

Parameters
formatstring of print format.
Returns
number of bytes written to string.

◆ rchr()

const char* ucommon::String::rchr ( char  character) const

Find pointer in string where specified character last appears.

Parameters
characterto find.
Returns
string pointer for last occurrence of character if found, NULL if not.

◆ release()

virtual void ucommon::String::release ( void  )
protectedvirtual

Decrease retention of our reference counted cstring.

May be overridden for memstring which has fixed cstring object.

Implements ucommon::ObjectProtocol.

◆ resize()

virtual bool ucommon::String::resize ( size_t  size)
virtual

Resize and re-allocate string memory.

Parameters
sizeto allocate for string.
Returns
true if re-allocated. False in derived memstring.

◆ retain()

virtual void ucommon::String::retain ( void  )
protectedvirtual

Increase retention of our reference counted cstring.

May be overridden for memstring which has fixed cstring object.

Implements ucommon::ObjectProtocol.

◆ rfind() [1/2]

const char* ucommon::String::rfind ( const char *  list,
size_t  offset = npos 
) const

Find last occurrence of character in the string.

Parameters
listof characters to search for.
offsetto start of search. Default is end of string.
Returns
pointer to last occurrence of character.

◆ rfind() [2/2]

static char* ucommon::String::rfind ( char *  text,
const char *  list 
)
static

Find the last occurrence of a character in a text buffer.

Parameters
textbuffer to examine.
listof characters to search for.
Returns
pointer to last instance found or NULL.

◆ right()

String ucommon::String::right ( size_t  offset) const
inline

Convenience method for right of string.

Parameters
offsetof substring from right.
Returns
string object holding substring.

Definition at line 702 of file string.h.

◆ rset() [1/2]

void ucommon::String::rset ( const char *  text,
char  overflow,
size_t  offset,
size_t  size = 0 
)

Set a text field within our string object offset from the end of buffer.

Parameters
textto set.
overflowcharacter to use as filler if text is too short.
offsetfrom end of object string buffer to set text at.
sizeof part of buffer to set with text and overflow.

◆ rset() [2/2]

static char* ucommon::String::rset ( char *  buffer,
size_t  size,
const char *  text 
)
static

Set a field in a null terminated string relative to the end of text.

Parameters
bufferto modify.
sizeof field to set.
textto replace end of string with.
Returns
pointer to text buffer.

◆ rskip() [1/2]

const char* ucommon::String::rskip ( const char *  list,
size_t  offset = npos 
) const

Skip trailing characters in the string.

This searches the string in reverse order.

Parameters
listof characters to skip when found.
offsetto start of scan. Default is end of string.
Returns
pointer to first part of string before skipped characters.

◆ rskip() [2/2]

static char* ucommon::String::rskip ( char *  text,
const char *  list 
)
static

Skip before trailing characters in a null terminated string.

Parameters
textpointer to start at.
listof characters to skip when found.
Returns
pointer to last part of string past skipped characters.

◆ rsplit() [1/2]

void ucommon::String::rsplit ( const char *  pointer)

Split the string by a pointer position.

Everything before the pointer is removed.

Parameters
pointerto split position in string.

◆ rsplit() [2/2]

void ucommon::String::rsplit ( size_t  offset)

Split the string at a specific offset.

Everything before the offset is removed.

Parameters
offsetto split position in string.

◆ search()

const char* ucommon::String::search ( const char *  string,
unsigned  instance = 0,
unsigned  flags = 0 
) const

Search for a substring in the string.

Parameters
substringto search for.
flagsfor case insensitive search.

◆ seek()

static size_t ucommon::String::seek ( char *  text,
const char *  list 
)
static

Offset until next occurrence of character in a text or length.

Parameters
textbuffer to examine.
listof characters to search for.
Returns
offset to next occurrence or length of string.

◆ set() [1/5]

void ucommon::String::set ( const char *  text)

Set string object to text of a null terminated string.

Parameters
textstring to set.
Examples
string.cpp.

◆ set() [2/5]

void ucommon::String::set ( size_t  offset,
const char *  text,
size_t  size = 0 
)

Set a portion of the string object at a specified offset to a text string.

Parameters
offsetin object string buffer.
textto set at offset.
sizeof text area to set or 0 until end of text.

◆ set() [3/5]

void ucommon::String::set ( const char *  text,
char  overflow,
size_t  offset,
size_t  size = 0 
)

Set a text field within our string object.

Parameters
textto set.
overflowcharacter to use as filler if text is too short.
offsetin object string buffer to set text at.
sizeof part of buffer to set with text and overflow.

◆ set() [4/5]

static char* ucommon::String::set ( char *  buffer,
size_t  size,
const char *  text 
)
static

Safely set a null terminated string buffer in memory.

If the text is too large to fit into the buffer, it is truncated to the size.

Parameters
bufferto set.
sizeof buffer. Includes null byte at end of string.
textto set in buffer.
Returns
pointer to text buffer.

◆ set() [5/5]

static char* ucommon::String::set ( char *  buffer,
size_t  size,
const char *  text,
size_t  max 
)
static

Safely set a null terminated string buffer in memory.

If the text is too large to fit into the buffer, it is truncated to the size.

Parameters
bufferto set.
sizeof buffer. Includes null byte at end of string.
textto set in buffer.
maxsize of text to set.
Returns
pointer to text buffer.

◆ size()

size_t ucommon::String::size ( void  ) const

Get the size of currently allocated space for string.

Returns
size allocated for text.

◆ skip() [1/2]

const char* ucommon::String::skip ( const char *  list,
size_t  offset = 0 
) const

Skip lead characters in the string.

Parameters
listof characters to skip when found.
offsetto start of scan.
Returns
pointer to first part of string past skipped characters.

◆ skip() [2/2]

static char* ucommon::String::skip ( char *  text,
const char *  list 
)
static

Skip after lead characters in a null terminated string.

Parameters
textpointer to start at.
listof characters to skip when found.
Returns
pointer to first part of string past skipped characters.

◆ split() [1/2]

void ucommon::String::split ( const char *  pointer)

Split the string by a pointer position.

Everything after the pointer is removed.

Parameters
pointerto split position in string.

◆ split() [2/2]

void ucommon::String::split ( size_t  offset)

Split the string at a specific offset.

Everything after the offset is removed.

Parameters
offsetto split position in string.

◆ strip() [1/2]

void ucommon::String::strip ( const char *  list)

Strip lead and trailing characters from the string.

Parameters
listof characters to remove.
Examples
string.cpp.

◆ strip() [2/2]

static char* ucommon::String::strip ( char *  text,
const char *  list 
)
static

Skip lead and remove trailing characters from a text string.

This function will modify memory.

Parameters
textbuffer to examine.
listof characters to trim and chop.
Returns
position in text past lead trim.

◆ swap()

static void ucommon::String::swap ( String object1,
String object2 
)
static

Swap the cstring references between two strings.

Parameters
object1to swap.
object2to swap.

◆ tod() [1/2]

double ucommon::String::tod ( char **  pointer = NULL)
inline

Convert string to a double value.

Parameters
objectto convert.
pointerto update with end of parsed value.
Returns
double value of object.

Definition at line 1272 of file string.h.

◆ tod() [2/2]

static double ucommon::String::tod ( const char *  text,
char **  pointer = NULL 
)
inlinestatic

Convert text to a double value.

Parameters
textto convert.
pointerto update with end of parsed value.
Returns
double value of object.

Definition at line 1292 of file string.h.

◆ token() [1/2]

static char* ucommon::String::token ( char *  text,
char **  last,
const char *  list,
const char *  quote = NULL,
const char *  end = NULL 
)
static

A thread-safe token parsing routine for null terminated strings.

This is related to strtok, but with safety checks for NULL values and a number of enhancements including support for quoted text that may contain token separators within quotes. The text string is modified as it is parsed.

Parameters
textstring to examine for tokens.
lasttoken position or set to NULL for start of string.
listof characters to use as token separators.
quotepairs of characters for quoted text or NULL if not used.
endof line marker characters or NULL if not used.
Returns
token extracted from string or NULL if no more tokens found.
Examples
string.cpp.

◆ token() [2/2]

char* ucommon::String::token ( char **  last,
const char *  list,
const char *  quote = NULL,
const char *  end = NULL 
)
inline

A thread-safe token parsing routine for strings objects.

This is related to strtok, but with safety checks for NULL values and a number of enhancements including support for quoted text that may contain token separators within quotes. The object is modified as it is parsed.

Parameters
lasttoken position or set to NULL for start of string.
listof characters to use as token separators.
quotepairs of characters for quoted text or NULL if not used.
endof line marker characters or NULL if not used.
Returns
token extracted from string or NULL if no more tokens found.

Definition at line 1262 of file string.h.

◆ tol() [1/2]

long ucommon::String::tol ( char **  pointer = NULL)
inline

Convert string to a long value.

Parameters
objectto convert.
pointerto update with end of parsed value.
Returns
long value of object.

Definition at line 1282 of file string.h.

◆ tol() [2/2]

static long ucommon::String::tol ( const char *  text,
char **  pointer = NULL 
)
inlinestatic

Convert text to a long value.

Parameters
textto convert.
pointerto update with end of parsed value.
Returns
long value of object.

Definition at line 1302 of file string.h.

◆ trim() [1/3]

void ucommon::String::trim ( const char *  list)

Trim lead characters from the string.

Parameters
listof characters to remove.

◆ trim() [2/3]

void ucommon::String::trim ( size_t  count = 1)
inline

Trim lead characters from text.

Parameters
countof characters to remove.

Definition at line 440 of file string.h.

◆ trim() [3/3]

static char* ucommon::String::trim ( char *  text,
const char *  list 
)
static

Return start of string after characters to trim from beginning.

This function does not modify memory.

Parameters
textbuffer to examine.
listof characters to skip from start.
Returns
position in text past lead trim.

◆ unquote() [1/2]

bool ucommon::String::unquote ( const char *  quote)

Unquote a quoted string.

Removes lead and trailing quote marks.

Parameters
quotepairs of characters for open and close quote.
Returns
true if string was quoted.

◆ unquote() [2/2]

static char* ucommon::String::unquote ( char *  text,
const char *  quote 
)
static

Unquote a quoted null terminated string.

Returns updated string position and replaces trailing quote with null byte if quoted.

Parameters
textto examine.
quotepairs of character for open and close quote.
Returns
new text pointer if quoted, NULL if unchanged.

◆ upper()

static void ucommon::String::upper ( char *  text)
static

Convert null terminated text to upper case.

Parameters
textto convert.

◆ vprintf()

size_t ucommon::String::vprintf ( const char *  format,
va_list  args 
)

Print items into a string object.

Parameters
formatstring of print format.
argslist to print.
Returns
number of bytes written to string.

Field Documentation

◆ str

cstring* ucommon::String::str
protected

cstring instance our object references.

Definition at line 209 of file string.h.


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