libcmutils 0.6.5
Multi-platform C99 utility library
Loading...
Searching...
No Matches
Strings, string arrays, byte buffers and charset conversion.

Data Structures

struct  CMUTIL_String
 Multifunctional string type. More...
 
struct  CMUTIL_StringArray
 Dynamic array of string objects. More...
 
struct  CMUTIL_ByteBuffer
 Manipulation of bytes. More...
 
struct  CMUTIL_CSConv
 Character set conversion object. More...
 

Macros

#define CMUTIL_StringCreate()    CMUTIL_StringCreateEx(CMUTIL_STRING_DEFAULT, NULL)
 Creates a string object.
 
#define CMUTIL_STRINGARRAY_DEFAULT   32
 
#define CMUTIL_StringArrayCreate()    CMUTIL_StringArrayCreateEx(CMUTIL_STRINGARRAY_DEFAULT)
 
#define CMUTIL_StringArrayCreateWith(s, ...)    CMUTIL_StringArrayCreateWithEx(s, __VA_ARGS__, NULL)
 Create a new CMUTIL_StringArray object with initial elements.
 
#define CMUTIL_BYTEBUFFER_DEFAULT   32
 
#define CMUTIL_ByteBufferCreate()    CMUTIL_ByteBufferCreateEx(CMUTIL_BYTEBUFFER_DEFAULT)
 

Functions

CMUTIL_StringCMUTIL_StringCreateEx (size_t initcapacity, const char *initcontent)
 Creates a string object with initial capacity and content.
 
CMUTIL_StringArrayCMUTIL_StringArrayCreateEx (size_t initcapacity)
 
CMUTIL_StringArrayCMUTIL_StringArrayCreateWithEx (const char *str,...)
 Create a new CMUTIL_StringArray object with initial elements.
 
char * CMUTIL_StrRTrim (char *inp)
 Trim the right side of a c-style string.
 
char * CMUTIL_StrLTrim (char *inp)
 Trim the left side of a c-style string.
 
char * CMUTIL_StrTrim (char *inp)
 Trim the both side of a c-style string.
 
const char * CMUTIL_StrNextToken (char *dest, size_t buflen, const char *src, const char *delims)
 Get the next token from src.
 
const char * CMUTIL_StrSkipSpaces (const char *src, const char *spaces)
 Skip leading spaces.
 
CMUTIL_StringArrayCMUTIL_StringSplit (const char *haystack, const char *needle)
 Split the haystack string using the needle.
 
int CMUTIL_StringHexToBytes (uint8_t *dest, const char *src, int len)
 Convert hex encoded string to bytes.
 
CMUTIL_ByteBufferCMUTIL_ByteBufferCreateEx (size_t initcapacity)
 Creates CMUTIL_ByteBuffer object with initial capacity.
 
CMUTIL_CSConvCMUTIL_CSConvCreate (const char *fromcs, const char *tocs)
 Create a character set conversion object.
 

Detailed Description

Macro Definition Documentation

◆ CMUTIL_StringCreate

#define CMUTIL_StringCreate ( )     CMUTIL_StringCreateEx(CMUTIL_STRING_DEFAULT, NULL)

Creates a string object.

Returns
A new string object.

◆ CMUTIL_STRINGARRAY_DEFAULT

#define CMUTIL_STRINGARRAY_DEFAULT   32

Default initial size of CMUTIL_String object.

◆ CMUTIL_StringArrayCreate

#define CMUTIL_StringArrayCreate ( )     CMUTIL_StringArrayCreateEx(CMUTIL_STRINGARRAY_DEFAULT)

Creates a new CMUTIL_String object.

Returns
A new CMUTIL_String object.

◆ CMUTIL_StringArrayCreateWith

#define CMUTIL_StringArrayCreateWith (   s,
  ... 
)     CMUTIL_StringArrayCreateWithEx(s, __VA_ARGS__, NULL)

Create a new CMUTIL_StringArray object with initial elements.

A convenient function to create a CMUTIL_StringArray with initial elements without the last NULL.

Parameters
sThe first initial element of the array.
...Additional initial elements.
Returns
A new CMUTIL_StringArray object with initial elements.

◆ CMUTIL_BYTEBUFFER_DEFAULT

#define CMUTIL_BYTEBUFFER_DEFAULT   32

Default bytebuffer initial capacity.

◆ CMUTIL_ByteBufferCreate

#define CMUTIL_ByteBufferCreate ( )     CMUTIL_ByteBufferCreateEx(CMUTIL_BYTEBUFFER_DEFAULT)

Creates CMUTIL_ByteBuffer object with the default parameter.

Function Documentation

◆ CMUTIL_StringCreateEx()

CMUTIL_String * CMUTIL_StringCreateEx ( size_t  initcapacity,
const char *  initcontent 
)

Creates a string object with initial capacity and content.

Parameters
initcapacityInitial capacity of this string object.
initcontentInitial content of this string object.
Returns
A new string object.

◆ CMUTIL_StringArrayCreateEx()

CMUTIL_StringArray * CMUTIL_StringArrayCreateEx ( size_t  initcapacity)

Creates a new CMUTIL_String object with initial capacity.

Parameters
initcapacityInitial capacity of the new CMUTIL_String object.
Returns
A new CMUTIL_String object.

◆ CMUTIL_StringArrayCreateWithEx()

CMUTIL_StringArray * CMUTIL_StringArrayCreateWithEx ( const char *  str,
  ... 
)

Create a new CMUTIL_StringArray object with initial elements.

Parameters
strThe first initial element of the array.
...Additional initial elements, the last element must be NULL.
Returns
A new CMUTIL_StringArray object with initial elements.

◆ CMUTIL_StrRTrim()

char * CMUTIL_StrRTrim ( char *  inp)

Trim the right side of a c-style string.

Trailing space characters (space, tab, line-feed, carriage-return) are removed from the input string. Input string must be null-terminated, and its contents could be changed after this function call. The result string reference is pointing to the original input string.

Parameters
inpInput string to be trimmed.
Returns
Right "trimmed" input string (pointing same as input).

◆ CMUTIL_StrLTrim()

char * CMUTIL_StrLTrim ( char *  inp)

Trim the left side of a c-style string.

Leading space characters (space, tab, line-feed, carriage-return) are removed from the input string. Input string must be null-terminated, and its contents could be changed after this function call. The result string reference is pointing to the original input string.

Parameters
inpInput string to be trimmed.
Returns
Right "trimmed" input string (pointing same as input).

◆ CMUTIL_StrTrim()

char * CMUTIL_StrTrim ( char *  inp)

Trim the both side of a c-style string.

Leading and trailing space characters (space, tab, line-feed, carriage-return) are removed from the input string. Input string must be null-terminated, and its contents could be changed after this function call. The result string reference is pointing to the original input string.

Parameters
inpInput string to be trimmed.
Returns
Left and right "trimmed" input string (pointing same as input).

◆ CMUTIL_StrNextToken()

const char * CMUTIL_StrNextToken ( char *  dest,
size_t  buflen,
const char *  src,
const char *  delims 
)

Get the next token from src.

If buflen is less than the length of the next token, the token will be split into multiple tokens.

Parameters
destThe next token will be stored in here.
buflenMaximum length of dest.
srcThe source string to get the next token.
delimsDelimiters to determine token.
Returns
The position of src after the token. NULL if invalid parameter.

◆ CMUTIL_StrSkipSpaces()

const char * CMUTIL_StrSkipSpaces ( const char *  src,
const char *  spaces 
)

Skip leading spaces.

Remove any leading space characters, custom spaces can be supplied.

Parameters
srcSource string.
spacesAny characters in here will be regards to space.
Returns
The position in src after leading spaces.

◆ CMUTIL_StringSplit()

CMUTIL_StringArray * CMUTIL_StringSplit ( const char *  haystack,
const char *  needle 
)

Split the haystack string using the needle.

Any occurrence of the needle in the haystack will cause a split.

Parameters
haystackSource string.
needleSplit delimiter.
Returns
Result strings in CMUTIL_StringArray form.

◆ CMUTIL_StringHexToBytes()

int CMUTIL_StringHexToBytes ( uint8_t *  dest,
const char *  src,
int  len 
)

Convert hex encoded string to bytes.

If len is an odd number, pad a '0' at the beginning.

Parameters
destByte array where result bytes will be stored in.
srcHex encoded source string.
lenThe number of characters to be converted from the source string.
Returns
Number of bytes of results.

◆ CMUTIL_ByteBufferCreateEx()

CMUTIL_ByteBuffer * CMUTIL_ByteBufferCreateEx ( size_t  initcapacity)

Creates CMUTIL_ByteBuffer object with initial capacity.

Parameters
initcapacityInitial capacity of this bytebuffer.
Returns
A new bytebuffer object.

◆ CMUTIL_CSConvCreate()

CMUTIL_CSConv * CMUTIL_CSConvCreate ( const char *  fromcs,
const char *  tocs 
)

Create a character set conversion object.

Parameters
fromcsThe source character set name.
tocsThe target character set name.
Returns
A pointer to the newly created CMUTIL_CSConv object, or NULL on failure.