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 | #define STRING_VIEW(s) { \ | ||
| 25 | .data = s, \ | ||
| 26 | .length = STRLEN(s) \ | ||
| 27 | } | ||
| 28 | |||
| 29 | #define SV(s) STRING_VIEW(s) | ||
| 30 | |||
| 31 | 122817 | static inline StringView string_view(const char *str, size_t length) | |
| 32 | { | ||
| 33 | 122817 | return (StringView) { | |
| 34 | .data = str, | ||
| 35 | .length = length | ||
| 36 | }; | ||
| 37 | } | ||
| 38 | |||
| 39 | 60542 | static inline StringView strview(const char *str) | |
| 40 | { | ||
| 41 |
2/2✓ Branch 2 → 3 taken 59995 times.
✓ Branch 2 → 4 taken 547 times.
|
60542 | return string_view(str, str ? strlen(str) : 0); |
| 42 | } | ||
| 43 | |||
| 44 | // Return a view of the `str` substring between `start` and `end`. | ||
| 45 | // This is similar to xstrslice(), but without the need to allocate, | ||
| 46 | // copy or null-terminate. | ||
| 47 | 36699 | static inline StringView strview_from_slice(const char *str, size_t start, size_t end) | |
| 48 | { | ||
| 49 | 36699 | BUG_ON(start > end); | |
| 50 | 36699 | BUG_ON(end && !str); | |
| 51 |
2/2✓ Branch 6 → 7 taken 25739 times.
✓ Branch 6 → 8 taken 10960 times.
|
36699 | return string_view(start ? str + start : str, end - start); |
| 52 | } | ||
| 53 | |||
| 54 | 2867 | static inline bool strview_equal(StringView a, StringView b) | |
| 55 | { | ||
| 56 | 2867 | size_t n = a.length; | |
| 57 |
4/4✓ Branch 2 → 3 taken 1820 times.
✓ Branch 2 → 6 taken 1047 times.
✓ Branch 4 → 5 taken 889 times.
✓ Branch 4 → 6 taken 931 times.
|
2867 | return n == b.length && mem_equal(a.data, b.data, n); |
| 58 | } | ||
| 59 | |||
| 60 | 16 | static inline bool strview_equal_icase(StringView a, StringView b) | |
| 61 | { | ||
| 62 | 16 | size_t n = a.length; | |
| 63 |
3/4✓ Branch 2 → 3 taken 13 times.
✓ Branch 2 → 6 taken 3 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 13 times.
|
16 | return n == b.length && mem_equal_icase(a.data, b.data, n); |
| 64 | } | ||
| 65 | |||
| 66 | 2413 | static inline bool strview_equal_cstring(StringView sv, const char *str) | |
| 67 | { | ||
| 68 | 2413 | return strview_equal(sv, strview(str)); | |
| 69 | } | ||
| 70 | |||
| 71 | 49975 | static inline bool strview_has_sv_prefix(StringView sv, StringView prefix) | |
| 72 | { | ||
| 73 | 49975 | const size_t plen = prefix.length; | |
| 74 |
4/4✓ Branch 2 → 3 taken 46792 times.
✓ Branch 2 → 6 taken 3183 times.
✓ Branch 4 → 5 taken 44765 times.
✓ Branch 4 → 6 taken 2027 times.
|
49975 | return sv.length >= plen && mem_equal(sv.data, prefix.data, plen); |
| 75 | } | ||
| 76 | |||
| 77 | 1871 | static inline bool strview_has_sv_suffix(StringView sv, StringView suffix) | |
| 78 | { | ||
| 79 | 1871 | size_t len = sv.length; | |
| 80 | 1871 | size_t suflen = suffix.length; | |
| 81 |
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); |
| 82 | } | ||
| 83 | |||
| 84 | 45430 | static inline bool strview_has_prefix(StringView sv, const char *prefix) | |
| 85 | { | ||
| 86 | 45430 | return strview_has_sv_prefix(sv, strview(prefix)); | |
| 87 | } | ||
| 88 | |||
| 89 | 12 | static inline bool strview_has_prefix_icase(StringView sv, const char *prefix) | |
| 90 | { | ||
| 91 | 12 | size_t length = strlen(prefix); | |
| 92 |
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); |
| 93 | } | ||
| 94 | |||
| 95 | 47 | static inline bool strview_has_either_prefix ( | |
| 96 | StringView sv, | ||
| 97 | const char *pfx1, | ||
| 98 | const char *pfx2 | ||
| 99 | ) { | ||
| 100 | 47 | return strview_has_sv_prefix(sv, strview(pfx1)) | |
| 101 |
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)); |
| 102 | } | ||
| 103 | |||
| 104 | 914 | static inline bool strview_has_suffix(StringView sv, const char *suffix) | |
| 105 | { | ||
| 106 | 914 | return strview_has_sv_suffix(sv, strview(suffix)); | |
| 107 | } | ||
| 108 | |||
| 109 | 365 | static inline bool strview_has_sv_prefix_and_suffix ( | |
| 110 | StringView sv, | ||
| 111 | StringView prefix, | ||
| 112 | StringView suffix | ||
| 113 | ) { | ||
| 114 | 365 | return | |
| 115 | 365 | sv.length >= prefix.length + suffix.length | |
| 116 |
2/2✓ Branch 4 → 5 taken 6 times.
✓ Branch 4 → 8 taken 234 times.
|
240 | && strview_has_sv_prefix(sv, prefix) |
| 117 |
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) |
| 118 | ; | ||
| 119 | } | ||
| 120 | |||
| 121 | 2 | static inline bool strview_has_prefix_and_suffix ( | |
| 122 | StringView sv, | ||
| 123 | const char *prefix, | ||
| 124 | const char *suffix | ||
| 125 | ) { | ||
| 126 | 2 | return strview_has_sv_prefix_and_suffix(sv, strview(prefix), strview(suffix)); | |
| 127 | } | ||
| 128 | |||
| 129 | 10814 | static inline size_t strview_blank_prefix_length(StringView sv) | |
| 130 | { | ||
| 131 | 10814 | size_t i = 0; | |
| 132 |
4/4✓ Branch 4 → 5 taken 23547 times.
✓ Branch 4 → 6 taken 1568 times.
✓ Branch 5 → 3 taken 14301 times.
✓ Branch 5 → 6 taken 9246 times.
|
25115 | while (i < sv.length && ascii_isblank(sv.data[i])) { |
| 133 | 14301 | i++; | |
| 134 | } | ||
| 135 | 10814 | return i; | |
| 136 | } | ||
| 137 | |||
| 138 | 247 | static inline size_t strview_blank_suffix_length(StringView sv) | |
| 139 | { | ||
| 140 | 247 | size_t n = sv.length; | |
| 141 |
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])) { |
| 142 | n--; | ||
| 143 | } | ||
| 144 | 247 | return sv.length - n; | |
| 145 | } | ||
| 146 | |||
| 147 | 9932 | static inline size_t strview_char_suffix_length(StringView sv, char ch) | |
| 148 | { | ||
| 149 | 9932 | size_t n = sv.length; | |
| 150 |
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) { |
| 151 | n--; | ||
| 152 | } | ||
| 153 | 9932 | return sv.length - n; | |
| 154 | } | ||
| 155 | |||
| 156 | 103 | static inline bool strview_isblank(StringView sv) | |
| 157 | { | ||
| 158 | 103 | return strview_blank_prefix_length(sv) == sv.length; | |
| 159 | } | ||
| 160 | |||
| 161 | 3 | static inline bool strview_contains_char_type(StringView sv, AsciiCharType mask) | |
| 162 | { | ||
| 163 |
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++) { |
| 164 |
1/2✓ Branch 3 → 4 taken 20 times.
✗ Branch 3 → 6 not taken.
|
20 | if (ascii_test(sv.data[i], mask)) { |
| 165 | return true; | ||
| 166 | } | ||
| 167 | } | ||
| 168 | return false; | ||
| 169 | } | ||
| 170 | |||
| 171 | 79 | static inline const char *strview_memchr(StringView sv, int c) | |
| 172 | { | ||
| 173 | 79 | return xmemchr(sv.data, c, sv.length); | |
| 174 | } | ||
| 175 | |||
| 176 | 716 | static inline const char *strview_memrchr(StringView sv, int c) | |
| 177 | { | ||
| 178 | 716 | return xmemrchr(sv.data, c, sv.length); | |
| 179 | } | ||
| 180 | |||
| 181 | 1 | static inline ssize_t strview_memchr_idx(StringView sv, int c) | |
| 182 | { | ||
| 183 | 1 | const char *ptr = strview_memchr(sv, c); | |
| 184 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1 time.
|
1 | return ptr ? (ssize_t)(ptr - sv.data) : -1; |
| 185 | } | ||
| 186 | |||
| 187 | 712 | static inline ssize_t strview_memrchr_idx(StringView sv, int c) | |
| 188 | { | ||
| 189 | 712 | const char *ptr = strview_memrchr(sv, c); | |
| 190 |
2/2✓ Branch 2 → 3 taken 302 times.
✓ Branch 2 → 4 taken 410 times.
|
712 | return ptr ? (ssize_t)(ptr - sv.data) : -1; |
| 191 | } | ||
| 192 | |||
| 193 | 11939 | static inline size_t strview_remove_prefix(StringView *sv, size_t len) | |
| 194 | { | ||
| 195 | 11939 | BUG_ON(len > sv->length); | |
| 196 |
2/2✓ Branch 4 → 5 taken 3959 times.
✓ Branch 4 → 6 taken 7980 times.
|
11939 | sv->data = len ? sv->data + len : sv->data; |
| 197 | 11939 | sv->length -= len; | |
| 198 | 11939 | return len; | |
| 199 | } | ||
| 200 | |||
| 201 | 1099 | static inline size_t strview_remove_suffix(StringView *sv, size_t len) | |
| 202 | { | ||
| 203 | 1099 | BUG_ON(len > sv->length); | |
| 204 | 1099 | sv->length -= len; | |
| 205 | 1099 | return len; | |
| 206 | } | ||
| 207 | |||
| 208 | 783 | static inline bool strview_remove_matching_sv_prefix ( | |
| 209 | StringView *sv, | ||
| 210 | StringView prefix | ||
| 211 | ) { | ||
| 212 | 783 | bool match = strview_has_sv_prefix(*sv, prefix); | |
| 213 |
2/2✓ Branch 3 → 4 taken 158 times.
✓ Branch 3 → 5 taken 625 times.
|
783 | strview_remove_prefix(sv, match ? prefix.length : 0); |
| 214 | 783 | return match; | |
| 215 | } | ||
| 216 | |||
| 217 | 887 | static inline bool strview_remove_matching_sv_suffix ( | |
| 218 | StringView *sv, | ||
| 219 | StringView suffix | ||
| 220 | ) { | ||
| 221 | 887 | bool match = strview_has_sv_suffix(*sv, suffix); | |
| 222 |
2/2✓ Branch 3 → 4 taken 15 times.
✓ Branch 3 → 5 taken 872 times.
|
887 | strview_remove_suffix(sv, match ? suffix.length : 0); |
| 223 | 887 | return match; | |
| 224 | } | ||
| 225 | |||
| 226 | 781 | static inline bool strview_remove_matching_prefix(StringView *sv, const char *prefix) | |
| 227 | { | ||
| 228 | 781 | return strview_remove_matching_sv_prefix(sv, strview(prefix)); | |
| 229 | } | ||
| 230 | |||
| 231 | 887 | static inline bool strview_remove_matching_suffix(StringView *sv, const char *suffix) | |
| 232 | { | ||
| 233 | 887 | return strview_remove_matching_sv_suffix(sv, strview(suffix)); | |
| 234 | } | ||
| 235 | |||
| 236 | // Remove `prefix1` or `prefix2` from the start of `sv` (if present) and | ||
| 237 | // return 1 or 2 accordingly, or 0 if neither is present | ||
| 238 | 250 | static inline unsigned int strview_remove_either_matching_prefix ( | |
| 239 | StringView *sv, | ||
| 240 | const char *prefix1, | ||
| 241 | const char *prefix2 | ||
| 242 | ) { | ||
| 243 | 250 | bool match1 = strview_remove_matching_prefix(sv, prefix1); | |
| 244 |
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); |
| 245 | } | ||
| 246 | |||
| 247 | 56 | static inline bool strview_remove_matching_affixes ( | |
| 248 | StringView *sv, | ||
| 249 | StringView prefix, | ||
| 250 | StringView suffix | ||
| 251 | ) { | ||
| 252 | 56 | size_t total_affix_length = prefix.length + suffix.length; | |
| 253 | 56 | bool pmatch = strview_has_sv_prefix(*sv, prefix); | |
| 254 | 56 | bool smatch = strview_has_sv_suffix(*sv, suffix); | |
| 255 |
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; |
| 256 | |||
| 257 | 15 | if (match) { | |
| 258 | 15 | sv->data += prefix.length; | |
| 259 | 15 | sv->length -= total_affix_length; | |
| 260 | } | ||
| 261 | |||
| 262 | 56 | return match; | |
| 263 | } | ||
| 264 | |||
| 265 | NONNULL_ARGS | ||
| 266 | 10681 | static inline size_t strview_trim_left(StringView *sv) | |
| 267 | { | ||
| 268 | 10681 | return strview_remove_prefix(sv, strview_blank_prefix_length(*sv)); | |
| 269 | } | ||
| 270 | |||
| 271 | NONNULL_ARGS | ||
| 272 | 209 | static inline size_t strview_trim_right(StringView *sv) | |
| 273 | { | ||
| 274 | 209 | return strview_remove_suffix(sv, strview_blank_suffix_length(*sv)); | |
| 275 | } | ||
| 276 | |||
| 277 | NONNULL_ARGS | ||
| 278 | 7 | static inline size_t strview_trim(StringView *sv) | |
| 279 | { | ||
| 280 | 7 | size_t r = strview_trim_right(sv); | |
| 281 | 7 | return r + strview_trim_left(sv); | |
| 282 | } | ||
| 283 | |||
| 284 | MALLOC RETURNS_NONNULL | ||
| 285 | 144 | static inline char *strview_clone_cstring(StringView sv) | |
| 286 | { | ||
| 287 | 144 | return xmemjoin(sv.data, sv.length, "", 1); | |
| 288 | } | ||
| 289 | |||
| 290 | #endif | ||
| 291 |