Line | Branch | Exec | Source |
---|---|---|---|
1 | #ifndef UTIL_STR_UTIL_H | ||
2 | #define UTIL_STR_UTIL_H | ||
3 | |||
4 | #include <stdbool.h> | ||
5 | #include <stddef.h> | ||
6 | #include <stdlib.h> | ||
7 | #include <string.h> | ||
8 | #include "debug.h" | ||
9 | #include "macros.h" | ||
10 | #include "string-view.h" | ||
11 | #include "xstring.h" | ||
12 | |||
13 | #define copyliteral(dest, lit) copystrn(dest, lit, STRLEN(lit)) | ||
14 | |||
15 | 887 | static inline size_t copystrn(char *dest, const char *src, size_t len) | |
16 | { | ||
17 | 887 | memcpy(dest, src, len); | |
18 | 887 | return len; | |
19 | } | ||
20 | |||
21 | // Like getenv(3), but also returning NULL for empty strings | ||
22 | 18 | static inline const char *xgetenv(const char *name) | |
23 | { | ||
24 | 18 | const char *val = getenv(name); | |
25 |
3/4✓ Branch 0 (3→4) taken 10 times.
✓ Branch 1 (3→5) taken 8 times.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→6) taken 10 times.
|
18 | return (!val || val[0] == '\0') ? NULL : val; |
26 | } | ||
27 | |||
28 | PURE NONNULL_ARGS | ||
29 | 2053 | static inline bool str_has_strn_prefix(const char *str, const char *prefix, size_t plen) | |
30 | { | ||
31 | // Note that `prefix` must be a null-terminated string and `plen` | ||
32 | // should be equal to strlen(prefix). The explicit length parameter | ||
33 | // is simply for the sake of efficiency, e.g. to allow strlen() to | ||
34 | // be hoisted out of loops when `prefix` is loop-invariant. | ||
35 |
4/4✓ Branch 0 (2→3) taken 1506 times.
✓ Branch 1 (2→5) taken 547 times.
✓ Branch 2 (3→4) taken 1408 times.
✓ Branch 3 (3→5) taken 98 times.
|
2053 | return plen == 0 || strncmp(str, prefix, plen) == 0; |
36 | } | ||
37 | |||
38 | PURE NONNULL_ARGS | ||
39 | 205 | static inline bool str_has_prefix(const char *str, const char *prefix) | |
40 | { | ||
41 | 205 | return str_has_strn_prefix(str, prefix, strlen(prefix)); | |
42 | } | ||
43 | |||
44 | PURE NONNULL_ARGS | ||
45 | 19 | static inline bool str_has_suffix(const char *str, const char *suffix) | |
46 | { | ||
47 | 19 | size_t l1 = strlen(str); | |
48 | 19 | size_t l2 = strlen(suffix); | |
49 |
4/4✓ Branch 0 (2→3) taken 15 times.
✓ Branch 1 (2→6) taken 4 times.
✓ Branch 2 (4→5) taken 8 times.
✓ Branch 3 (4→6) taken 7 times.
|
19 | return (l1 >= l2) && mem_equal(str + l1 - l2, suffix, l2); |
50 | } | ||
51 | |||
52 | NONNULL_ARGS | ||
53 | 128 | static inline bool strn_has_strview_prefix(const char *s, size_t n, const StringView *p) | |
54 | { | ||
55 |
4/4✓ Branch 0 (2→3) taken 80 times.
✓ Branch 1 (2→6) taken 48 times.
✓ Branch 2 (4→5) taken 77 times.
✓ Branch 3 (4→6) taken 3 times.
|
128 | return n >= p->length && mem_equal(s, p->data, p->length); |
56 | } | ||
57 | |||
58 | NONNULL_ARGS | ||
59 | 142 | static inline size_t str_common_prefix_length(const char *a, const char *b) | |
60 | { | ||
61 | 142 | size_t n = 0; | |
62 |
4/4✓ Branch 0 (4→5) taken 3044 times.
✓ Branch 1 (4→6) taken 82 times.
✓ Branch 2 (5→3) taken 2984 times.
✓ Branch 3 (5→6) taken 60 times.
|
3126 | while (a[n] && a[n] == b[n]) { |
63 | 2984 | n++; | |
64 | } | ||
65 | 142 | return n; | |
66 | } | ||
67 | |||
68 | // Replaces all occurrences of a specific byte with a replacement byte | ||
69 | // and returns the length of the string (like strlen(str)) | ||
70 | NONNULL_ARGS | ||
71 | 1 | static inline size_t str_replace_byte(char *str, char byte, char replacement) | |
72 | { | ||
73 | 1 | size_t n = 0; | |
74 |
2/2✓ Branch 0 (6→3) taken 17 times.
✓ Branch 1 (6→7) taken 1 times.
|
18 | for (char c; (c = str[n]); n++) { |
75 |
2/2✓ Branch 0 (3→4) taken 10 times.
✓ Branch 1 (3→5) taken 7 times.
|
17 | if (c == byte) { |
76 | 10 | str[n] = replacement; | |
77 | } | ||
78 | } | ||
79 | 1 | return n; | |
80 | } | ||
81 | |||
82 | NONNULL_ARGS | ||
83 | 5 | static inline void strn_replace_byte(char *str, size_t n, char byte, char rep) | |
84 | { | ||
85 |
2/2✓ Branch 0 (6→3) taken 70 times.
✓ Branch 1 (6→7) taken 5 times.
|
75 | for (size_t i = 0; i < n; i++) { |
86 |
2/2✓ Branch 0 (3→4) taken 23 times.
✓ Branch 1 (3→5) taken 47 times.
|
70 | if (str[i] == byte) { |
87 | 23 | str[i] = rep; | |
88 | } | ||
89 | } | ||
90 | 5 | } | |
91 | |||
92 | // Extract a substring between `buf + pos` and either the next `delim` | ||
93 | // byte (if found) or `buf + size` (the remainder of the string). The | ||
94 | // substring is returned as a StringView and the `posp` in-out param | ||
95 | // is set to the offset one byte after the found delimiter (or to the | ||
96 | // end of the size-bounded string, if no delimiter was found). | ||
97 | NONNULL_ARGS READONLY(1, 3) READWRITE(2) | ||
98 | 9466 | static inline StringView get_delim(const char *buf, size_t *posp, size_t size, int delim) | |
99 | { | ||
100 | 9466 | size_t pos = *posp; | |
101 | 9466 | BUG_ON(pos >= size); | |
102 | 9466 | size_t len = size - pos; | |
103 | 9466 | const char *start = buf + pos; | |
104 | 9466 | const char *found = memchr(start, delim, len); | |
105 |
2/2✓ Branch 0 (4→5) taken 9235 times.
✓ Branch 1 (4→6) taken 231 times.
|
9466 | len = found ? (size_t)(found - start) : len; |
106 | 9466 | size_t delim_len = found ? 1 : 0; | |
107 | 9466 | *posp += len + delim_len; | |
108 | 9466 | return string_view(start, len); | |
109 | } | ||
110 | |||
111 | // Similar to get_delim(), but returning a null-terminated substring | ||
112 | // instead of a StringView, by mutating the contents of `buf` (i.e. | ||
113 | // replacing the delimiter with a NUL byte). Using get_delim() should | ||
114 | // be preferred, unless the substring specifically needs to be | ||
115 | // null-terminated (e.g. for passing to a library function). | ||
116 | NONNULL_ARGS | ||
117 | 574 | static inline char *get_delim_str(char *buf, size_t *posp, size_t size, int delim) | |
118 | { | ||
119 | 574 | size_t pos = *posp; | |
120 | 574 | BUG_ON(pos >= size); | |
121 | 574 | size_t len = size - pos; | |
122 | 574 | char *start = buf + pos; | |
123 | 574 | char *found = memchr(start, delim, len); | |
124 |
2/2✓ Branch 0 (4→5) taken 572 times.
✓ Branch 1 (4→6) taken 2 times.
|
574 | if (found) { |
125 | 572 | *found = '\0'; | |
126 | 572 | *posp += (size_t)(found - start) + 1; | |
127 | } else { | ||
128 | // If no delimiter is found, write the null-terminator 1 byte | ||
129 | // beyond the `size` bound. Callers must ensure this is safe | ||
130 | // to do. Thus, when calling this function (perhaps repeatedly) | ||
131 | // to consume an entire string, either buf[size-1] must be a | ||
132 | // delim byte or buf[size] must be in-bounds, writable memory. | ||
133 | 2 | start[len] = '\0'; | |
134 | 2 | *posp += len; | |
135 | } | ||
136 | 574 | return start; | |
137 | } | ||
138 | |||
139 | NONNULL_ARGS | ||
140 | 8652 | static inline StringView buf_slice_next_line(const char *buf, size_t *posp, size_t size) | |
141 | { | ||
142 | 8652 | return get_delim(buf, posp, size, '\n'); | |
143 | } | ||
144 | |||
145 | NONNULL_ARGS | ||
146 | 570 | static inline char *buf_next_line(char *buf, size_t *posp, size_t size) | |
147 | { | ||
148 | 570 | return get_delim_str(buf, posp, size, '\n'); | |
149 | } | ||
150 | |||
151 | NONNULL_ARGS | ||
152 | 915 | static inline size_t count_nl(const char *buf, size_t size) | |
153 | { | ||
154 | 915 | const char *end = buf + size; | |
155 | 915 | size_t nl = 0; | |
156 |
2/2✓ Branch 0 (5→3) taken 3166 times.
✓ Branch 1 (5→6) taken 711 times.
|
3877 | while (buf < end) { |
157 | 3166 | buf = memchr(buf, '\n', end - buf); | |
158 |
2/2✓ Branch 0 (3→4) taken 2962 times.
✓ Branch 1 (3→6) taken 204 times.
|
3166 | if (!buf) { |
159 | break; | ||
160 | } | ||
161 | 2962 | buf++; | |
162 | 2962 | nl++; | |
163 | } | ||
164 | 915 | return nl; | |
165 | } | ||
166 | |||
167 | #endif | ||
168 |