Robot
Move a little robot.
string.h File Reference
#include <stddef.h>
#include <stdarg.h>
Include dependency graph for string.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  string
 A dynamically allocated string structure. More...
 

Macros

#define string_append_literal(x, y)   string_append_with_size(x, y, (sizeof y) - 1)
 Append a litteral to the string's content. More...
 

Functions

void string_init (struct string *s)
 Initializes a string structure with an empty string. More...
 
void string_deinit (struct string *s)
 Frees (de-initializes) memory used by a string structure. More...
 
void string_clear (struct string *s)
 Clears the contents of the string, making it empty. More...
 
size_t string_len (struct string *s)
 Returns the length of the string (excluding the final '\0'). More...
 
void string_print (struct string *s)
 Prints the string's content and internal data (pointers and sizes). More...
 
void string_append_with_size (struct string *s, const char *following, size_t len_following)
 Append a const char* to the string's content (size of following given). More...
 
void string_append (struct string *s, const char *following)
 Append a const char* to the string's content. More...
 
int string_snprintf (struct string *s, const char *format,...)
 Formats a string and stores the result in a dynamically allocated string struct. More...
 

Macro Definition Documentation

◆ string_append_literal

#define string_append_literal (   x,
 
)    string_append_with_size(x, y, (sizeof y) - 1)

Append a litteral to the string's content.

Size of following is computed using sizeof!

Parameters
xPointer to the string structure.
yString literal to append.

Function Documentation

◆ string_append()

void string_append ( struct string s,
const char *  following 
)

Append a const char* to the string's content.

Size of following is computed using strlen!

Parameters
sPointer to the string structure.
followingPointer to the const char* we want to append at the end of our string.

◆ string_append_with_size()

void string_append_with_size ( struct string s,
const char *  following,
size_t  len_following 
)

Append a const char* to the string's content (size of following given).

Reallocates memory when necessary and keeps used and available sizes up to date.

Parameters
sPointer to the string structure.
followingPointer to the const char* we want to append at the end of our string.
len_followingSize of following.

◆ string_clear()

void string_clear ( struct string s)

Clears the contents of the string, making it empty.

Sets the first character to the null terminator and resets the used size. Does not free or reallocate memory.

Parameters
sPointer to the string structure to clear.

◆ string_deinit()

void string_deinit ( struct string s)

Frees (de-initializes) memory used by a string structure.

Parameters
sPointer to the string structure to de-initialize.

◆ string_init()

void string_init ( struct string s)

Initializes a string structure with an empty string.

Allocates memory for a string containing only the null terminator. Sets available and used sizes to 1.

Parameters
sPointer to the string structure to initialize.

◆ string_len()

size_t string_len ( struct string s)

Returns the length of the string (excluding the final '\0').

Parameters
sPointer to the string structure.
Returns
The number of characters in the string, excluding the final '\0'.

◆ string_print()

void string_print ( struct string s)

Prints the string's content and internal data (pointers and sizes).

Outputs the string, its memory address, used size (including null terminator), and available size to stdout.

Parameters
sPointer to the string structure.

◆ string_snprintf()

int string_snprintf ( struct string s,
const char *  format,
  ... 
)

Formats a string and stores the result in a dynamically allocated string struct.

This function formats a string using a printf-like format string and variadic arguments, allocates memory for the result inside the provided struct string*, and stores the formatted string in it.

Parameters
sPointer to the string structure.
formatThe format string (as in printf).
...Variadic arguments to format into the string.
Returns
The number of characters written (excluding the null terminator), or a negative value if an error occurred.