dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 114 / 0 / 114
Functions: 100.0% 37 / 0 / 37
Branches: 94.4% 68 / 8 / 80

src/util/string-view.h
Line Branch Exec Source
1 #ifndef UTIL_STRING_VIEW_H
2 #define UTIL_STRING_VIEW_H
3
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include "ascii.h"
9 #include "debug.h"
10 #include "macros.h"
11 #include "xmalloc.h"
12 #include "xmemrchr.h"
13 #include "xstring.h"
14
15 // A non-owning, length-bounded "view" into another string, similar to
16 // the C++17 string_view class or what many languages call a "slice".
17 // The .data member will usually *not* be null-terminated and the
18 // underlying string *must* outlive the view.
19 typedef struct {
20 const char NONSTRING *data;
21 size_t length;
22 } StringView;
23
24 // Expands to the 2 values expected for e.g. printf("%.*s", ...)
25 #define SV_FMT(sv) (int)(sv).length, (sv).data
26
27 #define STRING_VIEW(s) { \
28 .data = s, \
29 .length = STRLEN(s) \
30 }
31
32 #define SV(s) STRING_VIEW(s)
33
34 214615 static inline StringView string_view(const char *str, size_t length)
35 {
36 214615 return (StringView) {
37 .data = str,
38 .length = length
39 };
40 }
41
42 128550 static inline StringView strview(const char *str)
43 {
44
2/2
✓ Branch 2 → 3 taken 127995 times.
✓ Branch 2 → 4 taken 555 times.
128550 return string_view(str, str ? strlen(str) : 0);
45 }
46
47 // Return a view of the `str` substring between `start` and `end`.
48 // This is similar to xstrslice(), but without the need to allocate,
49 // copy or null-terminate.
50 36723 static inline StringView strview_from_slice(const char *str, size_t start, size_t end)
51 {
52 36723 BUG_ON(start > end);
53 36723 BUG_ON(end && !str);
54
2/2
✓ Branch 6 → 7 taken 25759 times.
✓ Branch 6 → 8 taken 10964 times.
36723 return string_view(start ? str + start : str, end - start);
55 }
56
57 2811 static inline StringView strview_suffix(StringView sv, size_t start)
58 {
59 2811 return strview_from_slice(sv.data, start, sv.length);
60 }
61
62 13419 static inline bool strview_equal(StringView a, StringView b)
63 {
64 13419 size_t n = a.length;
65
4/4
✓ Branch 2 → 3 taken 6416 times.
✓ Branch 2 → 6 taken 7003 times.
✓ Branch 4 → 5 taken 1886 times.
✓ Branch 4 → 6 taken 4530 times.
13419 return n == b.length && mem_equal(a.data, b.data, n);
66 }
67
68 391 static inline bool strview_equal_icase(StringView a, StringView b)
69 {
70 391 size_t n = a.length;
71
4/4
✓ Branch 2 → 3 taken 44 times.
✓ Branch 2 → 6 taken 347 times.
✓ Branch 4 → 5 taken 27 times.
✓ Branch 4 → 6 taken 17 times.
391 return n == b.length && mem_equal_icase(a.data, b.data, n);
72 }
73
74 7866 static inline bool strview_equal_cstring(StringView sv, const char *str)
75 {
76 7866 return strview_equal(sv, strview(str));
77 }
78
79 7 static inline bool strview_equal_either_cstring(StringView sv, const char *s1, const char *s2)
80 {
81
3/4
✓ Branch 3 → 4 taken 6 times.
✓ Branch 3 → 7 taken 1 time.
✓ Branch 5 → 6 taken 6 times.
✗ Branch 5 → 7 not taken.
7 return strview_equal_cstring(sv, s1) || strview_equal_cstring(sv, s2);
82 }
83
84 49984 static inline bool strview_has_sv_prefix(StringView sv, StringView prefix)
85 {
86 49984 const size_t plen = prefix.length;
87
4/4
✓ Branch 2 → 3 taken 46798 times.
✓ Branch 2 → 6 taken 3186 times.
✓ Branch 4 → 5 taken 44770 times.
✓ Branch 4 → 6 taken 2028 times.
49984 return sv.length >= plen && mem_equal(sv.data, prefix.data, plen);
88 }
89
90 1871 static inline bool strview_has_sv_suffix(StringView sv, StringView suffix)
91 {
92 1871 size_t len = sv.length;
93 1871 size_t suflen = suffix.length;
94
4/4
✓ Branch 2 → 3 taken 1495 times.
✓ Branch 2 → 6 taken 376 times.
✓ Branch 4 → 5 taken 781 times.
✓ Branch 4 → 6 taken 714 times.
1871 return len >= suflen && mem_equal(sv.data + len - suflen, suffix.data, suflen);
95 }
96
97 45439 static inline bool strview_has_prefix(StringView sv, const char *prefix)
98 {
99 45439 return strview_has_sv_prefix(sv, strview(prefix));
100 }
101
102 12 static inline bool strview_has_prefix_icase(StringView sv, const char *prefix)
103 {
104 12 size_t length = strlen(prefix);
105
4/4
✓ Branch 2 → 3 taken 11 times.
✓ Branch 2 → 6 taken 1 time.
✓ Branch 4 → 5 taken 7 times.
✓ Branch 4 → 6 taken 4 times.
12 return sv.length >= length && mem_equal_icase(sv.data, prefix, length);
106 }
107
108 47 static inline bool strview_has_either_prefix (
109 StringView sv,
110 const char *pfx1,
111 const char *pfx2
112 ) {
113 47 return strview_has_sv_prefix(sv, strview(pfx1))
114
4/4
✓ Branch 3 → 4 taken 24 times.
✓ Branch 3 → 7 taken 23 times.
✓ Branch 5 → 6 taken 22 times.
✓ Branch 5 → 7 taken 2 times.
47 || strview_has_sv_prefix(sv, strview(pfx2));
115 }
116
117 914 static inline bool strview_has_suffix(StringView sv, const char *suffix)
118 {
119 914 return strview_has_sv_suffix(sv, strview(suffix));
120 }
121
122 365 static inline bool strview_has_sv_prefix_and_suffix (
123 StringView sv,
124 StringView prefix,
125 StringView suffix
126 ) {
127 365 return
128 365 sv.length >= prefix.length + suffix.length
129
2/2
✓ Branch 4 → 5 taken 6 times.
✓ Branch 4 → 8 taken 234 times.
240 && strview_has_sv_prefix(sv, prefix)
130
3/4
✓ Branch 2 → 3 taken 240 times.
✓ Branch 2 → 8 taken 125 times.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 6 times.
371 && strview_has_sv_suffix(sv, suffix)
131 ;
132 }
133
134 2 static inline bool strview_has_prefix_and_suffix (
135 StringView sv,
136 const char *prefix,
137 const char *suffix
138 ) {
139 2 return strview_has_sv_prefix_and_suffix(sv, strview(prefix), strview(suffix));
140 }
141
142 10813 static inline size_t strview_blank_prefix_length(StringView sv)
143 {
144 10813 size_t i = 0;
145
4/4
✓ Branch 4 → 5 taken 23542 times.
✓ Branch 4 → 6 taken 1568 times.
✓ Branch 5 → 3 taken 14297 times.
✓ Branch 5 → 6 taken 9245 times.
25110 while (i < sv.length && ascii_isblank(sv.data[i])) {
146 14297 i++;
147 }
148 10813 return i;
149 }
150
151 247 static inline size_t strview_blank_suffix_length(StringView sv)
152 {
153 247 size_t n = sv.length;
154
4/4
✓ Branch 3 → 4 taken 313 times.
✓ Branch 3 → 5 taken 46 times.
✓ Branch 4 → 3 taken 112 times.
✓ Branch 4 → 5 taken 201 times.
359 while (n && ascii_isblank(sv.data[n - 1])) {
155 n--;
156 }
157 247 return sv.length - n;
158 }
159
160 9932 static inline size_t strview_char_suffix_length(StringView sv, char ch)
161 {
162 9932 size_t n = sv.length;
163
4/4
✓ Branch 3 → 4 taken 9100 times.
✓ Branch 3 → 6 taken 1524 times.
✓ Branch 4 → 5 taken 692 times.
✓ Branch 4 → 6 taken 8408 times.
10624 while (n && sv.data[n - 1] == ch) {
164 n--;
165 }
166 9932 return sv.length - n;
167 }
168
169 103 static inline bool strview_isblank(StringView sv)
170 {
171 103 return strview_blank_prefix_length(sv) == sv.length;
172 }
173
174 3 static inline bool strview_contains_char_type(StringView sv, AsciiCharType mask)
175 {
176
2/2
✓ Branch 5 → 3 taken 20 times.
✓ Branch 5 → 6 taken 3 times.
23 for (size_t i = 0, n = sv.length; i < n; i++) {
177
1/2
✓ Branch 3 → 4 taken 20 times.
✗ Branch 3 → 6 not taken.
20 if (ascii_test(sv.data[i], mask)) {
178 return true;
179 }
180 }
181 return false;
182 }
183
184 79 static inline const char *strview_memchr(StringView sv, int c)
185 {
186 79 return xmemchr(sv.data, c, sv.length);
187 }
188
189 716 static inline const char *strview_memrchr(StringView sv, int c)
190 {
191 716 return xmemrchr(sv.data, c, sv.length);
192 }
193
194 1 static inline ssize_t strview_memchr_idx(StringView sv, int c)
195 {
196 1 const char *ptr = strview_memchr(sv, c);
197
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1 time.
1 return ptr ? (ssize_t)(ptr - sv.data) : -1;
198 }
199
200 712 static inline ssize_t strview_memrchr_idx(StringView sv, int c)
201 {
202 712 const char *ptr = strview_memrchr(sv, c);
203
2/2
✓ Branch 2 → 3 taken 302 times.
✓ Branch 2 → 4 taken 410 times.
712 return ptr ? (ssize_t)(ptr - sv.data) : -1;
204 }
205
206 11938 static inline size_t strview_remove_prefix(StringView *sv, size_t len)
207 {
208 11938 BUG_ON(len > sv->length);
209
2/2
✓ Branch 4 → 5 taken 3958 times.
✓ Branch 4 → 6 taken 7980 times.
11938 sv->data = len ? sv->data + len : sv->data;
210 11938 sv->length -= len;
211 11938 return len;
212 }
213
214 1099 static inline size_t strview_remove_suffix(StringView *sv, size_t len)
215 {
216 1099 BUG_ON(len > sv->length);
217 1099 sv->length -= len;
218 1099 return len;
219 }
220
221 783 static inline bool strview_remove_matching_sv_prefix (
222 StringView *sv,
223 StringView prefix
224 ) {
225 783 bool match = strview_has_sv_prefix(*sv, prefix);
226
2/2
✓ Branch 3 → 4 taken 158 times.
✓ Branch 3 → 5 taken 625 times.
783 strview_remove_prefix(sv, match ? prefix.length : 0);
227 783 return match;
228 }
229
230 887 static inline bool strview_remove_matching_sv_suffix (
231 StringView *sv,
232 StringView suffix
233 ) {
234 887 bool match = strview_has_sv_suffix(*sv, suffix);
235
2/2
✓ Branch 3 → 4 taken 15 times.
✓ Branch 3 → 5 taken 872 times.
887 strview_remove_suffix(sv, match ? suffix.length : 0);
236 887 return match;
237 }
238
239 781 static inline bool strview_remove_matching_prefix(StringView *sv, const char *prefix)
240 {
241 781 return strview_remove_matching_sv_prefix(sv, strview(prefix));
242 }
243
244 887 static inline bool strview_remove_matching_suffix(StringView *sv, const char *suffix)
245 {
246 887 return strview_remove_matching_sv_suffix(sv, strview(suffix));
247 }
248
249 // Remove `prefix1` or `prefix2` from the start of `sv` (if present) and
250 // return 1 or 2 accordingly, or 0 if neither is present
251 250 static inline unsigned int strview_remove_either_matching_prefix (
252 StringView *sv,
253 const char *prefix1,
254 const char *prefix2
255 ) {
256 250 bool match1 = strview_remove_matching_prefix(sv, prefix1);
257
4/4
✓ Branch 3 → 4 taken 206 times.
✓ Branch 3 → 7 taken 44 times.
✓ Branch 5 → 6 taken 188 times.
✓ Branch 5 → 7 taken 18 times.
250 return match1 ? 1 : (strview_remove_matching_prefix(sv, prefix2) ? 2 : 0);
258 }
259
260 56 static inline bool strview_remove_matching_affixes (
261 StringView *sv,
262 StringView prefix,
263 StringView suffix
264 ) {
265 56 size_t total_affix_length = prefix.length + suffix.length;
266 56 bool pmatch = strview_has_sv_prefix(*sv, prefix);
267 56 bool smatch = strview_has_sv_suffix(*sv, suffix);
268
4/4
✓ Branch 4 → 5 taken 54 times.
✓ Branch 4 → 7 taken 2 times.
✓ Branch 5 → 6 taken 15 times.
✓ Branch 5 → 7 taken 39 times.
56 bool match = (total_affix_length <= sv->length) && pmatch && smatch;
269
270 15 if (match) {
271 15 sv->data += prefix.length;
272 15 sv->length -= total_affix_length;
273 }
274
275 56 return match;
276 }
277
278 NONNULL_ARGS
279 10680 static inline size_t strview_trim_left(StringView *sv)
280 {
281 10680 return strview_remove_prefix(sv, strview_blank_prefix_length(*sv));
282 }
283
284 NONNULL_ARGS
285 209 static inline size_t strview_trim_right(StringView *sv)
286 {
287 209 return strview_remove_suffix(sv, strview_blank_suffix_length(*sv));
288 }
289
290 NONNULL_ARGS
291 7 static inline size_t strview_trim(StringView *sv)
292 {
293 7 size_t r = strview_trim_right(sv);
294 7 return r + strview_trim_left(sv);
295 }
296
297 MALLOC RETURNS_NONNULL
298 145 static inline char *strview_clone_cstring(StringView sv)
299 {
300 145 return xmemjoin(sv.data, sv.length, "", 1);
301 }
302
303 #endif
304