O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
string.h
Go to the documentation of this file.
1#pragma once
2
3/* ************************************************************************** */
4/* ____ _ _____ ____ _ _ _ _____ _ _ ____ ____ */
5/* / ___| / \ | ___| _ \ / \ | \ | | |_ _| || | | _ \/ ___| */
6/* \___ \ / _ \ | |_ | |_) | / _ \ | \| | | | | || |_| | | \___ \ */
7/* ___) / ___ \| _| | _ < / ___ \| |\ | | | |__ _| |_| |___) | */
8/* |____/_/ \_|_| |_| \_/_/ \_|_| \_| |_| |_| |____/|____/ */
9/* */
17/* ************************************************************************** */
18
19#include "o2s/array.h"
20
21#include <stdbool.h> // bool
22#include <stddef.h> // size_t
23#include <stdint.h> // uint32_t
24#include <string.h> // strlen
25
31
35string_t string_from(const char* cstring, size_t length);
36void string_clear(string_t* self);
38
45#define string_from_literal(StringLiteral) \
46 string_from(StringLiteral, strlen(StringLiteral))
47
63#define String __attribute__((cleanup(string_clear))) string_t
64
67bool string_append_char(string_t* self, char character);
68bool string_append_cstring(string_t* self, const char* cstring, size_t length);
69bool string_append(string_t* self, const string_t* other);
70
71__attribute__((format(printf, 2, 3)))
72bool string_append_format(string_t* self, const char* format, ...);
74
81#define string_append_literal(Self, StringLiteral) \
82 string_append_cstring(Self, StringLiteral, strlen(StringLiteral))
83
86bool string_pop(string_t* self, char* destination);
87bool string_pop_n(string_t* self, char* destination, size_t count);
88string_t string_pop_as_string(string_t* self, size_t count);
89
90bool string_pop_front(string_t* self, char* destination);
91bool string_pop_front_n(string_t* self, char* destination, size_t count);
93
96size_t string_length(const string_t* self);
97char* string_get(const string_t* self, size_t index);
98bool string_is_empty(const string_t* self);
99char* string_end(const string_t* self);
100
101bool string_reserve(string_t* self, size_t count);
102bool string_trim(string_t* self);
104
107string_t string_map(const string_t* self, char (*function)(char));
108string_t string_tolower(const string_t* self);
109string_t string_toupper(const string_t* self);
110
111void string_apply_inplace(string_t* self, char (*function)(char));
115
116bool string_is_equal(const string_t* self, const string_t* other);
117
118char* string_to_cstring(string_t* self);
119
120bool string_contains(const string_t* self, char character);
121bool is_char_in_cstring(char character, const char* cstring);
122
125
126#define string_foreach(STRING, Element) array_foreach (char, STRING, Element)
127
129#define string_enumerate(STRING, Element, Index) \
130 array_enumerate (char, STRING, Element, Index)
131
132
135uint32_t cstring_fnv1a_32(const char* cstring, size_t length);
136uint32_t string_fnv1a_32(const string_t* self);
Dynamic arrays.
Dynamic string implementation.
bool string_pop_n(string_t *self, char *destination, size_t count)
Pop the count last characters.
Definition pop.c:24
bool string_trim(string_t *self)
Resize the underlying storage, for it not to take any more room that needed.
Definition utils.c:39
size_t string_length(const string_t *self)
Number of characters in the string.
Definition getters.c:18
void string_apply_inplace(string_t *self, char(*function)(char))
Replace each character by its image through the provided function.
Definition apply.c:31
char * string_to_cstring(string_t *self)
Provide compatibility with legacy string functions.
Definition utils.c:24
bool string_pop_front(string_t *self, char *destination)
Pop the first character.
Definition pop.c:40
char * string_end(const string_t *self)
One-past-the-last pointer.
Definition getters.c:39
bool string_contains(const string_t *self, char character)
Searches character in self.
Definition utils.c:51
string_t string_tolower(const string_t *self)
Creates a copy of the string, with all alphabetic characters to lower-case.
Definition map.c:40
string_t string_new(void)
Constructs an empty string.
uint32_t string_fnv1a_32(const string_t *self)
Fowler–Noll–Vo hash function, on 32 bits.
Definition hashes.c:34
string_t string_from(const char *cstring, size_t length)
Construct a string with an initial value.
void string_toupper_inplace(string_t *self)
Replace each alphabetic character by its upper case equivalent.
Definition apply.c:47
bool string_is_empty(const string_t *self)
True if the string has a length of zero.
Definition getters.c:33
string_t string_pop_as_string(string_t *self, size_t count)
Create a string from the count last characters.
Definition pop.c:30
bool string_reserve(string_t *self, size_t count)
Ensure the underlying storage can accomodate count more characters.
Definition utils.c:33
array_t string_t
A string is an array of char.
Definition string.h:30
bool string_pop(string_t *self, char *destination)
Pop the last character.
Definition pop.c:18
string_t string_toupper(const string_t *self)
Creates a copy of the string, with all alphabetic characters to upper-case.
Definition map.c:46
bool string_is_equal(const string_t *self, const string_t *other)
Compares the content of the 2 strings.
Definition utils.c:45
bool string_append_cstring(string_t *self, const char *cstring, size_t length)
Add a raw character string to the back of the string.
Definition append.c:27
uint32_t cstring_fnv1a_32(const char *cstring, size_t length)
Fowler–Noll–Vo hash function, on 32 bits.
Definition hashes.c:18
bool string_pop_front_n(string_t *self, char *destination, size_t count)
Pop the first count characters.
Definition pop.c:46
bool string_append_char(string_t *self, char character)
Add a single character to the back of the string.
Definition append.c:21
bool is_char_in_cstring(char character, const char *cstring)
Searches character in cstring.
Definition utils.c:59
bool string_append(string_t *self, const string_t *other)
Concatenate two strings.
Definition append.c:33
void string_clear(string_t *self)
Release allocated resources to the system.
void string_tolower_inplace(string_t *self)
Replace each alphabetic character by its lower case equivalent.
Definition apply.c:41
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.
Definition map.c:23
char * string_get(const string_t *self, size_t index)
The address of the character at the requested index.
Definition getters.c:27
O2S array implementation.
Definition array.h:24