kiba-engine
string.h
Go to the documentation of this file.
1 
6 #pragma once
7 
9 #include <kiba/containers/array.h>
10 #include <kiba/defines.h>
11 
17 typedef struct string {
19  char *data;
21  usize length;
25 
31 typedef struct string_view {
33  string str;
35  usize length;
37  usize offset;
41 
42 static inline b8 character_is_whitespace(char c) { return c == ' ' || c == '\t' || c == '\n'; }
43 
44 static inline b8 character_is_decimal(char c) { return c >= '0' && c <= '9'; }
45 
46 static inline b8 character_is_hexadecimal(char c) { return character_is_decimal(c) || (c >= 'a' && c <= 'f'); }
47 
59 KB_API string string_from_raw(const char *raw);
60 
61 KB_API string string_copy_raw(const char *raw, allocator *alloc);
62 
75 KB_API string string_from_raw_n(const char *raw, usize n);
76 
87 KB_API string string_join_views(const array_of(string_view) views, const char sep, allocator *alloc);
88 
98 KB_API string string_from_view(const string_view view, allocator *alloc);
99 
109 KB_API string string_from_views(const array_of(string_view) views, allocator *alloc);
110 
116 KB_API void string_destroy(string *str);
117 
124 KB_API b8 string_is_valid(const string str);
125 
133 KB_API usize string_index_of(const string str, const char c);
134 
142 KB_API string string_clone(const string str, allocator *alloc);
143 
153 KB_API b8 string_equal(const string lhs, const string rhs);
154 
165 KB_API b8 string_n_equal(const string lhs, const string rhs, usize n);
166 
176 KB_API string_view string_view_from_string(const string str);
177 
186 KB_API const char *string_view_data(const string_view view);
187 
197 KB_API b8 string_view_equal(const string_view lhs, const string_view rhs);
198 
209 KB_API b8 string_view_n_equal(const string_view lhs, const string_view rhs, usize n);
210 
221 KB_API string_view string_view_at_offset(const string_view view, usize offset);
222 
235 KB_API string_view string_view_subview(const string_view view, usize start, usize length);
236 
244 KB_API char string_view_char_at(const string_view view, usize index);
245 
253 KB_API usize string_view_index_of(const string_view view, const char c);
254 
265 KB_API string_view string_view_after_next_char(const string_view view, const char c);
266 
277 KB_API string_view string_view_until_next_char(const string_view view, const char c);
278 
287 KB_API string_view string_view_trim(const string_view view);
288 
299 KB_API b8 string_view_split(const string_view view, const char c, array_of(string_view) * arr);
Central header providing allocator functionality.
KB_API string string_clone(const string str, allocator *alloc)
Clone a complete string.
Definition: string.c:113
KB_API usize string_view_index_of(const string_view view, const char c)
Get the first index of a letter in a string_view.
Definition: string.c:190
KB_API string_view string_view_until_next_char(const string_view view, const char c)
Create a new string_view of an existing string_view until a certain character.
Definition: string.c:211
KB_API string_view string_view_subview(const string_view view, usize start, usize length)
Create a new string_view of an existing string_view.
Definition: string.c:174
struct string_view string_view
Non owning views on actual strings.
struct string string
Structure for a string.
KB_API string string_from_raw_n(const char *raw, usize n)
Construct a string from n chars of raw character data.
Definition: string.c:27
KB_API string string_from_views(const array_of(string_view) views, allocator *alloc)
Creates a new single string based on all provided views.
Definition: string.c:77
KB_API string_view string_view_after_next_char(const string_view view, const char c)
Create a new string_view of an existing string_view after a certain character.
Definition: string.c:201
KB_API string string_from_raw(const char *raw)
Construct a string from a raw string.
Definition: string.c:13
KB_API usize string_index_of(const string str, const char c)
Get the first index of a letter in a string.
Definition: string.c:111
KB_API b8 string_view_n_equal(const string_view lhs, const string_view rhs, usize n)
Check if the contents of two string views are equal.
Definition: string.c:149
KB_API b8 string_is_valid(const string str)
Check if string is valid.
Definition: string.c:109
KB_API void string_destroy(string *str)
Free data of the string.
Definition: string.c:101
KB_API string string_join_views(const array_of(string_view) views, const char sep, allocator *alloc)
Creates a new single string based on all provided views separated by a separator.
Definition: string.c:35
KB_API string string_from_view(const string_view view, allocator *alloc)
Creates a new string from a string view.
Definition: string.c:61
KB_API b8 string_equal(const string lhs, const string rhs)
Check if the contents of two strings are equal.
Definition: string.c:118
KB_API string_view string_view_from_string(const string str)
Create a new string view of a whole string.
Definition: string.c:126
KB_API string_view string_view_trim(const string_view view)
Create a new string_view which does not start or end in whitespace.
Definition: string.c:221
KB_API const char * string_view_data(const string_view view)
Get raw pointer to beginning of the string_view data.
Definition: string.c:135
KB_API char string_view_char_at(const string_view view, usize index)
Get the letter at an specific index in a string_view.
Definition: string.c:183
KB_API b8 string_view_equal(const string_view lhs, const string_view rhs)
Check if the contents of two string views are equal.
Definition: string.c:137
KB_API string_view string_view_at_offset(const string_view view, usize offset)
Create a new string_view of an existing string_view.
Definition: string.c:165
KB_API b8 string_view_split(const string_view view, const char c, array_of(string_view) *arr)
Split a single string_view into multiple based on a separator.
Definition: string.c:241
KB_API b8 string_n_equal(const string lhs, const string rhs, usize n)
Check if the contents of two strings are equal.
Definition: string.c:122
Global typedefs and macros.
Central allocator structure.
Definition: allocator.h:87
Non owning views on actual strings.
Definition: string.h:31
string str
The string the view is defined on.
Definition: string.h:33
b8 is_valid
Indicates if the view is valid and can be used/accessed.
Definition: string.h:39
usize length
The length of the view.
Definition: string.h:35
usize offset
The offset into the string.
Definition: string.h:37
Structure for a string.
Definition: string.h:17
allocator * alloc
The allocator used for construction. KB_NULL if constructed from a raw literal.
Definition: string.h:23
char * data
The character sequence of the string.
Definition: string.h:19
usize length
The length of the string.
Definition: string.h:21