-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_sv.h
226 lines (181 loc) · 5.5 KB
/
os_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
#ifndef OSSV_INCLUDE_OS_SV_H
#define OSSV_INCLUDE_OS_SV_H
#include <stdbool.h>
#include <stddef.h>
#ifndef OSSVDEF
#ifdef OSSV_STATIC
#define OSSVDEF static
#else
#define OSSVDEF extern
#endif
#endif
typedef struct
{
const char *data;
size_t length;
} os_string_view_t;
#define OSSV_FMT "%.*s"
#define OSSV_ARG(sv) (int) (sv).length, (sv).data
#define OSSV_PARG(sv) (int) (sv)->length, (sv)->data
OSSVDEF os_string_view_t ossv_new(const char *data, size_t length);
OSSVDEF os_string_view_t ossv_from_cstr(const char *str);
OSSVDEF os_string_view_t ossv_trim_left(os_string_view_t sv);
OSSVDEF os_string_view_t ossv_trim_right(os_string_view_t sv);
OSSVDEF os_string_view_t ossv_trim(os_string_view_t sv);
OSSVDEF os_string_view_t ossv_chop_by_delimeter(os_string_view_t *sv,
char delimiter);
OSSVDEF bool ossv_equal(os_string_view_t sv, os_string_view_t other);
OSSVDEF bool ossv_equal_cstr(os_string_view_t sv, const char *str);
OSSVDEF bool ossv_starts_with(os_string_view_t sv, os_string_view_t other);
OSSVDEF bool ossv_starts_with_cstr(os_string_view_t sv, const char *str);
OSSVDEF bool ossv_ends_with(os_string_view_t sv, os_string_view_t other);
OSSVDEF bool ossv_ends_with_cstr(os_string_view_t sv, const char *str);
typedef bool (*ossv_predicate)(char character);
OSSVDEF bool ossv_all(os_string_view_t sv, ossv_predicate predicate);
OSSVDEF bool ossv_any(os_string_view_t sv, ossv_predicate predicate);
OSSVDEF bool ossv_is_empty(os_string_view_t sv);
OSSVDEF bool ossv_is_alpha(os_string_view_t sv);
OSSVDEF bool ossv_is_alnum(os_string_view_t sv);
OSSVDEF bool ossv_is_digit(os_string_view_t sv);
#endif
#ifdef OS_SV_IMPLEMENTATION
#include <ctype.h>
#include <string.h>
OSSVDEF os_string_view_t ossv_new(const char *data, size_t length)
{
return (os_string_view_t) {.data = data, .length = length};
}
OSSVDEF os_string_view_t ossv_from_cstr(const char *str)
{
return (os_string_view_t) {.data = str, .length = strlen(str)};
}
OSSVDEF os_string_view_t ossv_trim_left(os_string_view_t sv)
{
while (sv.length && isspace(*sv.data))
{
++sv.data;
--sv.length;
}
return sv;
}
OSSVDEF os_string_view_t ossv_trim_right(os_string_view_t sv)
{
while (sv.length && isspace(sv.data[sv.length - 1]))
{
--sv.length;
}
return sv;
}
OSSVDEF os_string_view_t ossv_trim(os_string_view_t sv)
{
return ossv_trim_right(ossv_trim_left(sv));
}
OSSVDEF os_string_view_t ossv_chop_by_delimeter(os_string_view_t *sv,
char delimiter)
{
os_string_view_t out = *sv;
while (sv->length && sv->data[0] != delimiter)
{
++sv->data;
--sv->length;
}
out.length -= sv->length;
if (sv->length && sv->data[0] == delimiter)
{
++sv->data;
--sv->length;
}
return out;
}
OSSVDEF bool ossv_equal(os_string_view_t sv, os_string_view_t other)
{
if (sv.length != other.length)
{
return false;
}
return 0 == strncmp(sv.data, other.data, sv.length);
}
OSSVDEF bool ossv_equal_cstr(os_string_view_t sv, const char *str)
{
return ossv_equal(sv, ossv_from_cstr(str));
}
OSSVDEF bool ossv_starts_with(os_string_view_t sv, os_string_view_t other)
{
if (other.length > sv.length)
return false;
while (other.length)
{
if (*sv.data != *other.data)
return false;
++sv.data;
++other.data;
--sv.length;
--other.length;
}
return true;
}
OSSVDEF bool ossv_starts_with_cstr(os_string_view_t sv, const char *str)
{
return ossv_starts_with(sv, ossv_from_cstr(str));
}
OSSVDEF bool ossv_ends_with(os_string_view_t sv, os_string_view_t other)
{
if (other.length > sv.length)
return false;
while (other.length)
{
if (sv.data[sv.length - 1] != other.data[other.length - 1])
return false;
--sv.length;
--other.length;
}
return true;
}
OSSVDEF bool ossv_ends_with_cstr(os_string_view_t sv, const char *str)
{
return ossv_ends_with(sv, ossv_from_cstr(str));
}
OSSVDEF bool ossv_is_empty(os_string_view_t sv)
{
return sv.length == 0;
}
OSSVDEF _Bool ossv_all(os_string_view_t sv, ossv_predicate predicate)
{
for (size_t i = 0; i < sv.length; ++i)
{
if (!predicate(sv.data[i]))
{
return false;
}
}
return true;
}
OSSVDEF bool ossv_any(os_string_view_t sv, ossv_predicate predicate)
{
for (size_t i = 0; i < sv.length; ++i)
{
if (predicate(sv.data[i]))
{
return true;
}
}
return false;
}
#define _retype_predicate(wide_funcname, ctype_funcname) \
static bool wide_funcname(char c) \
{ \
return 0 != ctype_funcname((int) c); \
}
_retype_predicate(is_alpha, isalpha)
_retype_predicate(is_alnum, isalnum)
_retype_predicate(is_digit, isdigit)
#define _ossv_predicate(ossv_funcname, predicate_funcname) \
OSSVDEF bool ossv_funcname(os_string_view_t sv) \
{ \
return ossv_all(sv, (ossv_predicate) predicate_funcname); \
}
_ossv_predicate(ossv_is_alpha, is_alpha)
_ossv_predicate(ossv_is_alnum, is_alnum)
_ossv_predicate(ossv_is_digit, is_digit)
#undef _ossv_predicate
#endif