1 /* Licensed under LGPLv2+ - see LICENSE file for details */
4 #include <ccan/bytestring/bytestring.h>
6 size_t bytestring_spn(struct bytestring s, struct bytestring accept)
10 for (i = 0; i < s.len; i++)
11 if (bytestring_index(accept, s.ptr[i]) == NULL)
17 size_t bytestring_cspn(struct bytestring s, struct bytestring reject)
21 for (i = 0; i < s.len; i++)
22 if (bytestring_index(reject, s.ptr[i]) != NULL)
28 static struct bytestring _splitchr(struct bytestring whole, char delim,
33 assert(start <= whole.len);
35 /* Check this first, in case memchr() is not safe with zero length */
36 if (start == whole.len)
37 return bytestring(whole.ptr + start, 0);
39 p = memchr(whole.ptr + start, delim, whole.len - start);
41 return bytestring_slice(whole, start, p - whole.ptr);
43 return bytestring_slice(whole, start, whole.len);
46 struct bytestring bytestring_splitchr_first(struct bytestring whole,
50 return bytestring_NULL;
52 return _splitchr(whole, delim, 0);
55 struct bytestring bytestring_splitchr_next(struct bytestring whole,
56 char delim, struct bytestring prev)
59 return bytestring_NULL;
61 /* prev has to be a substring of whole */
62 assert(prev.ptr >= whole.ptr);
64 if ((prev.ptr + prev.len) == (whole.ptr + whole.len))
65 return bytestring_NULL;
67 return _splitchr(whole, delim, (prev.ptr - whole.ptr) + prev.len + 1);
70 static struct bytestring _splitchrs(struct bytestring whole,
71 struct bytestring delim, size_t start)
73 struct bytestring remainder;
76 assert(start <= whole.len);
78 remainder = bytestring_slice(whole, start, whole.len);
79 n = bytestring_cspn(remainder, delim);
80 return bytestring_slice(whole, start, start + n);
83 struct bytestring bytestring_splitchrs_first(struct bytestring whole,
84 struct bytestring delim)
87 return bytestring_NULL;
89 return _splitchrs(whole, delim, 0);
92 struct bytestring bytestring_splitchrs_next(struct bytestring whole,
93 struct bytestring delim,
94 struct bytestring prev)
97 return bytestring_NULL;
99 /* prev has to be a substring of whole */
100 assert(prev.ptr >= whole.ptr);
102 if ((prev.ptr + prev.len) == (whole.ptr + whole.len))
103 return bytestring_NULL;
105 return _splitchrs(whole, delim, (prev.ptr - whole.ptr) + prev.len + 1);
108 static struct bytestring _splitstr(struct bytestring whole,
109 struct bytestring delim, size_t start)
111 struct bytestring remainder, nextdelim;
113 assert(start <= whole.len);
115 remainder = bytestring_slice(whole, start, whole.len);
116 nextdelim = bytestring_bytestring(remainder, delim);
118 return bytestring_slice(whole, start,
119 nextdelim.ptr - whole.ptr);
124 struct bytestring bytestring_splitstr_first(struct bytestring whole,
125 struct bytestring delim)
128 return bytestring_NULL;
130 return _splitstr(whole, delim, 0);
133 struct bytestring bytestring_splitstr_next(struct bytestring whole,
134 struct bytestring delim,
135 struct bytestring prev)
138 return bytestring_NULL;
140 /* prev has to be a substring of whole */
141 assert(prev.ptr >= whole.ptr);
143 if ((prev.ptr + prev.len) == (whole.ptr + whole.len))
144 return bytestring_NULL;
146 return _splitstr(whole, delim,
147 (prev.ptr - whole.ptr) + prev.len + delim.len);