libstring
simple flexible string manipulation library
 All Data Structures Files Functions Variables Typedefs Macros
Data Structures | Macros | Typedefs | Functions
libstring.h File Reference

Header file for libstring. More...

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>

Go to the source code of this file.

Data Structures

struct  _string_real
 

Macros

#define string_unit
 
#define string_unit   char
 
#define _strlen(x)   strlen(x)
 
#define SR_MAGIC_NUMBER   1
 
#define string_to_sr(x)   ((string_real *)((unsigned char *)x - sizeof(string_real)))
 
#define sr_to_string(x)   ((string)((unsigned char *)x + sizeof(string_real)))
 
#define is_sr(x)   (((string_real *)((unsigned char *)x - sizeof(string_real)))->flg == SR_MAGIC_NUMBER)
 
#define string_length(x)   is_sr(x) ? string_to_sr(x)->len : _strlen(x)
 
#define string_free(x)   free(is_sr(x)?(void *)string_to_sr(x):(void *)x)
 
#define string_append(a, b)   string_copy(a, b, string_length(a), 0)
 
#define string_new_size(x)   string_realloc(NULL, x)
 
#define string_istemporary(x)   (is_sr(x)?(string_to_sr(x)->tmp == 1):0)
 

Typedefs

typedef struct _string_real string_real
 
typedef string_unitstring
 

Functions

string string_new ()
 
string string_realloc (string a, uint16_t minS)
 
string string_copy (string a, const string b, uint16_t offset, uint16_t num)
 
string string_dup (const string a)
 
string string_temporary (string in)
 
string string_mknew (const string_unit *in)
 
string string_appendv (int count,...)
 

Detailed Description

Header file for libstring.

Macro Definition Documentation

#define _strlen (   x)    strlen(x)

Internal use strlen

#define is_sr (   x)    (((string_real *)((unsigned char *)x - sizeof(string_real)))->flg == SR_MAGIC_NUMBER)

Check if a string is a library-specific string by checking if it has this magic number specified

#define SR_MAGIC_NUMBER   1

Some random magic number used to check if a string passed to one of the library functions is library-compatible string or not (ie, it has the header that gives some extra information about it)

#define sr_to_string (   x)    ((string)((unsigned char *)x + sizeof(string_real)))

Convert from a string_real header to the string Simply done by going forwards to reach the start of the string

#define string_append (   a,
 
)    string_copy(a, b, string_length(a), 0)

Append two strings (aka copy characters from b to the end of a)

a string #1 b string #2

Returns
the appended string a+b (guaranteed to be library-compatible string)
#define string_free (   x)    free(is_sr(x)?(void *)string_to_sr(x):(void *)x)

Free a string

#define string_istemporary (   x)    (is_sr(x)?(string_to_sr(x)->tmp == 1):0)

Check if a string is temporary or not

Returns
t/f
#define string_length (   x)    is_sr(x) ? string_to_sr(x)->len : _strlen(x)

Get the length of a string

#define string_new_size (   x)    string_realloc(NULL, x)

Create a new string with a minimum given size

Returns
pointer to the new string
#define string_to_sr (   x)    ((string_real *)((unsigned char *)x - sizeof(string_real)))

Convert from a string to the string_real header Simply done by going backwards to reach the header

#define string_unit

The "unit" of each string For non-unicode, it is a char

#define string_unit   char

The "unit" of each string For non-unicode, it is a char

Typedef Documentation

typedef string_unit* string

a "string" (aka array of whatever type string_unit is)

typedef struct _string_real string_real

Main string type used by the library

Most of the time, the user interacts only with the "string". Internally, however, this is how strings are represented.

The value of the string itself follows after this small header. That is the memory address returned by a function.

IMPORTANT DISAMBIGUATION:

  • size = size in memory
  • length = the length of the string (not including null terminator)

string_real.len is a length! string_real.tot is a size!

Function Documentation

string string_appendv ( int  count,
  ... 
)

Append a variable number of strings

Returns
pointer to the new string
string string_copy ( string  a,
const string  b,
uint16_t  offset,
uint16_t  num 
)

Copy num characters from string B to A with offset offset.

if num is 0, then all characters will be copied.

a string #1 b string #2 offset offset num number

Returns
the new string (guaranteed to be library-compatible string)
string string_dup ( const string  a)

Make a duplicate of a string

a string

Returns
the copy of the string (guaranteed to be library-compatible string)
string string_mknew ( const string_unit in)

Duplicate a non-library string

Use this function if you are concerned about reads before the string

string string_new ( )

Create a new string

Returns
pointer to the new string
string string_realloc ( string  a,
uint16_t  minS 
)

Reallocate string A with minimum size

a the string to reallocate minS the minimum size of the string

Returns
pointer to the new string
string string_temporary ( string  in)

Declare a string as temporary, ie, that it will be freed right after use

If the string is not library-compatible, nothing happens.

Returns
the pointer to the string