1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #ifndef CCAN_BYTESTRING_H_
3 #define CCAN_BYTESTRING_H_
12 #include <ccan/array_size/array_size.h>
13 #include <ccan/mem/mem.h>
14 #include <ccan/compiler/compiler.h>
22 * bytestring - construct a new bytestring
23 * @p: pointer to the content of the bytestring
24 * @l: length of the bytestring
26 * Builds a new bytestring starting at p, of length l.
29 * char x[5] = "abcde";
30 * struct bytestring bs = bytestring(x, 5);
31 * assert(bs.len == 5);
33 static inline CONST_FUNCTION struct bytestring
34 bytestring(const char *p, size_t l)
36 struct bytestring bs = {
44 #define bytestring_NULL bytestring(NULL, 0)
47 * BYTESTRING - construct a bytestring from a string literal
50 * Builds a new bytestring containing the given literal string, not
51 * including the terminating \0 (but including any internal \0s).
54 * assert(BYTESTRING("abc\0def").len == 7);
56 #define BYTESTRING(s) (bytestring((s), ARRAY_SIZE(s) - 1))
59 * BYTESTRING_INIT - bytestring initializer
62 * Produces an initializer for a bytestring from a literal string.
63 * The resulting bytestring will not include the terminating \0, but
64 * will include any internal \0s.
67 * static const struct bytestring CONSTANT = BYTESTRING_INIT("CONSTANT");
69 #define BYTESTRING_INIT(s) { .ptr = (s), .len = ARRAY_SIZE(s) - 1}
72 * bytestring_from_string - construct a bytestring from a NUL terminated string
73 * @s: NUL-terminated string pointer
75 * Builds a new bytestring containing the given NUL-terminated string,
76 * up to, but not including, the terminating \0.
79 * assert(bytestring_from_string("abc\0def").len == 3);
81 static inline struct bytestring bytestring_from_string(const char *s)
84 return bytestring_NULL;
85 return bytestring(s, strlen(s));
89 * bytestring_eq - test if bytestrings have identical content
92 * Returns 1 if the given bytestrings have identical length and
93 * content, 0 otherwise.
95 static inline bool bytestring_eq(struct bytestring a, struct bytestring b)
97 return memeq(a.ptr, a.len, b.ptr, b.len);
101 * bytestring_byte - get a byte from a bytestring
105 * Return the @n-th byte from @s. Aborts (via assert) if @n is out of
106 * range for the length of @s.
108 static inline char bytestring_byte(struct bytestring s, size_t n)
115 * bytestring_slice - extract a substring from a bytestring
117 * @start, @end: indexes
119 * Return a sub-bytestring of @s, starting at byte index @start, and
120 * running to, but not including byte @end. If @end is before start,
121 * returns a zero-length bytestring. If @start is out of range,
122 * return a zero length bytestring at the end of @s.
124 * Note that this doesn't copy or allocate anything - the returned
125 * bytestring occupies (some of) the same memory as the given
128 static inline struct bytestring bytestring_slice(struct bytestring s,
129 size_t start, size_t end)
138 return bytestring(s.ptr + start, end - start);
142 * bytestring_starts - test if the start of one bytestring matches another
143 * @s, @prefix: bytestrings
145 * Returns true if @prefix appears as a substring at the beginning of
146 * @s, false otherwise.
148 static inline bool bytestring_starts(struct bytestring s,
149 struct bytestring prefix)
151 return memstarts(s.ptr, s.len, prefix.ptr, prefix.len);
155 * bytestring_ends - test if the end of one bytestring matches another
156 * @s, @suffix: bytestrings
158 * Returns true if @suffix appears as a substring at the end of @s,
161 static inline bool bytestring_ends(struct bytestring s,
162 struct bytestring suffix)
164 return memends(s.ptr, s.len, suffix.ptr, suffix.len);
168 * bytestring_index - locate character in bytestring
169 * @haystack: a bytestring
170 * @needle: a character or byte value
172 * Returns a pointer to the first occurrence of @needle within
173 * @haystack, or NULL if @needle does not appear in @haystack.
175 static inline const char *bytestring_index(struct bytestring haystack,
178 return memchr(haystack.ptr, needle, haystack.len);
182 * bytestring_rindex - locate character in bytestring
183 * @haystack: a bytestring
184 * @needle: a character or byte value
186 * Returns a pointer to the last occurrence of @needle within
187 * @haystack, or NULL if @needle does not appear in @haystack.
189 static inline const char *bytestring_rindex(struct bytestring haystack,
192 return memrchr(haystack.ptr, needle, haystack.len);
196 * bytestring_bytestring - search for a bytestring in another bytestring
197 * @haystack, @needle: bytestrings
199 * Returns a bytestring corresponding to the first occurrence of
200 * @needle in @haystack, or bytestring_NULL if @needle is not found
203 static inline struct bytestring bytestring_bytestring(struct bytestring haystack,
204 struct bytestring needle)
208 /* Allow needle.ptr == NULL, without memmem sanitizer complaining */
210 return bytestring(haystack.ptr, 0);
212 p = memmem(haystack.ptr, haystack.len, needle.ptr, needle.len);
214 return bytestring(p, needle.len);
216 return bytestring_NULL;
220 * bytestring_spn - search a bytestring for a set of bytes
222 * @accept: a bytestring containing a set of bytes to accept
224 * Returns the length, in bytes, of the initial segment of @s which
225 * consists entirely of characters in @accept.
227 size_t bytestring_spn(struct bytestring s, struct bytestring accept);
230 * bytestring_cspn - search a bytestring for a set of bytes (complemented)
232 * @reject: a bytestring containing a set of bytes to reject
234 * Returns the length, in bytes, of the initial segment of @s which
235 * consists entirely of characters not in @reject.
237 size_t bytestring_cspn(struct bytestring s, struct bytestring reject);
240 * bytestring_splitchr_first - split a bytestring on a single character delimiter
241 * @whole: a bytestring
242 * @delim: delimiter character
244 * Returns the first @delim delimited substring of @whole.
246 struct bytestring bytestring_splitchr_first(struct bytestring whole,
250 * bytestring_splitchr_next - split a bytestring on a single character delimiter
251 * @whole: a bytestring
252 * @delim: delimiter character
253 * @prev: last substring
255 * Returns the next @delim delimited substring of @whole after @prev.
257 struct bytestring bytestring_splitchr_next(struct bytestring whole,
258 char delim, struct bytestring prev);
260 #define bytestring_foreach_splitchr(_s, _w, _delim) \
261 for ((_s) = bytestring_splitchr_first((_w), (_delim)); \
263 (_s) = bytestring_splitchr_next((_w), (_delim), (_s)))
266 * bytestring_splitchrs_first - split a bytestring on a set of delimiter
268 * @whole: a bytestring
269 * @delim: delimiter characters
271 * Returns the first substring of @whole delimited by any character in
274 struct bytestring bytestring_splitchrs_first(struct bytestring whole,
275 struct bytestring delim);
278 * bytestring_splitchr_next - split a bytestring on a set of delimiter
280 * @whole: a bytestring
281 * @delim: delimiter character
282 * @prev: last substring
284 * Returns the next @delim delimited substring of @whole after @prev.
286 struct bytestring bytestring_splitchrs_next(struct bytestring whole,
287 struct bytestring delim,
288 struct bytestring prev);
290 #define bytestring_foreach_splitchrs(_s, _w, _delim) \
291 for ((_s) = bytestring_splitchrs_first((_w), (_delim)); \
293 (_s) = bytestring_splitchrs_next((_w), (_delim), (_s)))
296 * bytestring_splitstr_first - split a bytestring on a delimiter string
297 * @whole: a bytestring
298 * @delim: delimiter substring
300 * Returns the first substring of @whole delimited by the substring in
303 struct bytestring bytestring_splitstr_first(struct bytestring whole,
304 struct bytestring delim);
307 * bytestring_splitstr_next - split a bytestring on a delimiter string
308 * @whole: a bytestring
309 * @delim: delimiter string
310 * @prev: last substring
312 * Returns the next @delim delimited substring of @whole after @prev.
314 struct bytestring bytestring_splitstr_next(struct bytestring whole,
315 struct bytestring delim,
316 struct bytestring prev);
318 #define bytestring_foreach_splitstr(_s, _w, _delim) \
319 for ((_s) = bytestring_splitstr_first((_w), (_delim)); \
321 (_s) = bytestring_splitstr_next((_w), (_delim), (_s)))
323 #endif /* CCAN_BYTESTRING_H_ */