cbase 1.46.11
C/C++ Static Template
Loading...
Searching...
No Matches
base_strings.h
Go to the documentation of this file.
1
8
9#ifndef BASE_STRING_H
10#define BASE_STRING_H
11
12#include "base_macros.h"
13#include "base_types.h"
14
19
24typedef struct String8 {
28
36
46
48
53
59#define STR8(s) ((String8){.str = (u8 *)(s), .size = sizeof(s) - 1})
60
69#define STR8_FMT(s) (int)(s).size, (char *)(s).str
70
72
77
81function inline String8
82str8(u8 *str, usize size)
83{
84 String8 result = {str, size};
85 return result;
86}
87
91function inline String8
92str8_range(u8 *first, u8 *one_past_last)
93{
94 String8 result = {first, (usize)(one_past_last - first)};
95 return result;
96}
97
102function inline String8
103str8_cstr(const char *cstr)
104{
105 String8 result = {0};
106 if (cstr != NULL) {
107 result.str = (u8 *)cstr;
108 while (cstr[result.size] != '\0') {
109 result.size += 1;
110 }
111 }
112 return result;
113}
114
118function inline String8
120{
121 usize clamped_size = MIN(size, str.size);
122 return str8(str.str, clamped_size);
123}
124
128function inline String8
130{
131 usize clamped_amt = MIN(amt, str.size);
132 return str8(str.str + clamped_amt, str.size - clamped_amt);
133}
134
138function inline String8
139str8_substr(String8 str, usize first, usize one_past_last)
140{
141 usize clamped_first = MIN(first, str.size);
142 usize clamped_opl = MIN(one_past_last, str.size);
143 usize final_first = MIN(clamped_first, clamped_opl);
144 return str8(str.str + final_first, clamped_opl - final_first);
145}
146
150function inline b32
152{
153 if (a.size != b.size) {
154 return 0; // false
155 }
156 return (a.size == 0) || (memcmp(a.str, b.str, a.size) == 0);
157}
158
160
161#endif // BASE_STRING_H
Context detection, utilities, compiler abstractions, and data structures.
Core type definitions and fixed-width aliases.
uint8_t u8
Definition base_types.h:22
#define function
File-scoped function (invisible to the linker).
size_t usize
Definition base_types.h:43
static String8 str8(u8 *str, usize size)
Creates a String8 from a raw pointer and size.
static String8 str8_cstr(const char *cstr)
Converts a standard null-terminated C string into a String8.
static String8 str8_skip(String8 str, usize amt)
Returns a substring by skipping the first amt bytes of the input string.
static String8 str8_range(u8 *first, u8 *one_past_last)
Creates a String8 spanning from the first pointer to the one_past_last pointer.
static String8 str8_substr(String8 str, usize first, usize one_past_last)
Returns a substring spanning from first to one_past_last indices.
static b32 str8_match(String8 a, String8 b)
Checks if two String8s are exactly equal (case-sensitive).
static String8 str8_prefix(String8 str, usize size)
Returns a substring containing the first size bytes of the input string.
#define MIN(a, b)
Returns the minimum of two values (Standard C fallback).
int32_t b32
Definition base_types.h:75
A sized UTF-8 string slice.
usize size
A linked list of strings, useful for concatenating text without reallocation.
usize node_count
String8Node * last
usize total_size
String8Node * first
A node for a linked list of strings (String Builder).
struct String8Node * next
String8 string