cbase 1.50.0
C/C++ Static Template
Loading...
Searching...
No Matches
base_strings.h
Go to the documentation of this file.
1
37
38#ifndef BASE_STRINGS_H
39#define BASE_STRINGS_H
40
41#include "base_macros.h"
42#include "base_types.h"
43#include <ctype.h>
44#include <string.h>
45
47
52
57typedef struct String8 {
61
73
87
89
94
100#if defined(COMPILER_CLANG) || defined(COMPILER_GCC)
101// # define STR8(s) \
102// (_Static_assert(__builtin_constant_p(s), "STR8: pass a string literal only"), \
103// (String8){.str = (u8 *)(s), .size = sizeof(s) - 1})
104# define STR8(s) ((String8){.str = (u8 *)(s), .size = sizeof(s) - 1})
105#else
106# define STR8(s) ((String8){.str = (u8 *)(s), .size = sizeof(s) - 1})
107#endif
108
117#define STR8_FMT(s) (int)(s).size, (char *)(s).str
118
120
125
132function inline String8
133str8(u8 *str, usize size)
134{
135 String8 result = {str, size};
136 return result;
137}
138
145function inline String8
146str8_range(u8 *first, u8 *one_past_last)
147{
148 String8 result = {first, (usize)(one_past_last - first)};
149 return result;
150}
151
157function inline String8
158str8_cstr(const char *cstr)
159{
160 if (cstr == NULL) return (String8){0};
161 return str8((u8 *)cstr, strlen(cstr));
162}
163
170function inline String8
172{
173 usize clamped_size = MIN(size, str.size);
174 return str8(str.str, clamped_size);
175}
176
183function inline String8
185{
186 usize clamped_size = MIN(size, str.size);
187 return str8(str.str + (str.size - clamped_size), clamped_size);
188}
189
196function inline String8
198{
199 usize clamped_amt = MIN(amt, str.size);
200 return str8(str.str + clamped_amt, str.size - clamped_amt);
201}
202
209function inline String8
211{
212 usize clamped_amt = MIN(amt, str.size);
213 return str8(str.str, str.size - clamped_amt);
214}
215
223function inline String8
224str8_substr(String8 str, usize first, usize one_past_last)
225{
226 usize clamped_first = MIN(first, str.size);
227 usize clamped_opl = MIN(one_past_last, str.size);
228 if (clamped_opl < clamped_first) clamped_opl = clamped_first;
229 return str8(str.str + clamped_first, clamped_opl - clamped_first);
230}
231
238function inline b32
240{
241 if (a.size != b.size) {
242 return 0;
243 }
244 return (a.size == 0) || (memcmp(a.str, b.str, a.size) == 0);
245}
246
253function inline b32
255{
256 if (a.size != b.size) {
257 return 0;
258 }
259 for (usize i = 0; i < a.size; i += 1) {
260 if (tolower(a.str[i]) != tolower(b.str[i])) {
261 return 0;
262 }
263 }
264 return 1;
265}
266
273function inline b32
275{
276 if (prefix.size > str.size) {
277 return 0;
278 }
279 return str8_match(str8_prefix(str, prefix.size), prefix);
280}
281
288function inline b32
290{
291 if (suffix.size > str.size) {
292 return 0;
293 }
294 return str8_match(str8_suffix(str, suffix.size), suffix);
295}
296
303function inline usize
305{
306 for (usize i = 0; i < str.size; i += 1) {
307 if (str.str[i] == byte) {
308 return i;
309 }
310 }
311 return str.size;
312}
313
320function inline void
322{
323 node->string = str;
324 node->next = NULL;
325 if (list->last != NULL) {
326 list->last->next = node;
327 } else {
328 list->first = node;
329 }
330 list->last = node;
331 list->node_count += 1;
332 list->total_size += str.size;
333}
334
336
338
339#endif // BASE_STRINGS_H
Context detection, compiler abstractions, utility macros, and data structures.
Core fixed-width type aliases and memory size constants.
uint8_t u8
Definition base_types.h:46
#define function
File-scoped function (invisible to the linker).
#define C_LINKAGE_END
Closes a C linkage block for C++ compilers (Empty in C).
#define C_LINKAGE_BEGIN
Opens a C linkage block for C++ compilers (Empty in C).
size_t usize
Definition base_types.h:68
static String8 str8(u8 *str, usize size)
Creates a String8 from a raw pointer and explicit byte length.
static b32 str8_starts_with(String8 str, String8 prefix)
Returns 1 if str begins with prefix.
static String8 str8_cstr(const char *cstr)
Converts a null-terminated C string into a String8.
static String8 str8_suffix(String8 str, usize size)
Returns the last size bytes of str.
static String8 str8_skip(String8 str, usize amt)
Returns str with the first amt bytes removed.
static b32 str8_match_nocase(String8 a, String8 b)
Returns 1 if a and b are equal ignoring ASCII case.
static b32 str8_ends_with(String8 str, String8 suffix)
Returns 1 if str ends with suffix.
static String8 str8_chop(String8 str, usize amt)
Returns str with the last amt bytes removed.
static String8 str8_range(u8 *first, u8 *one_past_last)
Creates a String8 spanning from first to one_past_last.
static String8 str8_substr(String8 str, usize first, usize one_past_last)
Returns the substring from byte index first to one_past_last.
static void str8_list_push(String8List *list, String8Node *node, String8 str)
Appends a String8 to a String8List via a caller-allocated node.
static b32 str8_match(String8 a, String8 b)
Returns 1 if a and b contain identical bytes (case-sensitive).
static String8 str8_prefix(String8 str, usize size)
Returns the first size bytes of str.
static usize str8_find_first(String8 str, u8 byte)
Returns the byte index of the first occurrence of byte in str.
#define MIN(a, b)
Returns the minimum of two values (Standard C fallback).
int32_t b32
Definition base_types.h:111
A sized UTF-8 string slice.
usize size
A singly-linked list of String8 slices.
usize node_count
String8Node * last
usize total_size
String8Node * first
A node for building singly-linked lists of strings.
struct String8Node * next
String8 string