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>
21 * bytestring - construct a new bytestring
22 * @p: pointer to the content of the bytestring
23 * @l: length of the bytestring
25 * Builds a new bytestring starting at p, of length l.
28 * char x[5] = "abcde";
29 * struct bytestring bs = bytestring(x, 5);
30 * assert(bs.len == 5);
32 static inline struct bytestring bytestring(const char *p, size_t l)
34 struct bytestring bs = {
42 #define bytestring_NULL bytestring(NULL, 0)
45 * BYTESTRING - construct a bytestring from a string literal
48 * Builds a new bytestring containing the given literal string, not
49 * including the terminating \0 (but including any internal \0s).
52 * assert(BYTESTRING("abc\0def").len == 7);
54 #define BYTESTRING(s) (bytestring((s), ARRAY_SIZE(s) - 1))
58 * bytestring_from_string - construct a bytestring from a NUL terminated string
59 * @s: NUL-terminated string pointer
61 * Builds a new bytestring containing the given NUL-terminated string,
62 * up to, but not including, the terminating \0.
65 * assert(bytestring_from_string("abc\0def").len == 3);
67 static inline struct bytestring bytestring_from_string(const char *s)
70 return bytestring_NULL;
71 return bytestring(s, strlen(s));
75 * bytestring_eq - test if bytestrings have identical content
78 * Returns 1 if the given bytestrings have identical length and
79 * content, 0 otherwise.
81 static inline bool bytestring_eq(struct bytestring a, struct bytestring b)
83 return (a.len == b.len)
84 && (memcmp(a.ptr, b.ptr, a.len) == 0);
88 * bytestring_byte - get a byte from a bytestring
92 * Return the @n-th byte from @s. Aborts (via assert) if @n is out of
93 * range for the length of @s.
95 static inline char bytestring_byte(struct bytestring s, size_t n)
102 * bytestring_slice - extract a substring from a bytestring
104 * @start, @end: indexes
106 * Return a sub-bytestring of @s, starting at byte index @start, and
107 * running to, but not including byte @end. If @end is before start,
108 * returns a zero-length bytestring. If @start is out of range,
109 * return a zero length bytestring at the end of @s.
111 * Note that this doesn't copy or allocate anything - the returned
112 * bytestring occupies (some of) the same memory as the given
115 static inline struct bytestring bytestring_slice(struct bytestring s,
116 size_t start, size_t end)
125 return bytestring(s.ptr + start, end - start);
129 * bytestring_starts - test if the start of one bytestring matches another
130 * @s, @prefix: bytestrings
132 * Returns true if @prefix appears as a substring at the beginning of
133 * @s, false otherwise.
135 static inline bool bytestring_starts(struct bytestring s,
136 struct bytestring prefix)
138 return (s.len >= prefix.len) && (memcmp(s.ptr,
139 prefix.ptr, prefix.len) == 0);
143 * bytestring_ends - test if the end of one bytestring matches another
144 * @s, @suffix: bytestrings
146 * Returns true if @suffix appears as a substring at the end of @s,
149 static inline bool bytestring_ends(struct bytestring s,
150 struct bytestring suffix)
152 return (s.len >= suffix.len) && (memcmp(s.ptr + s.len - suffix.len,
153 suffix.ptr, suffix.len) == 0);
157 * bytestring_index - locate character in bytestring
158 * @haystack: a bytestring
159 * @needle: a character or byte value
161 * Returns a pointer to the first occurrence of @needle within
162 * @haystack, or NULL if @needle does not appear in @haystack.
164 static inline const char *bytestring_index(struct bytestring haystack,
167 return memchr(haystack.ptr, needle, haystack.len);
171 * bytestring_rindex - locate character in bytestring
172 * @haystack: a bytestring
173 * @needle: a character or byte value
175 * Returns a pointer to the last occurrence of @needle within
176 * @haystack, or NULL if @needle does not appear in @haystack.
178 static inline const char *bytestring_rindex(struct bytestring haystack,
181 return memrchr(haystack.ptr, needle, haystack.len);
185 * bytestring_bytestring - search for a bytestring in another bytestring
186 * @haystack, @needle: bytestrings
188 * Returns a bytestring corresponding to the first occurrence of
189 * @needle in @haystack, or bytestring_NULL if @needle is not found
192 static inline struct bytestring bytestring_bytestring(struct bytestring haystack,
193 struct bytestring needle)
195 const char *p = memmem(haystack.ptr, haystack.len,
196 needle.ptr, needle.len);
198 return bytestring(p, needle.len);
200 return bytestring_NULL;
204 * bytestring_spn - search a bytestring for a set of bytes
206 * @accept: a bytestring containing a set of bytes to accept
208 * Returns the length, in bytes, of the initial segment of @s which
209 * consists entirely of characters in @accept.
211 size_t bytestring_spn(struct bytestring s, struct bytestring accept);
214 * bytestring_cspn - search a bytestring for a set of bytes (complemented)
216 * @reject: a bytestring containing a set of bytes to reject
218 * Returns the length, in bytes, of the initial segment of @s which
219 * consists entirely of characters not in @reject.
221 size_t bytestring_cspn(struct bytestring s, struct bytestring reject);
224 * bytestring_splitchr_first - split a bytestring on a single character delimiter
225 * @whole: a bytestring
226 * @delim: delimiter character
228 * Returns the first @delim delimited substring of @whole.
230 struct bytestring bytestring_splitchr_first(struct bytestring whole,
234 * bytestring_splitchr_next - split a bytestring on a single character delimiter
235 * @whole: a bytestring
236 * @delim: delimiter character
237 * @prev: last substring
239 * Returns the next @delim delimited substring of @whole after @prev.
241 struct bytestring bytestring_splitchr_next(struct bytestring whole,
242 char delim, struct bytestring prev);
244 #define bytestring_foreach_splitchr(_s, _w, _delim) \
245 for ((_s) = bytestring_splitchr_first((_w), (_delim)); \
247 (_s) = bytestring_splitchr_next((_w), (_delim), (_s)))
249 #endif /* CCAN_BYTESTRING_H_ */