CString

Encapsulates C string datatype and hides memory management.

Summary
CStringEncapsulates C string datatype and hides memory management.
CopyrightThis program is free software.
Files
C-kern/api/string/cstring.hHeader file of CString.
C-kern/string/cstring.cImplementation file CString impl.
Types
struct cstring_tExport cstring_t.
Functions
test
unittest_string_cstringTests cstring_t.
cstring_tDynamically growing C string with trailing ‘\0’ byte.
lengthLength of string in bytes (sizeof(char)==1).
allocated_sizeSize of allocated memory block chars points to.
charsPointer to character array with ‘\0’ at the end of the string.
lifetime
cstring_FREEStatic initializer.
cstring_INITStatic initializer.
init_cstringInits cstring_t and preallocates memory.
initfromstring_cstringInits cstring_t and copies data from string_t.
initmove_cstringInits dest with content of source and sets source to cstring_FREE.
free_cstringFrees any allocated memory associated with type cstring_t.
query
str_cstringReturns ‘\0’ terminated string.
size_cstringReturns the size of the string in bytes.
allocatedsize_cstringReturns the allocated buffer size in bytes.
change
allocate_cstringMakes sure cstr has at least allocate_size preallocated bytes.
append_cstringAppends str with len str_len to cstr.
clear_cstringSets size of string to 0.
printfappend_cstringAppends printf formatted string to cstr.
resize_cstringAllocates memory and sets size to new_size.
truncate_cstringAdapts size of cstr to a smaller value.
inline implementation
Macros
initmove_cstringImplements cstring_t.initmove_cstring.
allocatedsize_cstringImplements <cstring_t.allocatedsize>.
size_cstringImplements cstring_t.size_cstring.
str_cstringImplements cstring_t.str_cstring.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2011 Jörg Seebohn

Files

C-kern/api/string/cstring.h

Header file of CString.

C-kern/string/cstring.c

Implementation file CString impl.

Types

struct cstring_t

typedef struct cstring_t cstring_t

Export cstring_t.

test

unittest_string_cstring

int unittest_string_cstring(void)

Tests cstring_t.

cstring_t

struct cstring_t

Dynamically growing C string with trailing ‘\0’ byte.

Summary
lengthLength of string in bytes (sizeof(char)==1).
allocated_sizeSize of allocated memory block chars points to.
charsPointer to character array with ‘\0’ at the end of the string.
lifetime
cstring_FREEStatic initializer.
cstring_INITStatic initializer.
init_cstringInits cstring_t and preallocates memory.
initfromstring_cstringInits cstring_t and copies data from string_t.
initmove_cstringInits dest with content of source and sets source to cstring_FREE.
free_cstringFrees any allocated memory associated with type cstring_t.
query
str_cstringReturns ‘\0’ terminated string.
size_cstringReturns the size of the string in bytes.
allocatedsize_cstringReturns the allocated buffer size in bytes.
change
allocate_cstringMakes sure cstr has at least allocate_size preallocated bytes.
append_cstringAppends str with len str_len to cstr.
clear_cstringSets size of string to 0.
printfappend_cstringAppends printf formatted string to cstr.
resize_cstringAllocates memory and sets size to new_size.
truncate_cstringAdapts size of cstr to a smaller value.

length

Length of string in bytes (sizeof(char)==1).  The number of characters could be smaller if the encoding is UTF-8 for example.

Invariant

(0 == chars) || <chars>[size] == 0

allocated_size

size_t allocated_size

Size of allocated memory block chars points to.

Invariant

(0 == chars) || <size> + 1 <= allocated_size

chars

uint8_t* chars

Pointer to character array with ‘\0’ at the end of the string.

Invariant

((0 == chars) && (0 == allocated_size)) || ((0 != chars) && (0 != allocated_size))

lifetime

cstring_FREE

#define cstring_FREE { (size_t)0, (size_t)0, (void*)0 }

Static initializer.  Makes calling free_cstring a no op.

cstring_INIT

#define cstring_INIT { (size_t)0, (size_t)0, (void*)0 }

Static initializer.  Same as calling init_cstring with preallocate_size set to 0.

init_cstring

int init_cstring(/*out*/cstring_t *cstr,
size_t preallocate_size)

Inits cstring_t and preallocates memory.  If you set preallocate_size to 0 no memory is preallocated.

initfromstring_cstring

int initfromstring_cstring(/*out*/cstring_t *cstr,
const struct string_t *copiedfrom)

Inits cstring_t and copies data from string_t.  If parameter copiedfrom is empty no data is preallocated.

initmove_cstring

void initmove_cstring(/*out*/cstring_t * restrict dest,
cstring_t * restrict source)

Inits dest with content of source and sets source to cstring_FREE.

free_cstring

int free_cstring(cstring_t *cstr)

Frees any allocated memory associated with type cstring_t.

query

str_cstring

char * str_cstring(cstring_t *cstr)

Returns ‘\0’ terminated string.  The returned value can be NULL in case cstr->chars is NULL.  The returned value is valid as long as cstr is not changed.

size_cstring

size_t size_cstring(const cstring_t *cstr)

Returns the size of the string in bytes.  For multibyte encoded characters the length of a string in characters is less than the size in bytes.

allocatedsize_cstring

size_t allocatedsize_cstring(const cstring_t *cstr)

Returns the allocated buffer size in bytes.  To access the start address of the buffer use str_cstring.

change

allocate_cstring

int allocate_cstring(cstring_t *cstr,
size_t allocate_size)

Makes sure cstr has at least allocate_size preallocated bytes.  The string buffer is reallocated if necessary.  If it is big enough nothing is done.  After successful return the buffer returned from str_cstring points to at least allocate_size bytes of memory.

append_cstring

int append_cstring(cstring_t *cstr,
size_t str_len,
const char str[str_len])

Appends str with len str_len to cstr.

Attention

It is not allowed to use parts of the buffer of cstr as arguments for this function.  Cause during a possible reallocation of the buffer these arguments become invalid.

clear_cstring

void clear_cstring(cstring_t *cstr)

Sets size of string to 0.  This function has the same result as calling truncate_cstring with parameter 0.  No memory is deallocated.

printfappend_cstring

int printfappend_cstring(
   cstring_t *cstr,
   const char *format,
    ...
) __attribute__ ((__format__ (__printf__, 2, 3)))

Appends printf formatted string to cstr.  Like sprintf but the buffer is reallocated if it is too small and the new content is appended to the end of the string.

Attention

It is not allowed to use parts of the buffer of cstr as arguments for this function.  Cause during a possible reallocation of the buffer these arguments become invalid.

resize_cstring

int resize_cstring(cstring_t *cstr,
size_t new_size)

Allocates memory and sets size to new_size.  The possibly reallocated buffer can be accessed with a call to str_cstring.  If the new size is bigger than the current size the buffer will contain “random” characters.

truncate_cstring

int truncate_cstring(cstring_t *cstr,
size_t new_size)

Adapts size of cstr to a smaller value.  Use this if you you want to make the string smaller in size.  A trailing 0 byte is added by this call.  Use this function with caution cause new_size is interpreted as byte offset.  If a character uses more than one byte for its encoding and if new_size points not to the end of a valid character sequence the encoded character sequence becomes invalid.

inline implementation

Macros

initmove_cstring

#define initmove_cstring(
   dest,
   source
) do { *(dest) = *(source) ; *(source) = (cstring_t) cstring_FREE ; } while(0)

Implements cstring_t.initmove_cstring.

allocatedsize_cstring

#define allocatedsize_cstring(cstr) ((cstr)->allocated_size)

Implements <cstring_t.allocatedsize>.

size_cstring

#define size_cstring(cstr) ((cstr)->size)

Implements cstring_t.size_cstring.

str_cstring

#define str_cstring(cstr) ((char*)(cstr)->chars)

Implements cstring_t.str_cstring.

Encapsulates C string datatype and hides memory management.
Implements CString.
typedef struct cstring_t cstring_t
Export cstring_t.
struct cstring_t
Dynamically growing C string with trailing ‘\0’ byte.
int unittest_string_cstring(void)
Tests cstring_t.
size_t allocated_size
Size of allocated memory block chars points to.
uint8_t* chars
Pointer to character array with ‘\0’ at the end of the string.
#define cstring_FREE { (size_t)0, (size_t)0, (void*)0 }
Static initializer.
#define cstring_INIT { (size_t)0, (size_t)0, (void*)0 }
Static initializer.
int init_cstring(/*out*/cstring_t *cstr,
size_t preallocate_size)
Inits cstring_t and preallocates memory.
int initfromstring_cstring(/*out*/cstring_t *cstr,
const struct string_t *copiedfrom)
Inits cstring_t and copies data from string_t.
void initmove_cstring(/*out*/cstring_t * restrict dest,
cstring_t * restrict source)
Inits dest with content of source and sets source to cstring_FREE.
int free_cstring(cstring_t *cstr)
Frees any allocated memory associated with type cstring_t.
char * str_cstring(cstring_t *cstr)
Returns ‘\0’ terminated string.
size_t size_cstring(const cstring_t *cstr)
Returns the size of the string in bytes.
size_t allocatedsize_cstring(const cstring_t *cstr)
Returns the allocated buffer size in bytes.
int allocate_cstring(cstring_t *cstr,
size_t allocate_size)
Makes sure cstr has at least allocate_size preallocated bytes.
int append_cstring(cstring_t *cstr,
size_t str_len,
const char str[str_len])
Appends str with len str_len to cstr.
void clear_cstring(cstring_t *cstr)
Sets size of string to 0.
int printfappend_cstring(
   cstring_t *cstr,
   const char *format,
    ...
) __attribute__ ((__format__ (__printf__, 2, 3)))
Appends printf formatted string to cstr.
int resize_cstring(cstring_t *cstr,
size_t new_size)
Allocates memory and sets size to new_size.
int truncate_cstring(cstring_t *cstr,
size_t new_size)
Adapts size of cstr to a smaller value.
#define initmove_cstring(
   dest,
   source
) do { *(dest) = *(source) ; *(source) = (cstring_t) cstring_FREE ; } while(0)
Implements cstring_t.initmove_cstring.
#define allocatedsize_cstring(cstr) ((cstr)->allocated_size)
Implements cstring_t.allocatedsize.
#define size_cstring(cstr) ((cstr)->size)
Implements cstring_t.size_cstring.
#define str_cstring(cstr) ((char*)(cstr)->chars)
Implements cstring_t.str_cstring.
Close