build system revision
[vg.git] / vg_string.h
1 #pragma once
2 #include "vg_platform.h"
3
4 /* string builder with optional dynamic memory or static buffer. */
5
6 typedef struct vg_str vg_str;
7 typedef struct vg_str_dynamic vg_str_dynamic;
8
9 struct vg_str
10 {
11 char *buffer;
12 i32 i, /* -1: error condition. otherwise, current cursor position */
13 len; /* -1: dynamically allocated. otherwise, buffer length */
14 };
15
16 struct vg_str_dynamic
17 {
18 i32 len;
19 };
20
21 /*
22 * Returns the current storage size of the string
23 */
24 i32 vg_str_storage( vg_str *str );
25
26 /*
27 * Reset string. If len is -1 (dynamically allocated), buffer must be either
28 * NULL or be acquired from malloc or realloc
29 */
30 void vg_strnull( vg_str *str, char *buffer, i32 len );
31 void vg_strfree( vg_str *str );
32
33 /*
34 * Append null terminated string to vg_str
35 */
36 void vg_strcat( vg_str *str, const char *append );
37
38 /*
39 * Append character to vg_str
40 */
41 void vg_strcatch( vg_str *str, char c );
42 void vg_strcati32( vg_str *str, i32 value );
43 void vg_strcati32r( vg_str *str, i32 value, i32 n, char alt );
44 /*
45 * Returns 1 if string did not overflow while building
46 */
47 int vg_strgood( vg_str *str );
48
49 /*
50 * Returns pointer to last instance of character
51 */
52 char *vg_strch( vg_str *str, char c );
53
54 enum strncpy_behaviour
55 {
56 k_strncpy_always_add_null = 0,
57 k_strncpy_allow_cutoff = 1,
58 k_strncpy_overflow_fatal = 2
59 };
60
61 u32 vg_strncpy( const char *src, char *dst, u32 len,
62 enum strncpy_behaviour behaviour );
63 u32 vg_strdjb2( const char *str );
64 int vg_strdjb2_eq( const char *s1, u32 h1, const char *s2, u32 h2 );
65
66 #define VG_STRDJB2_EQ( CS1, S2, H2 ) \
67 vg_strdjb2_eq( CS1, vg_strdjb2(CS1), S2, H2 )