36#ifndef _UCOMMON_STRING_H_
37#define _UCOMMON_STRING_H_
39#ifndef _UCOMMON_CPR_H_
43#ifndef _UCOMMON_GENERICS_H_
47#ifndef _UCOMMON_PROTOCOLS_H_
51#ifndef _UCOMMON_OBJECT_H_
63#define PGP_B64_WIDTH 64
64#define MIME_B64_WIDTH 76
105 __DELETE_COPY(regex);
108 regex(
const char *pattern,
size_t size = 1);
109 regex(
size_t size = 1);
112 size_t offset(
unsigned member);
113 size_t size(
unsigned member);
115 inline size_t members(
void)
const {
119 bool match(
const char *text,
unsigned flags = 0);
121 regex& operator=(
const char *
string);
123 bool operator*=(
const char *
string);
125 operator bool()
const {
126 return object != NULL;
129 bool operator!()
const {
130 return object == NULL;
137 __DELETE_COPY(cstring);
140 virtual void dealloc(
void) __OVERRIDE;
154 cstring(
size_t size);
160 void clear(
size_t offset);
168 void set(
size_t offset,
const char *text,
size_t size);
174 void set(
const char *text);
180 void add(
const char *text);
186 void add(
char character);
198 void inc(
size_t number);
205 void dec(
size_t number);
226 virtual int compare(
const char *
string)
const;
234 bool equal(
const char *
string)
const;
260 virtual void cow(
size_t size = 0);
262 size_t getStringSize(
void)
const;
265 const static size_t npos = ((size_t)-1);
266 const static char eos =
'\0';
286 String(
size_t size,
const char *format, ...);
309 String(
const char *text,
const char *end);
337 int scanf(
const char *format, ...) __SCANF(2, 3);
345 int vscanf(const
char *format, va_list args) __SCANF(2, 0);
352 size_t printf(const
char *format, ...) __PRINTF(2, 3);
360 size_t vprintf(const
char *format, va_list args) __PRINTF(2, 0);
368 inline
char *c_mem() {
389 void set(
const char *text);
398 void set(
size_t offset,
const char *text,
size_t size = 0);
407 void set(
const char *text,
char overflow,
size_t offset,
size_t size = 0);
416 void rset(
const char *text,
char overflow,
size_t offset,
size_t size = 0);
422 void add(
const char *text);
440 inline void trim(
size_t count = 1) {
454 inline void chop(
size_t count = 1) {
476 void cut(
size_t offset,
size_t size = 0);
484 void paste(
size_t offset,
const char *text,
size_t size = 0);
547 char at(
int position)
const;
559 const char *
end(
void)
const;
567 const char *
skip(
const char *list,
size_t offset = 0)
const;
576 const char *
rskip(
const char *list,
size_t offset = npos)
const;
583 const char *
search(
const char *
string,
unsigned instance = 0,
unsigned flags = 0)
const;
585 const char *search(regex& expr,
unsigned instance = 0,
unsigned flags = 0)
const;
587 unsigned replace(
const char *
string,
const char *text = NULL,
unsigned flags = 0);
589 unsigned replace(regex& expr,
const char *text = NULL,
unsigned flags = 0);
597 const char *
find(
const char *list,
size_t offset = 0)
const;
605 const char *
rfind(
const char *list,
size_t offset = npos)
const;
621 void fill(
size_t size,
char fill);
642 const char *
chr(
char character)
const;
650 const char *
rchr(
char character)
const;
662 inline operator const char *()
const {
694 return operator()(0, size);
703 return operator()(-((
int)offset), 0);
713 return operator()((
int)offset, size);
742 operator bool()
const;
757 String& operator&=(
const char *text);
803 bool operator*=(
const char *substring);
805 bool operator*=(regex& expr);
883 inline String& operator<<(
const char *text) {
884 add(text);
return *
this;
887 inline String& operator<<(
char code) {
888 add(code);
return *
this;
953 static bool check(
const char *
string,
size_t maximum,
size_t minimum = 0);
986 static char *
token(
char *text,
char **last,
const char *list,
const char *quote = NULL,
const char *end = NULL);
994 static char *
skip(
char *text,
const char *list);
1002 static char *
rskip(
char *text,
const char *list);
1011 static char *
unquote(
char *text,
const char *quote);
1020 static char *
rset(
char *buffer,
size_t size,
const char *text);
1030 static char *
set(
char *buffer,
size_t size,
const char *text);
1041 static char *
set(
char *buffer,
size_t size,
const char *text,
size_t max);
1052 static char *
add(
char *buffer,
size_t size,
const char *text);
1064 static char *
add(
char *buffer,
size_t size,
const char *text,
size_t max);
1073 static const char *
ifind(
const char *text,
const char *key,
const char *optional);
1082 static const char *
find(
const char *text,
const char *key,
const char *optional);
1097 static int compare(
const char *text1,
const char *text2);
1099 inline static int collate(
const char *text1,
const char *text2) {
1100 return compare(text1, text2);
1109 static bool equal(
const char *text1,
const char *text2);
1118 static int compare(
const char *text1,
const char *text2,
size_t size);
1127 static bool equal(
const char *text1,
const char *text2,
size_t size);
1135 static bool eq_case(
const char *text1,
const char *text2);
1144 static bool eq_case(
const char *text1,
const char *text2,
size_t size);
1153 static char *
trim(
char *text,
const char *list);
1162 static char *
chop(
char *text,
const char *list);
1171 static char *
strip(
char *text,
const char *list);
1181 static char *
fill(
char *text,
size_t size,
char character);
1189 static unsigned ccount(
const char *text,
const char *list);
1197 static char *
find(
char *text,
const char *list);
1205 static size_t seek(
char *text,
const char *list);
1213 static char *
rfind(
char *text,
const char *list);
1220 static char *
dup(
const char *text);
1228 static char *
left(
const char *text,
size_t size);
1236 static const char *
pos(
const char *text, ssize_t offset);
1238 inline static char *right(
const char *text,
size_t size) {
1239 return dup(pos(text, -(ssize_t)size));
1242 inline static char *
copy(
const char *text,
size_t offset,
size_t len) {
1243 return left(pos(text, (ssize_t)offset), len);
1246 static void cut(
char *text,
size_t offset,
size_t len);
1248 static void paste(
char *text,
size_t max,
size_t offset,
const char *data,
size_t len = 0);
1262 inline char *
token(
char **last,
const char *list,
const char *quote = NULL,
const char *end = NULL) {
1263 return token(data(), last, list, quote, end);
1273 return strtod(data(),
pointer);
1283 return strtol(data(),
pointer, 0);
1292 inline static double tod(
const char *text,
char **
pointer = NULL) {
1302 inline static long tol(
const char *text,
char **
pointer = NULL) {
1303 return strtol(text,
pointer, 0);
1314 static size_t b64size(
size_t size);
1324 static size_t b64encode(
char *
string,
const uint8_t *binary,
size_t size,
size_t width = 0);
1334 static size_t b64decode(uint8_t *binary,
const char *
string,
size_t size,
bool ws =
false);
1336 static size_t b64count(
const char *str,
bool ws =
false);
1344 static uint32_t
crc24(uint8_t *binary,
size_t size);
1352 static uint16_t
crc16(uint8_t *binary,
size_t size);
1369 static size_t hexdump(
const uint8_t *binary,
char *
string,
const char *format);
1379 static size_t hexpack(uint8_t *binary,
const char *
string,
const char *format);
1381 static size_t hex2bin(
const char *
string, uint8_t *binary,
size_t maxsize,
bool wsflag =
false);
1383 static size_t hexsize(
const char *format);
1385 static size_t hexcount(
const char *str,
bool ws =
false);
1398 const static size_t header =
sizeof(String::cstring);
1401 bool resize(
size_t size) __FINAL;
1402 void cow(
size_t adj = 0) __FINAL;
1403 void release(
void) __FINAL;
1414 set(
object.c_str());
1509 inline operator bool()
const {
1518 return buffer[0] == 0;
1525 inline operator char *() {
1544 __THROW_RANGE(
"charbuf offset");
1545 return buffer[offset];
1555 __THROW_RANGE(
"charbuf range");
1556 return buffer + offset;
1571 inline size_t len(
void)
const {
1572 return strlen(buffer);
1581typedef String::regex stringex_t;
1597 char buffer[
sizeof(cstring) + S];
1636inline bool eq(
char const *s1,
char const *s2) {
1640inline bool ne(
char const *s1,
char const *s2) {
1651inline bool eq(
char const *s1,
char const *s2,
size_t size) {
1655inline bool ne(
char const *s1,
char const *s2,
size_t size) {
1672inline bool ne(String &s1, String &s2) {
1673 return s1.compare(s2) != 0;
1676inline bool lt(String &s1,
const char *s2) {
1677 return s1.compare(s2) < 0;
1680inline bool gt(String &s1,
const char *s2) {
1681 return s1.compare(s2) > 0;
1684inline bool le(String &s1,
const char *s2) {
1685 return s1.compare(s2) <= 0;
1688inline bool ge(String &s1,
const char *s2) {
1689 return s1.compare(s2) >= 0;
1699inline bool eq_case(
char const *s1,
char const *s2)
1702inline bool ne_case(
char const *s1,
char const *s2)
1713inline bool eq_case(
char const *s1,
char const *s2,
size_t size) {
1717inline String str(
const char *
string) {
1718 return (String)string;
1721inline String str(String&
string) {
1722 return (String)string;
1725inline String str(
short value) {
1726 String temp(16,
"%hd", value);
return temp;
1729inline String str(
unsigned short value) {
1730 String temp(16,
"%hu", value);
return temp;
1733inline String str(
long value) {
1734 String temp(32,
"%ld", value);
return temp;
1737inline String str(
unsigned long value) {
1738 String temp(32,
"%lu", value);
return temp;
1741inline String str(
double value) {
1742 String temp(40,
"%f", value);
return temp;
1750class __EXPORT strdup_t
1760 inline strdup_t(
char *str) {
1764 inline ~strdup_t() {
1769 inline strdup_t& operator=(
char *str) {
1776 inline operator bool()
const {
1777 return data !=
nullptr;
1780 inline bool operator!()
const {
1781 return data ==
nullptr;
1784 inline operator char*()
const {
1788 inline const char *c_str(
void)
const {
1792 inline const char *operator*()
const {
1796 inline char& operator[](
int size) {
1800 inline char *operator+(
size_t size) {
Abstract interfaces and support.
Generic templates for C++.
Common namespace for all ucommon objects.
String string_t
A convenience type for string.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
T & max(T &o1, T &o2)
Convenience function to return max of two objects.
bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two socket addresses to see if equal.
T copy(const T &src)
Convenience function to copy objects.
bool eq_case(char const *s1, char const *s2)
Compare two null terminated strings if equal ignoring case.
Generic smart pointer class.
Mempager managed type factory for pager pool objects.
A base class for reference counted objects.
A common base class for all managed objects.
A copy-on-write string class that operates by reference count.
static void fix(String &object)
Fix and reset string object filler.
static bool equal(const char *text1, const char *text2)
Simple equal test for strings.
size_t offset(const char *pointer) const
Find offset of a pointer into our string buffer.
const char * rskip(const char *list, size_t offset=npos) const
Skip trailing characters in the string.
static size_t hexpack(uint8_t *binary, const char *string, const char *format)
Pack hex data from a string buffer.
static long tol(const char *text, char **pointer=NULL)
Convert text to a long value.
static size_t count(const char *text)
Safe version of strlen function.
virtual void cow(size_t size=0)
Copy on write operation for cstring.
String & operator%(const char *text)
Parse text from a string in a scan expression.
cstring * str
cstring instance our object references.
static int compare(const char *text1, const char *text2)
Safe string collation function.
const char * skip(const char *list, size_t offset=0) const
Skip lead characters in the string.
String(const char *text, const char *end)
Create a string for a substring.
String & operator-=(size_t number)
Delete a specified number of characters from end of string.
static int compare(const char *text1, const char *text2, size_t size)
Depreciated string comparison function.
void add(const char *text)
Append null terminated text to our string buffer.
String & operator%(short &value)
Parse short integer value from a string.
void split(const char *pointer)
Split the string by a pointer position.
static char * rfind(char *text, const char *list)
Find the last occurrence of a character in a text buffer.
void chop(size_t count=1)
Chop trailing characters from text.
String & operator%(unsigned short &value)
Parse short integer value from a string.
void clear(void)
Clear string by setting to empty.
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.
void rsplit(size_t offset)
Split the string at a specific offset.
void set(const char *text, char overflow, size_t offset, size_t size=0)
Set a text field within our string object.
void set(const char *text)
Set string object to text of a null terminated string.
static char * fill(char *text, size_t size, char character)
Fill a section of memory with a fixed text character.
String & operator&(const char *text)
Concatenate null terminated text to our object.
static double tod(const char *text, char **pointer=NULL)
Convert text to a double value.
const char * operator()(int offset) const
Reference a string in the object by relative offset.
String & operator^=(const char *text)
Create new cow instance and assign value from null terminated text.
String get(size_t offset, size_t size=0) const
Get a new string object as a substring of the current object.
static char * add(char *buffer, size_t size, const char *text)
Safely append a null terminated string into an existing string in memory.
size_t len(void) const
Get length of string.
const char * rchr(char character) const
Find pointer in string where specified character last appears.
String(size_t size)
Create an empty string with a buffer pre-allocated to a specified size.
String & operator++(void)
Delete first character from string.
String & operator+=(const char *text)
Concatenate text to an existing string object.
void add(char character)
Append a single character to our string buffer.
static char * skip(char *text, const char *list)
Skip after lead characters in a null terminated string.
static char * dup(const char *text)
Duplicate null terminated text into the heap.
char * token(char **last, const char *list, const char *quote=NULL, const char *end=NULL)
A thread-safe token parsing routine for strings objects.
void clear(size_t offset)
Clear a field of a filled string with filler.
bool operator!() const
Test if string is empty.
bool unquote(const char *quote)
Unquote a quoted string.
const char * begin(void) const
Get pointer to first character in string for iteration.
virtual bool resize(size_t size)
Resize and re-allocate string memory.
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.
long tol(char **pointer=NULL)
Convert string to a long value.
static void lower(char *text)
Convert null terminated text to lower case.
virtual ~String()
Destroy string.
void split(size_t offset)
Split the string at a specific offset.
double tod(char **pointer=NULL)
Convert string to a double value.
String(const char *text, size_t size)
Create a string from null terminated text up to a maximum specified size.
virtual void retain(void)
Increase retention of our reference counted cstring.
static char * set(char *buffer, size_t size, const char *text)
Safely set a null terminated string buffer in memory.
static bool eq_case(const char *text1, const char *text2)
Simple case insensitive equal test for strings.
static size_t b64encode(char *string, const uint8_t *binary, size_t size, size_t width=0)
Standard radix 64 encoding.
String & operator%(double &value)
Parse double value from a string.
static void erase(char *text)
Erase string memory.
static bool check(const char *string, size_t maximum, size_t minimum=0)
Check if string is valid and in specific constraints.
void lower(void)
Convert string to lower case.
String & operator%(long &value)
Parse long integer value from a string.
const char * chr(char character) const
Find pointer in string where specified character appears.
void strip(const char *list)
Strip lead and trailing characters from the string.
static void upper(char *text)
Convert null terminated text to upper case.
String()
Create a new empty string object.
static char * unquote(char *text, const char *quote)
Unquote a quoted null terminated string.
String(size_t size, const char *format,...)
Create a string by printf-like formating into a pre-allocated space of a specified size.
String(const String &existing)
Construct a copy of a string object.
static String b64(const uint8_t *binary, size_t size)
Standard radix 64 string encoding.
char at(int position) const
Return character found at a specific position in the string.
int scanf(const char *format,...)
Scan input items from a string object.
void trim(const char *list)
Trim lead characters from the string.
String & operator^=(const String &object)
Create new cow instance and assign value from another string object.
const char operator[](int offset) const
Reference a single character in string object by array offset.
String & operator=(const char *text)
Assign text to our existing buffer.
String(const char *text)
Create a string from null terminated text.
bool operator>=(const char *text) const
Compare our object with null terminated text.
bool operator==(const char *text) const
Compare our object with null terminated text.
String & operator|=(const char *text)
Concatenate text to an existing string object.
void upper(void)
Convert string to upper case.
String right(size_t offset) const
Convenience method for right of string.
size_t count(void) const
Count all characters in the string (strlen).
static uint32_t crc24(uint8_t *binary, size_t size)
24 bit crc as used in openpgp.
static bool equal(const char *text1, const char *text2, size_t size)
Simple equal test for strings.
bool full(void) const
Test if the string's allocated space is all used up.
static const char * find(const char *text, const char *key, const char *optional)
Find position of substring within a string.
String left(size_t size) const
Convenience method for left of string.
static char * rskip(char *text, const char *list)
Skip before trailing characters in a null terminated string.
void cut(size_t offset, size_t size=0)
Cut (remove) text from string.
bool equal(const char *string) const
Test if two string values are equal.
const char * rfind(const char *list, size_t offset=npos) const
Find last occurrence of character in the string.
String & operator|(const char *text)
Concatenate null terminated text to our object.
bool operator<=(const char *text) const
Compare our object with null terminated text.
String operator()(int offset, size_t size) const
Get a new substring through object expression.
static char * set(char *buffer, size_t size, const char *text, size_t max)
Safely set a null terminated string buffer in memory.
virtual void release(void)
Decrease retention of our reference counted cstring.
String & operator+=(size_t number)
Delete a specified number of characters from start of string.
static String hex(const uint8_t *binary, size_t size)
Convert binary data buffer into hex string.
static bool eq_case(const char *text1, const char *text2, size_t size)
Simple case insensitive equal test for strings.
static void swap(String &object1, String &object2)
Swap the cstring references between two strings.
const String operator+(const char *text) const
Concatenate null terminated text to our object.
bool operator!=(const char *text) const
Compare our object with null terminated text.
const char * operator*() const
Reference raw text buffer by pointer operator.
static const char * ifind(const char *text, const char *key, const char *optional)
Find position of case insensitive substring within a string.
static size_t b64decode(uint8_t *binary, const char *string, size_t size, bool ws=false)
Standard radix 64 decoding.
size_t ccount(const char *list) const
Count number of occurrences of characters in string.
virtual int compare(const char *string) const
Compare the values of two string.
void rsplit(const char *pointer)
Split the string by a pointer position.
String & operator%(unsigned long &value)
Parse long integer value from a string.
static size_t seek(char *text, const char *list)
Offset until next occurrence of character in a text or length.
static char * strip(char *text, const char *list)
Skip lead and remove trailing characters from a text string.
static unsigned ccount(const char *text, const char *list)
Count instances of characters in a list in a text buffer.
void chop(const char *list)
Chop trailing characters from the string.
static const char * pos(const char *text, ssize_t offset)
Compute position in string.
String & operator*=(size_t number)
Delete a specified number of characters from start of string.
const char * c_str(void) const
Get character text buffer of string object.
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.
void paste(size_t offset, const char *text, size_t size=0)
Insert (paste) text into string.
static char * chop(char *text, const char *list)
Strip trailing characters from the text string.
const char * find(const char *list, size_t offset=0) const
Find a character in the string.
String & operator--(void)
Delete last character from string.
static char * left(const char *text, size_t size)
Duplicate null terminated text of specific size to heap.
static size_t hexdump(const uint8_t *binary, char *string, const char *format)
Dump hex data to a string buffer.
static char * trim(char *text, const char *list)
Return start of string after characters to trim from beginning.
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.
static uint16_t crc16(uint8_t *binary, size_t size)
ccitt 16 bit crc for binary data.
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.
static char * find(char *text, const char *list)
Find the first occurrence of a character in a text buffer.
void erase(void)
Erase string memory.
String copy(size_t offset, size_t size) const
Convenience method for substring extraction.
const char * end(void) const
Get pointer to last character in string for iteration.
void trim(size_t count=1)
Trim lead characters from text.
const char * search(const char *string, unsigned instance=0, unsigned flags=0) const
Search for a substring in the string.
size_t size(void) const
Get the size of currently allocated space for string.
String & operator=(const String &object)
Assign our string with the cstring of another object.
virtual cstring * c_copy(void) const
Return cstring to use in copy constructors.
cstring * create(size_t size) const
Factory create a cstring object of specified size.
bool operator<(const char *text) const
Compare our object with null terminated text.
bool operator>(const char *text) const
Compare our object with null terminated text.
A string class that uses a cstring buffer that is fixed in memory.
void operator=(String &object)
Assign the text of a string to our object.
memstring(void *memory, size_t size)
Create an instance of a memory string.
cstring * c_copy(void) const
Return cstring to use in copy constructors.
static memstring * create(MemoryProtocol *pager, size_t size)
Create a memory string with memory allocated from a pager.
static memstring * create(size_t size)
Create a memory string with memory allocated from the heap.
~memstring()
Destroy memory string.
void operator=(const char *text)
Assign null terminated text to our object.
A template to create a character array that can be manipulated as a string.
size_t len(void) const
Get current length of string.
char & operator[](size_t offset) const
Array operator to get a character from the object.
charbuf()
Create a new character buffer with an empty string.
charbuf(const charbuf ©)
Copy constructor.
size_t size(void) const
Get allocated size of the object.
void operator+=(const char *text)
Concatenate text into the object.
char * operator*()
Get text by object pointer reference.
charbuf(const char *text)
Create a character buffer with assigned text.
bool operator!() const
Test if the object is empty.
char * operator()(size_t offset)
Get a pointer to an offset in the object by expression operator.
void operator=(const char *text)
Assign null terminated text to the object.
A string class that has a predefined string buffer.
void operator=(const char *text)
Assign a string buffer from a null terminated string.
stringbuf()
Create an empty instance of a string buffer.
void operator=(String &object)
Assign a string buffer from another string object.
stringbuf(const char *text)
Create a string buffer from a null terminated string.
A common object base class with auto-pointer support.
A common string class and character string support functions.