-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsv.h
307 lines (249 loc) · 7.66 KB
/
sv.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
// Copyright 2021 Alexey Kutepov <reximkut@gmail.com>
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef SV_H_
#define SV_H_
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#ifndef SVDEF
#define SVDEF
#endif // SVDEF
typedef struct {
size_t count;
const char *data;
} String_View;
#define SV(cstr_lit) sv_from_parts(cstr_lit, sizeof(cstr_lit) - 1)
#define SV_STATIC(cstr_lit) \
{ \
sizeof(cstr_lit) - 1, \
(cstr_lit) \
}
#define SV_NULL sv_from_parts(NULL, 0)
// printf macros for String_View
#define SV_Fmt "%.*s"
#define SV_Arg(sv) (int) (sv).count, (sv).data
// USAGE:
// String_View name = ...;
// printf("Name: "SV_Fmt"\n", SV_Arg(name));
SVDEF String_View sv_from_parts(const char *data, size_t count);
SVDEF String_View sv_from_cstr(const char *cstr);
SVDEF String_View sv_trim_left(String_View sv);
SVDEF String_View sv_trim_right(String_View sv);
SVDEF String_View sv_trim(String_View sv);
SVDEF String_View sv_take_left_while(String_View sv, bool (*predicate)(char x));
SVDEF String_View sv_chop_by_delim(String_View *sv, char delim);
SVDEF String_View sv_chop_by_sv(String_View *sv, String_View thicc_delim);
SVDEF bool sv_try_chop_by_delim(String_View *sv, char delim, String_View *chunk);
SVDEF String_View sv_chop_left(String_View *sv, size_t n);
SVDEF String_View sv_chop_right(String_View *sv, size_t n);
SVDEF String_View sv_chop_left_while(String_View *sv, bool (*predicate)(char x));
SVDEF bool sv_index_of(String_View sv, char c, size_t *index);
SVDEF bool sv_eq(String_View a, String_View b);
SVDEF bool sv_eq_ignorecase(String_View a, String_View b);
SVDEF bool sv_starts_with(String_View sv, String_View prefix);
SVDEF bool sv_ends_with(String_View sv, String_View suffix);
SVDEF uint64_t sv_to_u64(String_View sv);
#endif // SV_H_
#ifdef SV_IMPLEMENTATION
SVDEF String_View sv_from_parts(const char *data, size_t count)
{
String_View sv;
sv.count = count;
sv.data = data;
return sv;
}
SVDEF String_View sv_from_cstr(const char *cstr)
{
return sv_from_parts(cstr, strlen(cstr));
}
SVDEF String_View sv_trim_left(String_View sv)
{
size_t i = 0;
while (i < sv.count && isspace(sv.data[i])) {
i += 1;
}
return sv_from_parts(sv.data + i, sv.count - i);
}
SVDEF String_View sv_trim_right(String_View sv)
{
size_t i = 0;
while (i < sv.count && isspace(sv.data[sv.count - 1 - i])) {
i += 1;
}
return sv_from_parts(sv.data, sv.count - i);
}
SVDEF String_View sv_trim(String_View sv)
{
return sv_trim_right(sv_trim_left(sv));
}
SVDEF String_View sv_chop_left(String_View *sv, size_t n)
{
if (n > sv->count) {
n = sv->count;
}
String_View result = sv_from_parts(sv->data, n);
sv->data += n;
sv->count -= n;
return result;
}
SVDEF String_View sv_chop_right(String_View *sv, size_t n)
{
if (n > sv->count) {
n = sv->count;
}
String_View result = sv_from_parts(sv->data + sv->count - n, n);
sv->count -= n;
return result;
}
SVDEF bool sv_index_of(String_View sv, char c, size_t *index)
{
size_t i = 0;
while (i < sv.count && sv.data[i] != c) {
i += 1;
}
if (i < sv.count) {
if (index) {
*index = i;
}
return true;
} else {
return false;
}
}
SVDEF bool sv_try_chop_by_delim(String_View *sv, char delim, String_View *chunk)
{
size_t i = 0;
while (i < sv->count && sv->data[i] != delim) {
i += 1;
}
String_View result = sv_from_parts(sv->data, i);
if (i < sv->count) {
sv->count -= i + 1;
sv->data += i + 1;
if (chunk) {
*chunk = result;
}
return true;
}
return false;
}
SVDEF String_View sv_chop_by_delim(String_View *sv, char delim)
{
size_t i = 0;
while (i < sv->count && sv->data[i] != delim) {
i += 1;
}
String_View result = sv_from_parts(sv->data, i);
if (i < sv->count) {
sv->count -= i + 1;
sv->data += i + 1;
} else {
sv->count -= i;
sv->data += i;
}
return result;
}
SVDEF String_View sv_chop_by_sv(String_View *sv, String_View thicc_delim)
{
String_View window = sv_from_parts(sv->data, thicc_delim.count);
size_t i = 0;
while (i + thicc_delim.count < sv->count
&& !(sv_eq(window, thicc_delim)))
{
i++;
window.data++;
}
String_View result = sv_from_parts(sv->data, i);
if (i + thicc_delim.count == sv->count) {
// include last <thicc_delim.count> characters if they aren't
// equal to thicc_delim
result.count += thicc_delim.count;
}
// Chop!
sv->data += i + thicc_delim.count;
sv->count -= i + thicc_delim.count;
return result;
}
SVDEF bool sv_starts_with(String_View sv, String_View expected_prefix)
{
if (expected_prefix.count <= sv.count) {
String_View actual_prefix = sv_from_parts(sv.data, expected_prefix.count);
return sv_eq(expected_prefix, actual_prefix);
}
return false;
}
SVDEF bool sv_ends_with(String_View sv, String_View expected_suffix)
{
if (expected_suffix.count <= sv.count) {
String_View actual_suffix = sv_from_parts(sv.data + sv.count - expected_suffix.count, expected_suffix.count);
return sv_eq(expected_suffix, actual_suffix);
}
return false;
}
SVDEF bool sv_eq(String_View a, String_View b)
{
if (a.count != b.count) {
return false;
} else {
return memcmp(a.data, b.data, a.count) == 0;
}
}
SVDEF bool sv_eq_ignorecase(String_View a, String_View b)
{
if (a.count != b.count) {
return false;
}
char x, y;
for (size_t i = 0; i < a.count; i++) {
x = 'A' <= a.data[i] && a.data[i] <= 'Z'
? a.data[i] + 32
: a.data[i];
y = 'A' <= b.data[i] && b.data[i] <= 'Z'
? b.data[i] + 32
: b.data[i];
if (x != y) return false;
}
return true;
}
SVDEF uint64_t sv_to_u64(String_View sv)
{
uint64_t result = 0;
for (size_t i = 0; i < sv.count && isdigit(sv.data[i]); ++i) {
result = result * 10 + (uint64_t) sv.data[i] - '0';
}
return result;
}
SVDEF String_View sv_chop_left_while(String_View *sv, bool (*predicate)(char x))
{
size_t i = 0;
while (i < sv->count && predicate(sv->data[i])) {
i += 1;
}
return sv_chop_left(sv, i);
}
SVDEF String_View sv_take_left_while(String_View sv, bool (*predicate)(char x))
{
size_t i = 0;
while (i < sv.count && predicate(sv.data[i])) {
i += 1;
}
return sv_from_parts(sv.data, i);
}
#endif // SV_IMPLEMENTATION