-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlb_string.h
158 lines (135 loc) · 3.26 KB
/
dlb_string.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
#ifndef DLB_STRING_H
#define DLB_STRING_H
#include "dlb_types.h"
// str: nil-terminated string
// returns: length of string, not including the nil terminator
static inline u32 dlb_str_len(const char *str)
{
u32 len = 0;
while (*str++) {
len++;
}
return len;
}
// str: nil-terminated string or NULL
// prefix: nil-terminated string or NULL
// returns: 1 if str starts with entire prefix, 0 otherwise
static inline int dlb_str_startswith(const char *str, const char *prefix)
{
if (!str || !prefix) {
return 0;
}
const char *str_ptr = str;
const char *prefix_ptr = prefix;
while (*str_ptr && *prefix_ptr && *str_ptr == *prefix_ptr) {
str_ptr++;
prefix_ptr++;
}
return *prefix_ptr == 0;
}
// str: nil-terminated string
// c: character to replace
// new_c: character to replace with
static inline void dlb_str_replace_char(char *str, char c, char new_c)
{
char *str_ptr = str;
while (*str_ptr) {
if (*str_ptr == c) {
*str_ptr = new_c;
}
str_ptr++;
}
}
#if 0
#define STR(str) (const struct dlb_string) { str, sizeof(str) }
#define STRL(str, len) (const struct dlb_string) { str, len }
struct dlb_string
{
const char *s;
u32 len;
};
// NOTE: Don't remember what these do.. not well tested. Test before using.
static inline char *dlb_strsep_c(char **stringp, const char delim)
{
char *start = *stringp;
while (**stringp) {
if (**stringp == delim) {
**stringp = '\0';
(*stringp)++;
break;
}
(*stringp)++;
}
return start;
}
static inline char *dlb_strsep(char **stringp, const char *delims)
{
char *start = *stringp;
const char *delim;
while (**stringp) {
delim = delims;
while (delim) {
if (**stringp == *delim) {
**stringp = '\0';
(*stringp)++;
return start;
}
delim++;
}
(*stringp)++;
}
return start;
}
// str: nil-terminated string containing only digits 0-9
// returns: number contained in string as long
static inline long dlb_atol(const char *str)
{
if (!str) return 0;
long val = 0;
while(*str) {
val = val*10 + (*str++ - '0');
}
return val;
}
#endif
#if 0
struct dlb_string
{
u32 buffer_len;
const char *str;
};
struct dlb_string *dlb_string_alloc(const char *str)
{
u32 buffer_len = strlen(str);
struct dlb_string *s = calloc(1, sizeof(struct dlb_string) + strlen(str) +
1);
s->buffer_len = buffer_len;
s->str = s + 1;
return s;
}
typedef struct Intern {
size_t len;
const char *str;
} Intern;
Arena str_arena;
Intern *interns;
const char *str_intern_range(const char *start, const char *end)
{
size_t len = end - start;
for (Intern *it = interns; it != buf_end(interns); it++) {
if (it->len == len && strncmp(it->str, start, len) == 0) {
return it->str;
}
}
char *str = arena_alloc(&str_arena, len + 1);
memcpy(str, start, len);
str[len] = 0;
buf_push(interns, (Intern) { len, str });
return str;
}
const char *str_intern(const char *str)
{
return str_intern_range(str, str + strlen(str));
}
#endif
#endif