O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
string.h File Reference

Dynamic string implementation. More...

#include "o2s/array.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

Go to the source code of this file.

Macros

#define string_from_literal(StringLiteral)
 Allocates a string, copying the content from a C string.
#define String
 Automatically free the allocated storage when going out of scope.
#define string_append_literal(Self, StringLiteral)
 Copy the content of a C string to the end of this string.
Iterators
#define string_foreach(STRING, Element)
 Iterate over each character of the string.
#define string_enumerate(STRING, Element, Index)
 Iterate over each character of the string with its index.

Typedefs

typedef array_t string_t
 A string is an array of char.

Functions

bool string_is_equal (const string_t *self, const string_t *other)
 Compares the content of the 2 strings.
char * string_to_cstring (string_t *self)
 Provide compatibility with legacy string functions.
bool string_contains (const string_t *self, char character)
 Searches character in self.
bool is_char_in_cstring (char character, const char *cstring)
 Searches character in cstring.
Contructors and destructors
string_t string_new (void)
 Constructs an empty string.
string_t string_from (const char *cstring, size_t length)
 Construct a string with an initial value.
void string_clear (string_t *self)
 Release allocated resources to the system.
Adding characters
bool string_append_char (string_t *self, char character)
 Add a single character to the back of the string.
bool string_append_cstring (string_t *self, const char *cstring, size_t length)
 Add a raw character string to the back of the string.
bool string_append (string_t *self, const string_t *other)
 Concatenate two strings.
bool string_append_format (string_t *self, const char *format,...)
 Append a string formed in a similar manner as printf.
Removing characters
bool string_pop (string_t *self, char *destination)
 Pop the last character.
bool string_pop_n (string_t *self, char *destination, size_t count)
 Pop the count last characters.
string_t string_pop_as_string (string_t *self, size_t count)
 Create a string from the count last characters.
bool string_pop_front (string_t *self, char *destination)
 Pop the first character.
bool string_pop_front_n (string_t *self, char *destination, size_t count)
 Pop the first count characters.
Capacity
size_t string_length (const string_t *self)
 Number of characters in the string.
char * string_get (const string_t *self, size_t index)
 The address of the character at the requested index.
bool string_is_empty (const string_t *self)
 True if the string has a length of zero.
char * string_end (const string_t *self)
 One-past-the-last pointer.
bool string_reserve (string_t *self, size_t count)
 Ensure the underlying storage can accomodate count more characters.
bool string_trim (string_t *self)
 Resize the underlying storage, for it not to take any more room that needed.
Transformation
string_t string_map (const string_t *self, char(*function)(char))
 Creates a copy of the string, where each character is the image through the provided function.
string_t string_tolower (const string_t *self)
 Creates a copy of the string, with all alphabetic characters to lower-case.
string_t string_toupper (const string_t *self)
 Creates a copy of the string, with all alphabetic characters to upper-case.
void string_apply_inplace (string_t *self, char(*function)(char))
 Replace each character by its image through the provided function.
void string_tolower_inplace (string_t *self)
 Replace each alphabetic character by its lower case equivalent.
void string_toupper_inplace (string_t *self)
 Replace each alphabetic character by its upper case equivalent.
Hashes
uint32_t cstring_fnv1a_32 (const char *cstring, size_t length)
 Fowler–Noll–Vo hash function, on 32 bits.
uint32_t string_fnv1a_32 (const string_t *self)
 Fowler–Noll–Vo hash function, on 32 bits.

Detailed Description

Dynamic string implementation.

Author
Hugo FOLCHER
Antoine GAGNIERE

Definition in file string.h.

Macro Definition Documentation

◆ String

#define String

Automatically free the allocated storage when going out of scope.

In a situation where one wants to declare a string in a local scope, this "typedef" can be used for that string to release the allocated memory automatically when the variable goes out of scope.

It means this "typedef" can only be used like this:

{
String my_string = string_new();
...
} // <- the underlying storage will be freed at that point
string_t string_new(void)
Constructs an empty string.
#define String
Automatically free the allocated storage when going out of scope.
Definition string.h:63

Definition at line 63 of file string.h.

◆ string_append_literal

#define string_append_literal ( Self,
StringLiteral )

Copy the content of a C string to the end of this string.

Note that strlen of a literal can be computed at compile-time. This function can technically also be used with run-time C string, it's just better not to rely on null-termination too much.

Definition at line 81 of file string.h.

◆ string_from_literal

#define string_from_literal ( StringLiteral)

Allocates a string, copying the content from a C string.

Note that strlen of a literal can be computed at compile-time. This function can technically also be used with run-time C string, it's just better not to rely on null-termination too much.

Definition at line 45 of file string.h.

Typedef Documentation

◆ string_t

typedef array_t string_t

A string is an array of char.

Note that it means that a string owns the memory.

Definition at line 30 of file string.h.

Function Documentation

◆ string_apply_inplace()

void string_apply_inplace ( string_t * self,
char(* function )(char) )

Replace each character by its image through the provided function.

Possible implementation in GNU C :

void local(char* c)
{
*c = function(*c);
}
array_iter(self, local);
void array_iter(const array_t *self, void(*function)(void *))
Call function on each element.
Definition iter.c:21

Definition at line 31 of file apply.c.

◆ string_clear()

void string_clear ( string_t * self)

Release allocated resources to the system.

See also
array_clear

Definition at line 33 of file constructors_destructors.c.

◆ string_get()

char * string_get ( const string_t * self,
size_t index )

The address of the character at the requested index.

Returns
NULL if the index is not valid

Definition at line 27 of file getters.c.

◆ string_is_equal()

bool string_is_equal ( const string_t * one,
const string_t * two )

Compares the content of the 2 strings.

Returns
true if they are the same

Definition at line 45 of file utils.c.

◆ string_to_cstring()

char * string_to_cstring ( string_t * self)

Provide compatibility with legacy string functions.

Ensure the string is null-terminated, without impacting the length.

Returns
NULL if a reallocation was necessary and failed

Definition at line 24 of file utils.c.