liblibgb.c 1.2021.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/gb/gb_string.h ADDED
@@ -0,0 +1,511 @@
1
+ /* gb_string.h - v0.95a - public domain string library - no warranty implied; use at your own risk
2
+ A Simple Dynamic Strings Library for C and C++
3
+
4
+ Version History:
5
+ 0.95b - Fix issue #21
6
+ 0.95a - Change brace style because why not?
7
+ 0.95 - C90 Support
8
+ 0.94 - Remove "declare anywhere"
9
+ 0.93 - Fix typos and errors
10
+ 0.92 - Add extern "C" if compiling as C++
11
+ 0.91 - Remove `char * cstr` from String_Header
12
+ 0.90 - Initial Version
13
+
14
+ LICENSE
15
+
16
+ This software is in the public domain. Where that dedication is not
17
+ recognized, you are granted a perpetual, irrevocable license to copy,
18
+ distribute, and modify this file as you see fit.
19
+
20
+ How to use:
21
+
22
+ Do this:
23
+ #define GB_STRING_IMPLEMENTATION
24
+ before you include this file in *one* C++ file to create the implementation
25
+
26
+ i.e. it should look like this:
27
+ #include ...
28
+ #include ...
29
+ #include ...
30
+ #define GB_STRING_IMPLEMENTATION
31
+ #include "gb_string.h"
32
+
33
+ You can #define GB_ALLOC, and GB_FREE to avoid using malloc,free.
34
+
35
+ If you prefer to use C++, you can use all the same functions in a
36
+ namespace instead, do this:
37
+ #define GB_STRING_CPP
38
+ before you include the header file
39
+
40
+ i.e it should look like this:
41
+ #define GB_STRING_CPP
42
+ #include "gb_string.h"
43
+
44
+ The C++ version has the advantage that you do not need to reassign variables
45
+ i.e.
46
+
47
+ C version
48
+ str = gb_append_cstring(str, "another string");
49
+ C++ version
50
+ gb::append_cstring(str, "another string");
51
+
52
+ This could be achieved in C by passing a pointer to the string but for
53
+ simplicity and consistency, reassigning the variable is better.
54
+
55
+ Reasoning:
56
+
57
+ By default, strings in C are null terminated which means you have to count
58
+ the number of character up to the null character to calculate the length.
59
+ Many "better" C string libraries will create a struct for a string.
60
+ i.e.
61
+
62
+ struct String {
63
+ size_t length;
64
+ size_t capacity;
65
+ char * cstring;
66
+ };
67
+
68
+ This library tries to augment normal C strings in a better way that is still
69
+ compatible with C-style strings.
70
+
71
+ +--------+-----------------------+-----------------+
72
+ | Header | Binary C-style String | Null Terminator |
73
+ +--------+-----------------------+-----------------+
74
+ |
75
+ +-> Pointer returned by functions
76
+
77
+ Due to the meta-data being stored before the string pointer and every gb string
78
+ having an implicit null terminator, gb strings are full compatible with c-style
79
+ strings and read-only functions.
80
+
81
+ Advantages:
82
+
83
+ * gb strings can be passed to C-style string functions without accessing a struct
84
+ member of calling a function, i.e.
85
+
86
+ printf("%s\n", gb_str);
87
+
88
+ Many other libraries do either of these:
89
+
90
+ printf("%s\n", string->cstr);
91
+ printf("%s\n", get_cstring(string));
92
+
93
+ * You can access each character just like a C-style string:
94
+
95
+ printf("%c %c\n", str[0], str[13]);
96
+
97
+ * gb strings are singularly allocated. The meta-data is next to the character
98
+ array which is better for the cache.
99
+
100
+ Disadvantages:
101
+
102
+ * In the C version of these functions, many return the new string. i.e.
103
+
104
+ str = gb_append_cstring(str, "another string");
105
+
106
+ In the C++ version, this is made easier with the use of references. i.e.
107
+
108
+ gb::append_cstring(str, "another string");
109
+
110
+ * Custom allocators must redefine GB_ALLOC and GB_FREE which can be annoying.
111
+ realloc is not used for compatibility with many custom allocators that do not
112
+ have a reallocation function.
113
+
114
+ * This is not compatible with the "gb.h" gbString. That version is a better version
115
+ as it allows for custom allocators.
116
+
117
+ */
118
+
119
+ /* Examples: */
120
+ /* C example */
121
+ #if 0
122
+ #include <stdio.h>
123
+ #include <stdlib.h>
124
+
125
+ #define GB_STRING_IMPLEMENTATION
126
+ #include "gb_string.h"
127
+
128
+ int main(int argc, char **argv) {
129
+ gbString str = gb_make_string("Hello");
130
+ gbString other_str = gb_make_string_length(", ", 2);
131
+ str = gb_append_string(str, other_str);
132
+ str = gb_append_cstring(str, "world!");
133
+
134
+ printf("%s\n", str); // Hello, world!
135
+
136
+ printf("str length = %d\n", gb_string_length(str));
137
+
138
+ str = gb_set_string(str, "Potato soup");
139
+ printf("%s\n", str); // Potato soup
140
+
141
+ str = gb_set_string(str, "Hello");
142
+ other_str = gb_set_string(other_str, "Pizza");
143
+ if (gb_strings_are_equal(str, other_str))
144
+ printf("Not called\n");
145
+ else
146
+ printf("Called\n");
147
+
148
+ str = gb_set_string(str, "Ab.;!...AHello World ??");
149
+ str = gb_trim_string(str, "Ab.;!. ?");
150
+ printf("%s\n", str); // "Hello World"
151
+
152
+ gb_free_string(str);
153
+ gb_free_string(other_str);
154
+
155
+ }
156
+ #endif
157
+
158
+ /* C++ example */
159
+ #if 0
160
+ #include <stdio.h>
161
+ #include <stdlib.h>
162
+
163
+ #define GB_STRING_CPP
164
+ #define GB_STRING_IMPLEMENTATION
165
+ #include "gb_string.h"
166
+
167
+ int main(int argc, char **argv) {
168
+ using namespace gb;
169
+
170
+ String str = make_string("Hello");
171
+ String other_str = make_string(", ", 2);
172
+ append_string(str, other_str);
173
+ append_cstring(str, "world!");
174
+
175
+ printf("%s\n", str); /* Hello, world! */
176
+
177
+ printf("str length = %d\n", string_length(str));
178
+
179
+ set_string(str, "Potato soup");
180
+ printf("%s\n", str); /* Potato soup */
181
+
182
+ set_string(str, "Hello");
183
+ set_string(other_str, "Pizza");
184
+ if (strings_are_equal(str, other_str))
185
+ printf("Not called\n");
186
+ else
187
+ printf("Called\n");
188
+
189
+ set_string(str, "Ab.;!...AHello World ??");
190
+ trim_string(str, "Ab.;!. ?");
191
+ printf("%s\n", str); /* "Hello World" */
192
+
193
+ free_string(str);
194
+ free_string(other_str);
195
+ }
196
+ #endif
197
+
198
+ #ifndef GB_STRING_INCLUDE_GB_STRING_H
199
+ #define GB_STRING_INCLUDE_GB_STRING_H
200
+
201
+ #ifndef GB_ALLOC
202
+ #define GB_ALLOC(sz) malloc(sz)
203
+ #define GB_FREE(ptr) free(ptr)
204
+ #endif
205
+
206
+ #ifndef _MSC_VER
207
+ #ifdef __cplusplus
208
+ #define gb_inline inline
209
+ #else
210
+ #define gb_inline
211
+ #endif
212
+ #else
213
+ #define gb_inline __forceinline
214
+ #endif
215
+
216
+ #include <string.h> /* Needed for memcpy and cstring functions */
217
+
218
+ #ifdef __cplusplus
219
+ extern "C" {
220
+ #endif
221
+
222
+ typedef char *gbString;
223
+
224
+ typedef int gbBool;
225
+ #if !defined(GB_TRUE) || !defined(GB_FALSE)
226
+ #define GB_TRUE 1
227
+ #define GB_FALSE 0
228
+ #endif
229
+
230
+ #ifndef GB_SIZE_TYPE
231
+ #define GB_SIZE_TYPE
232
+ typedef size_t gbUsize;
233
+ #endif
234
+
235
+ #ifndef GB_NULLPTR
236
+ #if __cplusplus
237
+ #if __cplusplus >= 201103L
238
+ #define GB_NULLPTR nullptr
239
+ #else
240
+ #define GB_NULLPTR 0
241
+ #endif
242
+ #else
243
+ #define GB_NULLPTR (void*)0
244
+ #endif
245
+ #endif
246
+
247
+ typedef struct gbStringHeader {
248
+ gbUsize len;
249
+ gbUsize cap;
250
+ } gbStringHeader;
251
+
252
+ #define GB_STRING_HEADER(s) ((gbStringHeader *)s - 1)
253
+
254
+ gbString gb_make_string(char const *str);
255
+ gbString gb_make_string_length(void const *str, gbUsize len);
256
+ void gb_free_string(gbString str);
257
+
258
+ gbString gb_duplicate_string(gbString const str);
259
+
260
+ gbUsize gb_string_length(gbString const str);
261
+ gbUsize gb_string_capacity(gbString const str);
262
+ gbUsize gb_string_available_space(gbString const str);
263
+
264
+ void gb_clear_string(gbString str);
265
+
266
+ gbString gb_append_string_length(gbString str, void const *other, gbUsize len);
267
+ gbString gb_append_string(gbString str, gbString const other);
268
+ gbString gb_append_cstring(gbString str, char const *other);
269
+
270
+ gbString gb_set_string(gbString str, char const *cstr);
271
+
272
+ gbString gb_string_make_space_for(gbString str, gbUsize add_len);
273
+ gbUsize gb_string_allocation_size(gbString const str);
274
+
275
+ gbBool gb_strings_are_equal(gbString const lhs, gbString const rhs);
276
+
277
+ gbString gb_trim_string(gbString str, char const *cut_set);
278
+
279
+
280
+ #ifdef __cplusplus
281
+ }
282
+ #endif
283
+
284
+ #if defined(GB_STRING_CPP)
285
+
286
+ #if !defined(__cplusplus)
287
+ #error You need to compile as C++ for the C++ version of gb_string.h to work
288
+ #endif
289
+
290
+ namespace gb
291
+ {
292
+ typedef gbString String;
293
+ typedef gbUsize usize;
294
+
295
+ gb_inline String make_string(char const *str = "") { return gb_make_string(str); }
296
+ gb_inline String make_string(void const *str, usize len) { return gb_make_string_length(str, len); }
297
+ gb_inline void free_string(String& str) { gb_free_string(str); str = GB_NULLPTR; }
298
+ gb_inline String duplicate_string(const String str) { return gb_duplicate_string(str); }
299
+ gb_inline usize string_length(const String str) { return gb_string_length(str); }
300
+ gb_inline usize string_capacity(const String str) { return gb_string_capacity(str); }
301
+ gb_inline usize string_available_space(const String str) { return gb_string_available_space(str); }
302
+ gb_inline void clear_string(String str) { gb_clear_string(str); }
303
+ gb_inline void append_string_length(String& str, void const *other, usize len) { str = gb_append_string_length(str, other, len); }
304
+ gb_inline void append_string(String& str, const String other) { str = gb_append_string(str, other); }
305
+ gb_inline void append_cstring(String& str, char const *other) { str = gb_append_cstring(str, other); }
306
+ gb_inline void set_string(String& str, char const *cstr) { str = gb_set_string(str, cstr); }
307
+ gb_inline void string_make_space_for(String& str, usize add_len) { str = gb_string_make_space_for(str, add_len); }
308
+ gb_inline usize string_allocation_size(const String str) { return gb_string_allocation_size(str); }
309
+ gb_inline bool strings_are_equal(const String lhs, const String rhs) { return gb_strings_are_equal(lhs, rhs) == GB_TRUE; }
310
+ gb_inline void trim_string(String& str, char const *cut_set) { str = gb_trim_string(str, cut_set); }
311
+ } /* namespace gb */
312
+ #endif /* GB_STRING_CPP */
313
+ #endif /* GB_STRING_H */
314
+ #ifdef GB_STRING_IMPLEMENTATION
315
+ static void gb_set_string_length(gbString str, gbUsize len) {
316
+ GB_STRING_HEADER(str)->len = len;
317
+ }
318
+
319
+ static void gb_set_string_capacity(gbString str, gbUsize cap) {
320
+ GB_STRING_HEADER(str)->cap = cap;
321
+ }
322
+
323
+
324
+ gbString gb_make_string_length(void const *init_str, gbUsize len) {
325
+ gbString str;
326
+ gbStringHeader *header;
327
+ gbUsize header_size = sizeof(gbStringHeader);
328
+ void *ptr = GB_ALLOC(header_size + len + 1);
329
+ if (ptr == GB_NULLPTR)
330
+ return GB_NULLPTR;
331
+ if (!init_str)
332
+ memset(ptr, 0, header_size + len + 1);
333
+
334
+ str = (char *)ptr + header_size;
335
+ header = GB_STRING_HEADER(str);
336
+ header->len = len;
337
+ header->cap = len;
338
+ if (len && init_str)
339
+ memcpy(str, init_str, len);
340
+ str[len] = '\0';
341
+
342
+ return str;
343
+ }
344
+
345
+ gbString gb_make_string(char const *str) {
346
+ gbUsize len = str ? strlen(str) : 0;
347
+ return gb_make_string_length(str, len);
348
+ }
349
+
350
+ void gb_free_string(gbString str) {
351
+ if (str == GB_NULLPTR)
352
+ return;
353
+
354
+ GB_FREE((gbStringHeader *)str - 1);
355
+ }
356
+
357
+ gbString gb_duplicate_string(gbString const str) {
358
+ return gb_make_string_length(str, gb_string_length(str));
359
+ }
360
+
361
+ gbUsize gb_string_length(gbString const str) {
362
+ return GB_STRING_HEADER(str)->len;
363
+ }
364
+
365
+ gbUsize gb_string_capacity(gbString const str) {
366
+ return GB_STRING_HEADER(str)->cap;
367
+ }
368
+
369
+ gbUsize gb_string_available_space(gbString const str) {
370
+ gbStringHeader *h = GB_STRING_HEADER(str);
371
+ if (h->cap > h->len)
372
+ return h->cap - h->len;
373
+ return 0;
374
+ }
375
+
376
+ void gb_clear_string(gbString str) {
377
+ gb_set_string_length(str, 0);
378
+ str[0] = '\0';
379
+ }
380
+
381
+ gbString gb_append_string_length(gbString str, void const *other, gbUsize other_len) {
382
+ gbUsize curr_len = gb_string_length(str);
383
+
384
+ str = gb_string_make_space_for(str, other_len);
385
+ if (str == GB_NULLPTR)
386
+ return GB_NULLPTR;
387
+
388
+ memcpy(str + curr_len, other, other_len);
389
+ str[curr_len + other_len] = '\0';
390
+ gb_set_string_length(str, curr_len + other_len);
391
+
392
+ return str;
393
+ }
394
+
395
+ gbString gb_append_string(gbString str, gbString const other) {
396
+ return gb_append_string_length(str, other, gb_string_length(other));
397
+ }
398
+
399
+ gbString gb_append_cstring(gbString str, char const *other) {
400
+ return gb_append_string_length(str, other, strlen(other));
401
+ }
402
+
403
+ gbString gb_set_string(gbString str, char const *cstr) {
404
+ gbUsize len = strlen(cstr);
405
+ if (gb_string_capacity(str) < len) {
406
+ str = gb_string_make_space_for(str, len - gb_string_length(str));
407
+ if (str == GB_NULLPTR)
408
+ return GB_NULLPTR;
409
+ }
410
+
411
+ memcpy(str, cstr, len);
412
+ str[len] = '\0';
413
+ gb_set_string_length(str, len);
414
+
415
+ return str;
416
+ }
417
+
418
+ static gb_inline void *gb__string_realloc(void *ptr, gbUsize old_size, gbUsize new_size) {
419
+ void *new_ptr;
420
+ if (!ptr)
421
+ return GB_ALLOC(new_size);
422
+
423
+ if (new_size < old_size)
424
+ new_size = old_size;
425
+
426
+ if (old_size == new_size)
427
+ return ptr;
428
+
429
+ new_ptr = GB_ALLOC(new_size);
430
+ if (!new_ptr)
431
+ return GB_NULLPTR;
432
+
433
+ memcpy(new_ptr, ptr, old_size);
434
+
435
+ GB_FREE(ptr);
436
+
437
+ return new_ptr;
438
+ }
439
+
440
+ gbString gb_string_make_space_for(gbString str, gbUsize add_len) {
441
+ gbUsize len = gb_string_length(str);
442
+ gbUsize new_len = len + add_len;
443
+ void *ptr, *new_ptr;
444
+ gbUsize available, old_size, new_size;
445
+
446
+ available = gb_string_available_space(str);
447
+ if (available >= add_len) /* Return if there is enough space left */
448
+ return str;
449
+
450
+
451
+ ptr = (char *)str - sizeof(gbStringHeader);
452
+ old_size = sizeof(gbStringHeader) + gb_string_length(str) + 1;
453
+ new_size = sizeof(gbStringHeader) + new_len + 1;
454
+
455
+ new_ptr = gb__string_realloc(ptr, old_size, new_size);
456
+ if (new_ptr == GB_NULLPTR)
457
+ return GB_NULLPTR;
458
+ str = (char *)new_ptr + sizeof(gbStringHeader);
459
+
460
+ gb_set_string_capacity(str, new_len);
461
+
462
+ return str;
463
+ }
464
+
465
+ gbUsize gb_string_allocation_size(gbString const s) {
466
+ gbUsize cap = gb_string_capacity(s);
467
+ return sizeof(gbStringHeader) + cap;
468
+ }
469
+
470
+ gbBool gb_strings_are_equal(gbString const lhs, gbString const rhs) {
471
+ gbUsize lhs_len, rhs_len, i;
472
+ lhs_len = gb_string_length(lhs);
473
+ rhs_len = gb_string_length(rhs);
474
+ if (lhs_len != rhs_len)
475
+ return GB_FALSE;
476
+
477
+ for (i = 0; i < lhs_len; i++) {
478
+ if (lhs[i] != rhs[i])
479
+ return GB_FALSE;
480
+ }
481
+
482
+ return GB_TRUE;
483
+ }
484
+
485
+ gbString gb_trim_string(gbString str, char const *cut_set) {
486
+ char *start, *end, *start_pos, *end_pos;
487
+ gbUsize len;
488
+
489
+ start_pos = start = str;
490
+ end_pos = end = str + gb_string_length(str) - 1;
491
+
492
+ while (start_pos <= end && strchr(cut_set, *start_pos))
493
+ start_pos++;
494
+ while (end_pos > start_pos && strchr(cut_set, *end_pos))
495
+ end_pos--;
496
+
497
+ len = (start_pos > end_pos) ? 0 : ((end_pos - start_pos)+1);
498
+
499
+ if (str != start_pos)
500
+ memmove(str, start_pos, len);
501
+ str[len] = '\0';
502
+
503
+ gb_set_string_length(str, len);
504
+
505
+ return str;
506
+ }
507
+
508
+
509
+
510
+ #endif /* GB_STRING_IMPLEMENTATION */
511
+
package/gb.h ADDED
@@ -0,0 +1,15 @@
1
+ #pragma once
2
+ #define GB_IMPLEMENTATION
3
+ #include "gb/gb.h"
4
+
5
+ #define GB_MATH_IMPLEMENTATION
6
+ #include "gb/gb_math.h"
7
+
8
+ #define GB_STRING_IMPLEMENTATION
9
+ #include "gb/gb_string.h"
10
+
11
+ #define GB_INI_IMPLEMENTATION
12
+ #include "gb/gb_ini.h"
13
+
14
+ #define GBGL_IMPLEMENTATION
15
+ #include "gb/gb_gl.h"
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "liblibgb.c",
3
+ "version": "1.2021.3",
4
+ "description": "gb single-file public domain libraries for C & C++; gingerBill (2015).",
5
+ "keywords": [
6
+ "c",
7
+ "library",
8
+ "single-file",
9
+ "header-only",
10
+ "public-domain",
11
+ "gingerBill"
12
+ ],
13
+ "homepage": "https://github.com/nodef/libgb.c#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/nodef/libgb.c/issues"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/nodef/libgb.c.git"
20
+ },
21
+ "license": "CC0-1.0",
22
+ "main": "libgb.h",
23
+ "dependencies": {
24
+ "stb.c": "^2.17.1"
25
+ }
26
+ }