-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.h
337 lines (255 loc) · 8 KB
/
str.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#pragma once
/*
c string implementation in pure C
AUTHOR: korvo
(korvonesto.com)
*/
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef STR_INLINE
#define STR_EXPORT static inline
#else
#define STR_EXPORT extern
#endif
// #define STR_IMPLEMENTATION
typedef struct {
char* ptr;
uint32_t capacity;
uint32_t len;
} str_t;
#define STR_STATIC_DEFN(str) \
(str_t) { \
.ptr = str, .capacity = sizeof(str) - 1, .len = sizeof(str) - 1 \
}
#define STR_IS_NULL(str) (str.ptr == NULL)
#define STR_MAKE_NULL \
(str_t) { \
.ptr = NULL, .capacity = 0, .len = 0 \
}
STR_EXPORT str_t str_create(void);
STR_EXPORT str_t str_generate(const char* format, ...);
STR_EXPORT str_t str_clone(const str_t* from);
STR_EXPORT str_t str_from_cstr_move(char**);
STR_EXPORT str_t str_from_cstr_clone(const char*);
// end must be one after whatever you want to copy
STR_EXPORT str_t str_from_range(const char* begin, const char* end);
STR_EXPORT const char* str_cstr(const str_t*);
STR_EXPORT void str_destroy(str_t*);
STR_EXPORT bool str_cmp(const str_t*, const str_t*);
STR_EXPORT bool str_cmp_cstr(const str_t*, const char*);
STR_EXPORT void str_append_char(str_t*, const char);
STR_EXPORT void str_append_cstr(str_t*, const char*);
STR_EXPORT void str_append(str_t*, const str_t* from);
STR_EXPORT void str_insert_char(str_t*, const char, const uint32_t idx);
STR_EXPORT void str_insert(str_t*, const str_t* from, const uint32_t idx);
STR_EXPORT char str_pop(str_t*);
STR_EXPORT bool str_starts_with(const str_t*, const str_t* startswith);
STR_EXPORT bool str_starts_with_cstr(const str_t*, const char* startswith);
STR_EXPORT bool str_ends_with(const str_t*, const str_t* endswith);
STR_EXPORT bool str_ends_with_cstr(const str_t*, const char* endswith);
#ifndef DEFAULT_STR_CAPACITY
#define DEFAULT_STR_CAPACITY 32
#endif
#ifndef DEFAULT_STR_GROWTH_FACTOR
#define DEFAULT_STR_GROWTH_FACTOR 1.5f
#endif
#ifdef STR_IMPLEMENTATION
#define STR_UNUSED(x) (void)x
static inline uint32_t str_cstrlen(const char* in) {
uint32_t len = 0;
while (in[len] != 0)
len += 1;
return len;
}
static inline void str_memcpy(char* to, const char* from, uint32_t len) {
for (uint32_t idx = 0; idx < len; idx++)
to[idx] = from[idx];
}
// continuously resizes a strings capacity until it is >= than fit_cap
static inline void str_fit(str_t* str, uint32_t fit_cap) {
uint32_t cap = str->capacity;
while (cap <= fit_cap) {
cap *= DEFAULT_STR_GROWTH_FACTOR;
}
str->capacity = cap;
str->ptr = realloc(str->ptr, cap);
}
STR_EXPORT str_t str_create(void) {
const str_t out = {
.ptr = malloc(DEFAULT_STR_CAPACITY),
.capacity = DEFAULT_STR_CAPACITY,
.len = 0,
};
return out;
}
STR_EXPORT str_t str_generate(const char* fmt, ...) {
va_list list, copy;
va_start(list, fmt);
va_copy(copy, list);
uint32_t len = vsnprintf(NULL, 0, fmt, list);
char* ptr = malloc(len + 1);
ptr[len] = 0;
vsnprintf(ptr, len + 1, fmt, copy);
va_end(list);
va_end(copy);
return (str_t){
.ptr = ptr,
.len = len,
.capacity = len,
};
}
STR_EXPORT str_t str_clone(const str_t* in) {
char* copy = malloc(in->len + 1);
copy[in->len] = 0;
str_memcpy(copy, in->ptr, in->len);
return (str_t){
.len = in->len,
.capacity = in->len,
.ptr = copy,
};
}
STR_EXPORT const char* str_cstr(const str_t* in) {
if (in->ptr[in->len] != 0)
in->ptr[in->len] = 0;
return in->ptr;
}
// clones the cstr
STR_EXPORT str_t str_from_cstr_clone(const char* in) {
const uint32_t len = str_cstrlen(in);
char* clone = malloc(len);
for (uint32_t i = 0; i < len; i++)
clone[i] = in[i];
const str_t out = {
.ptr = clone,
.capacity = len,
.len = len,
};
return out;
}
STR_EXPORT str_t str_from_cstr_move(char** in) {
char* cstr = *in;
*in = NULL;
const uint32_t len = str_cstrlen(cstr);
const str_t out = {
.ptr = cstr,
.capacity = len,
.len = len,
};
return out;
}
STR_EXPORT str_t str_from_range(const char* begin, const char* end) {
const uint32_t len = end - begin;
char* copy = malloc(len);
str_memcpy(copy, begin, len);
return (str_t){
.ptr = copy,
.capacity = len,
.len = len,
};
}
STR_EXPORT void str_destroy(str_t* str) {
if (str->ptr)
free(str->ptr);
}
STR_EXPORT bool str_cmp(const str_t* left, const str_t* right) {
if (left->len != right->len)
return false;
for (uint32_t i = 0; i < left->len; i++)
if (left->ptr[i] != right->ptr[i])
return false;
return true;
}
STR_EXPORT bool str_cmp_cstr(const str_t* left, const char* right) {
const uint32_t right_len = str_cstrlen(right);
if (left->len != right_len)
return false;
for (uint32_t i = 0; i < left->len; i++)
if (left->ptr[i] != right[i])
return false;
return true;
}
STR_EXPORT void str_append_char(str_t* str, char c) {
str_fit(str, str->len + 1);
str->ptr[str->len] = c;
str->len += 1;
}
STR_EXPORT void str_append_cstr(str_t* str, const char* from) {
const uint32_t from_len = str_cstrlen(from);
const uint32_t final_len = str->len + from_len;
str_fit(str, final_len);
for (uint32_t i = 0; i < from_len; i++)
str->ptr[i + str->len] = from[i];
str->len += from_len;
}
STR_EXPORT void str_append(str_t* str, const str_t* from) {
const uint32_t final_len = str->len + from->len;
str_fit(str, final_len);
for (uint32_t i = 0; i < from->len; i++)
str->ptr[i + str->len] = from->ptr[i];
str->len += from->len;
}
STR_EXPORT void str_insert_char(str_t* str, const char c, const uint32_t idx) {
str_fit(str, str->len += 1);
for (uint32_t i = str->len; i > idx; i--)
str->ptr[i] = str->ptr[i - 1];
str->ptr[idx] = c;
}
STR_EXPORT void str_insert(str_t* str, const str_t* from, const uint32_t idx) {
const uint32_t final_len = str->len + from->len;
str_fit(str, final_len);
// move all of the characters that are displaced over
for (uint32_t i = final_len; i >= idx; i--)
str->ptr[i] = str->ptr[i - from->len];
// now copy the other string into its new place
for (uint32_t i = str->len; i < final_len; i++)
str->ptr[i] = from->ptr[i];
str->len = final_len;
}
STR_EXPORT char str_pop(str_t* str) {
str->len -= 1;
return str->ptr[str->len];
}
STR_EXPORT bool str_starts_with(const str_t* str, const str_t* startswith) {
if (str->len < startswith->len)
return false;
for (uint32_t i = 0; i < startswith->len; i++) {
if (str->ptr[i] != startswith->ptr[i])
return false;
}
return true;
}
STR_EXPORT bool str_starts_with_cstr(const str_t* str, const char* startswith) {
const uint32_t startswith_len = str_cstrlen(startswith);
if (str->len < startswith_len)
return false;
for (uint32_t i = 0; i < startswith_len; i++) {
if (str->ptr[i] != startswith[i])
return false;
}
return true;
}
STR_EXPORT bool str_ends_with(const str_t* str, const str_t* endswith) {
if (str->len < endswith->len)
return false;
const uint32_t offset = str->len - endswith->len;
for (uint32_t i = 0; i < endswith->len; i++) {
if (str->ptr[offset + i] != endswith->ptr[i])
return false;
}
return true;
}
STR_EXPORT bool str_ends_with_cstr(const str_t* str, const char* endswith) {
const uint32_t endswith_len = str_cstrlen(endswith);
if (str->len < endswith_len)
return false;
const uint32_t offset = str->len - endswith_len;
for (uint32_t i = 0; i < endswith_len; i++) {
if (str->ptr[offset + i] != endswith[i])
return false;
}
return true;
}
#endif