String

Offers type to wrap constant strings or substrings into an object.

Summary
StringOffers type to wrap constant strings or substrings into an object.
CopyrightThis program is free software.
Files
C-kern/api/string/string.hHeader file of String.
C-kern/string/string.cImplementation file String impl.
Types
struct string_tExport string_t.
Functions
test
unittest_stringTest <escapechar_string>.
string_tPoints to memory which contains a constant string.
addrStart address of non-const string memory.
sizeSize in bytes of memory.
lifetime
string_FREEStatic initializer.
string_INITStatic initializer.
string_INIT_CSTRStatic initializer.
init_stringAssigns constant string buffer to str.
initcopy_stringCopies content of srcstr to str.
initfl_stringAssigns static string buffer to str.
initse_stringAssigns static string buffer to str.
initsubstr_stringInitializes str with substring of fromstr.
initfromstringstream_stringInitializes str with content of stringstream_t.
free_stringSets string to string_FREE.
query
isfree_stringReturns true if string has address and size of 0.
isempty_stringReturns true if string has size 0.
addr_stringReturns the start address of the string in memory.
size_stringReturns size in bytes of string.
findbyte_stringFinds byte in string.
compare
isequalasciicase_stringReturns true if two strings compare equal in a case insensitive way.
change
substr_stringThe string is made a substring of itself.
shrinkleft_stringShrinks size of string by skipping bytes from the start.
shrinkright_stringShrinks size of string by decrementing its size.
skipbyte_stringIncrements the start address of the string by one and decrements its size.
generic
genericcast_stringCasts a pointer to generic obj type into pointer to string_t.
inline implementation
string_t
addr_stringImplements string_t.addr_string.
findbyte_stringImplements string_t.findbyte_string.
free_stringImplements string_t.free_string.
genericcast_stringImplements string_t.genericcast_string.
init_stringImplements string_t.init_string.
initcopy_stringImplements string_t.initcopy_string.
isempty_stringImplements string_t.isempty_string.
isfree_stringImplements string_t.isfree_string.
size_stringImplements string_t.size_string.
skipbyte_stringImplements string_t.skipbyte_string.

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/string.h

Header file of String.

C-kern/string/string.c

Implementation file String impl.

Types

struct string_t

typedef struct string_t string_t

Export string_t.

Functions

Summary
test
unittest_stringTest <escapechar_string>.

test

unittest_string

int unittest_string(void)

Test <escapechar_string>.

string_t

struct string_t

Points to memory which contains a constant string.  This string does not include a trailing ‘\0’ byte.  Null bytes are allowed in the string cause size describes the length of string and not any implicit trailing \0 byte.  If you want to convert this string into a C string, i.e. either (char *) or cstring_t make sure that it does not contain any \0 byte.

Empty string

The empty string is represented as

{ "", 0 }

The size does not include any trailing ‘\0’ byte.

Undefined string

The undefined (or null) string is represented as

{ 0, 0 }
Summary
addrStart address of non-const string memory.
sizeSize in bytes of memory.
lifetime
string_FREEStatic initializer.
string_INITStatic initializer.
string_INIT_CSTRStatic initializer.
init_stringAssigns constant string buffer to str.
initcopy_stringCopies content of srcstr to str.
initfl_stringAssigns static string buffer to str.
initse_stringAssigns static string buffer to str.
initsubstr_stringInitializes str with substring of fromstr.
initfromstringstream_stringInitializes str with content of stringstream_t.
free_stringSets string to string_FREE.
query
isfree_stringReturns true if string has address and size of 0.
isempty_stringReturns true if string has size 0.
addr_stringReturns the start address of the string in memory.
size_stringReturns size in bytes of string.
findbyte_stringFinds byte in string.
compare
isequalasciicase_stringReturns true if two strings compare equal in a case insensitive way.
change
substr_stringThe string is made a substring of itself.
shrinkleft_stringShrinks size of string by skipping bytes from the start.
shrinkright_stringShrinks size of string by decrementing its size.
skipbyte_stringIncrements the start address of the string by one and decrements its size.
generic
genericcast_stringCasts a pointer to generic obj type into pointer to string_t.

addr

const uint8_t * addr

Start address of non-const string memory.

size

size_t size

Size in bytes of memory.  The number of characters is lower or equal to this value.

lifetime

string_FREE

#define string_FREE { 0, 0 }

Static initializer.  Sets string_t to null.

string_INIT

#define string_INIT(strsize,
straddr) { (straddr), (strsize) }

Static initializer.  Assigns static string buffer to string_t.

Parameter

strsizeThe length in bytes of the substring.  The number of characters represented is usually less then its size (a character can be represented with multiple bytes).
straddrThe start address of the string.

string_INIT_CSTR

#define string_INIT_CSTR(cstr) { (const uint8_t*)(cstr), strlen(cstr) }

Static initializer.  Sets string_t to address of cstr and size to strlen(cstr).

Example

const char   * buffer = "teststring" ;
string_t     str      = string_INIT_CSTR(buffer) ;

init_string

static inline void init_string(/*out*/string_t *str,
size_t size,
const uint8_t string[size])

Assigns constant string buffer to str.

Expected parameter

strpointer to string_t object which is initialized
sizeLength of string in bytes
stringAddress of first character.

initcopy_string

static inline void initcopy_string(/*out*/string_t *str,
const string_t * restrict srcstr)

Copies content of srcstr to str.  The two string objects are not allowed to overlap.  Only references are copied.  The characters in memory are not copied.

initfl_string

int initfl_string(/*out*/string_t *str,
const uint8_t *first,
const uint8_t *last)

Assigns static string buffer to str.  To initialize as empty string set last to a value smaller first.  If first == last the size of the string is one.

Expected parameter

strpointer to string_t object which is initialized
firstAddress of first character.
lastAddress of last character.

initse_string

int initse_string(/*out*/string_t *str,
const uint8_t *start,
const uint8_t *end)

Assigns static string buffer to str.

Expected parameter

strpointer to string_t object which is initialized
startAddress of first character.
endAddress of memory after last character.

initsubstr_string

int initsubstr_string(/*out*/string_t *str,
const string_t * restrict fromstr,
size_t start_offset,
size_t size)

Initializes str with substring of fromstr.  Same as calling <initcopy_string>(str,fromstr) and then calling <substr_string>(str,start_offset,size).

initfromstringstream_string

void initfromstringstream_string(/*out*/string_t *str,
const struct stringstream_t *strstream)

Initializes str with content of stringstream_t.  The string points to the unread string buffer of stringstream_t.

free_string

void free_string(string_t *str)

Sets string to string_FREE.

query

isfree_string

bool isfree_string(const string_t *str)

Returns true if string has address and size of 0.

isempty_string

bool isempty_string(const string_t *str)

Returns true if string has size 0.  A string initialized with string_FREE is considered an empty string.  Use isfree_string to check if it is uninitialized.

addr_string

const uint8_t * addr_string(const string_t *str)

Returns the start address of the string in memory.  The start address is the lowest address in memory.  To access the second byte of the string use

string_t str = string_INIT(...) ;
uint8_t  seconde_byte = addr_string(&str)[1] ;

size_string

size_t size_string(const string_t *str)

Returns size in bytes of string.  The size is not the same as the number of contained characters.  A character can occupy more than one byte in memory so the size of a string is the upper limit of the number of contained characters.

findbyte_string

const uint8_t * findbyte_string(const string_t *str,
uint8_t byte)

Finds byte in string.  The returned value points to the position of the found byte.  The value 0 is returned if str does not contain the byte.

compare

isequalasciicase_string

bool isequalasciicase_string(const string_t *str,
const string_t *str2)

Returns true if two strings compare equal in a case insensitive way.  This functions assumes only ‘A’-’Z’ and ‘a’-’z’ as case sensitive characters.

change

substr_string

int substr_string(string_t *str,
size_t start_offset,
size_t size)

The string is made a substring of itself.  Call initsubstr_string if you want to keep the original.  If the precondition fails EINVAL is returned.

Parameter

start_offsetThe start address of substring relative to the previous start address (see addr_string).  This value must be smaller or equal than <size_string>(str).
sizeThe size (length) in number of bytes of the substring.  This value must be smaller or equal than (<size_string>(str)-start_offset).

shrinkleft_string

int shrinkleft_string(string_t *str,
size_t nr_bytes_removed_from_string_start)

Shrinks size of string by skipping bytes from the start.  Increments the start address of the string and decrements its size by nr_bytes_removed_from_string_start.  The parameter nr_bytes_removed_from_string_start must be smaller or equal to <size_string>(str).  If the precondition fails EINVAL is returned.

shrinkright_string

int shrinkright_string(string_t *str,
size_t nr_bytes_removed_from_string_end)

Shrinks size of string by decrementing its size.  The parameter nr_bytes_removed_from_string_end must be smaller or equal to <size_string>(str).  If the precondition fails EINVAL is returned.  After successfull return the size is decremented by nr_bytes_removed_from_string_end.  The start address of the string keeps the same.

skipbyte_string

void skipbyte_string(string_t *str)

Increments the start address of the string by one and decrements its size.

Attention

Call this function only if you know that isempty_string does return false.

generic

genericcast_string

const string_t * genericcast_string(const void *obj)

Casts a pointer to generic obj type into pointer to string_t.

string_t

addr_string

#define addr_string(str) ((str)->addr)

Implements string_t.addr_string.

findbyte_string

free_string

genericcast_string

init_string

static inline void init_string(/*out*/string_t *str,
size_t size,
const uint8_t string[size])

Implements string_t.init_string.

initcopy_string

static inline void initcopy_string(/*out*/string_t *str,
const string_t * restrict srcstr)

Implements string_t.initcopy_string.

isempty_string

#define isempty_string(str) (0 == (str)->size)

Implements string_t.isempty_string.

isfree_string

#define isfree_string(str) (0 == (str)->addr && 0 == (str)->size)

Implements string_t.isfree_string.

size_string

#define size_string(str) ((str)->size)

Implements string_t.size_string.

skipbyte_string

#define skipbyte_string(str) do { ++ (str)->addr ; -- (str)->size ; } while(0)

Implements string_t.skipbyte_string.

Offers type to wrap constant strings or substrings into an object.
Implements String.
typedef struct string_t string_t
Export string_t.
struct string_t
Points to memory which contains a constant string.
int unittest_string(void)
Test escapechar_string.
const uint8_t * addr
Start address of non-const string memory.
size_t size
Size in bytes of memory.
#define string_FREE { 0, 0 }
Static initializer.
#define string_INIT(strsize,
straddr) { (straddr), (strsize) }
Static initializer.
#define string_INIT_CSTR(cstr) { (const uint8_t*)(cstr), strlen(cstr) }
Static initializer.
static inline void init_string(/*out*/string_t *str,
size_t size,
const uint8_t string[size])
Assigns constant string buffer to str.
static inline void initcopy_string(/*out*/string_t *str,
const string_t * restrict srcstr)
Copies content of srcstr to str.
int initfl_string(/*out*/string_t *str,
const uint8_t *first,
const uint8_t *last)
Assigns static string buffer to str.
int initse_string(/*out*/string_t *str,
const uint8_t *start,
const uint8_t *end)
Assigns static string buffer to str.
int initsubstr_string(/*out*/string_t *str,
const string_t * restrict fromstr,
size_t start_offset,
size_t size)
Initializes str with substring of fromstr.
void initfromstringstream_string(/*out*/string_t *str,
const struct stringstream_t *strstream)
Initializes str with content of stringstream_t.
struct stringstream_t
void free_string(string_t *str)
Sets string to string_FREE.
bool isfree_string(const string_t *str)
Returns true if string has address and size of 0.
bool isempty_string(const string_t *str)
Returns true if string has size 0.
const uint8_t * addr_string(const string_t *str)
Returns the start address of the string in memory.
size_t size_string(const string_t *str)
Returns size in bytes of string.
const uint8_t * findbyte_string(const string_t *str,
uint8_t byte)
Finds byte in string.
bool isequalasciicase_string(const string_t *str,
const string_t *str2)
Returns true if two strings compare equal in a case insensitive way.
int substr_string(string_t *str,
size_t start_offset,
size_t size)
The string is made a substring of itself.
int shrinkleft_string(string_t *str,
size_t nr_bytes_removed_from_string_start)
Shrinks size of string by skipping bytes from the start.
int shrinkright_string(string_t *str,
size_t nr_bytes_removed_from_string_end)
Shrinks size of string by decrementing its size.
void skipbyte_string(string_t *str)
Increments the start address of the string by one and decrements its size.
const string_t * genericcast_string(const void *obj)
Casts a pointer to generic obj type into pointer to string_t.
#define addr_string(str) ((str)->addr)
Implements string_t.addr_string.
static inline void init_string(/*out*/string_t *str,
size_t size,
const uint8_t string[size])
Implements string_t.init_string.
static inline void initcopy_string(/*out*/string_t *str,
const string_t * restrict srcstr)
Implements string_t.initcopy_string.
#define isempty_string(str) (0 == (str)->size)
Implements string_t.isempty_string.
#define isfree_string(str) (0 == (str)->addr && 0 == (str)->size)
Implements string_t.isfree_string.
#define size_string(str) ((str)->size)
Implements string_t.size_string.
#define skipbyte_string(str) do { ++ (str)->addr ; -- (str)->size ; } while(0)
Implements string_t.skipbyte_string.
struct cstring_t
Dynamically growing C string with trailing ‘\0’ byte.
Close