bad char
[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 void vg_strcatf( vg_str *str, const char *fmt, ... );
38
39 /*
40 * Append character to vg_str
41 */
42 void vg_strcatch( vg_str *str, char c );
43 void vg_strcati32( vg_str *str, i32 value );
44 void vg_strcati32r( vg_str *str, i32 value, i32 n, char alt );
45 /*
46 * Returns 1 if string did not overflow while building
47 */
48 int vg_strgood( vg_str *str );
49
50 /*
51 * Returns pointer to last instance of character
52 */
53 char *vg_strch( vg_str *str, char c );
54
55 enum strncpy_behaviour
56 {
57 k_strncpy_always_add_null = 0,
58 k_strncpy_allow_cutoff = 1,
59 k_strncpy_overflow_fatal = 2
60 };
61
62 u32 vg_strncpy( const char *src, char *dst, u32 len,
63 enum strncpy_behaviour behaviour );
64 u32 vg_strdjb2( const char *str );
65 int vg_strdjb2_eq( const char *s1, u32 h1, const char *s2, u32 h2 );
66
67 #define VG_STRDJB2_EQ( CS1, S2, H2 ) \
68 vg_strdjb2_eq( CS1, vg_strdjb2(CS1), S2, H2 )