]> git.ozlabs.org Git - ccan/commitdiff
bytestring: Split bytestrings by a character delimiter
authorDavid Gibson <david@gibson.dropbear.id.au>
Mon, 28 Jul 2014 09:31:11 +0000 (19:31 +1000)
committerDavid Gibson <david@gibson.dropbear.id.au>
Thu, 30 Oct 2014 14:04:59 +0000 (01:04 +1100)
This introduces the functions bytestring_splitchr_first() and
bytestring_splitchr_next() which can be used to iterate through substrings
of a bytestring separated by a single, specified delimiter character.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/bytestring/bytestring.c
ccan/bytestring/bytestring.h
ccan/bytestring/test/run.c

index 69e2b32bcebcc2067cddfd816ab54257646191b1..ddc5dcd06e9e1427b6776fc8a7b92c5164f51f5a 100644 (file)
@@ -24,3 +24,45 @@ size_t bytestring_cspn(struct bytestring s, struct bytestring reject)
 
        return s.len;
 }
+
+static struct bytestring _splitchr(struct bytestring whole, char delim,
+                                  size_t start)
+{
+       const char *p;
+
+       assert(start <= whole.len);
+
+       /* Check this first, in case memchr() is not safe with zero length */
+       if (start == whole.len)
+               return bytestring(whole.ptr + start, 0);
+
+       p = memchr(whole.ptr + start, delim, whole.len - start);
+       if (p)
+               return bytestring_slice(whole, start, p - whole.ptr);
+       else
+               return bytestring_slice(whole, start, whole.len);
+}
+
+struct bytestring bytestring_splitchr_first(struct bytestring whole,
+                                           char delim)
+{
+       if (whole.len == 0)
+               return bytestring_NULL;
+
+       return _splitchr(whole, delim, 0);
+}
+
+struct bytestring bytestring_splitchr_next(struct bytestring whole,
+                                          char delim, struct bytestring prev)
+{
+       if (!prev.ptr)
+               return bytestring_NULL;
+
+       /* prev has to be a substring of whole */
+       assert(prev.ptr >= whole.ptr);
+
+       if ((prev.ptr + prev.len) == (whole.ptr + whole.len))
+               return bytestring_NULL;
+
+       return _splitchr(whole, delim, (prev.ptr - whole.ptr) + prev.len + 1);
+}
index 34d81af2038e4bc5ea0c481e19f27ffb6620ae2a..8aa272755caa90b2ad1c18655a8195093d7ec7cf 100644 (file)
@@ -220,4 +220,30 @@ size_t bytestring_spn(struct bytestring s, struct bytestring accept);
  */
 size_t bytestring_cspn(struct bytestring s, struct bytestring reject);
 
+/**
+ * bytestring_splitchr_first - split a bytestring on a single character delimiter
+ * @whole: a bytestring
+ * @delim: delimiter character
+ *
+ * Returns the first @delim delimited substring of @whole.
+ */
+struct bytestring bytestring_splitchr_first(struct bytestring whole,
+                                           char delim);
+
+/**
+ * bytestring_splitchr_next - split a bytestring on a single character delimiter
+ * @whole: a bytestring
+ * @delim: delimiter character
+ * @prev: last substring
+ *
+ * Returns the next @delim delimited substring of @whole after @prev.
+ */
+struct bytestring bytestring_splitchr_next(struct bytestring whole,
+                                          char delim, struct bytestring prev);
+
+#define bytestring_foreach_splitchr(_s, _w, _delim) \
+       for ((_s) = bytestring_splitchr_first((_w), (_delim)); \
+            (_s).ptr;                                         \
+            (_s) = bytestring_splitchr_next((_w), (_delim), (_s)))
+
 #endif /* CCAN_BYTESTRING_H_ */
index 23415df58c3f4c5fa11d631f8951c6eb47a02c8f..3b563de3466812229ecf2b3a7bede2083a43de5a 100644 (file)
@@ -13,10 +13,11 @@ const char *str2 = TEST_STRING;
 
 int main(void)
 {
-       struct bytestring bs, bs1, bs2, bs3, bs4, bs5, bs6;
+       struct bytestring bs, bs1, bs2, bs3, bs4, bs5, bs6, bs7;
+       int n;
 
        /* This is how many tests you plan to run */
-       plan_tests(53);
+       plan_tests(89);
 
        bs = bytestring(str1, sizeof(str1) - 1);
        ok1(bs.ptr == str1);
@@ -100,6 +101,70 @@ int main(void)
        ok1(bytestring_spn(bs1, BYTESTRING("eginrst ")) == bs1.len);
        ok1(bytestring_cspn(bs2, BYTESTRING("z")) == bs2.len);
 
+       bs7 = bytestring_splitchr_first(bs, ' ');
+       ok1(bs7.ptr == bs.ptr);
+       ok1(bytestring_eq(bs7, BYTESTRING("test")));
+       bs7 = bytestring_splitchr_next(bs, ' ', bs7);
+       ok1(bs7.ptr == bs.ptr + 5);
+       ok1(bytestring_eq(bs7, BYTESTRING("string")));
+       bs7 = bytestring_splitchr_next(bs, ' ', bs7);
+       ok1(!bs7.ptr);
+       bs7 = bytestring_splitchr_next(bs, ' ', bs7);
+       ok1(!bs7.ptr);
+
+       bs7 = bytestring_splitchr_first(bs2, '\0');
+       ok1(bs7.ptr = bs2.ptr);
+       ok1(bytestring_eq(bs7, BYTESTRING("abc")));
+       bs7 = bytestring_splitchr_next(bs2, '\0', bs7);
+       ok1(bs7.ptr == bs2.ptr + 4);
+       ok1(bytestring_eq(bs7, BYTESTRING("def")));
+       bs7 = bytestring_splitchr_next(bs2, '\0', bs7);
+       ok1(!bs7.ptr);
+       bs7 = bytestring_splitchr_next(bs2, ' ', bs7);
+       ok1(!bs7.ptr);
+
+       bs7 = bytestring_splitchr_first(bs, 's');
+       ok1(bs7.ptr == bs.ptr);
+       ok1(bytestring_eq(bs7, BYTESTRING("te")));
+       bs7 = bytestring_splitchr_next(bs, 's', bs7);
+       ok1(bs7.ptr == bs.ptr + 3);
+       ok1(bytestring_eq(bs7, BYTESTRING("t ")));
+       bs7 = bytestring_splitchr_next(bs, 's', bs7);
+       ok1(bs7.ptr == bs.ptr + 6);
+       ok1(bytestring_eq(bs7, BYTESTRING("tring")));
+       bs7 = bytestring_splitchr_next(bs, 's', bs7);
+       ok1(!bs7.ptr);
+       bs7 = bytestring_splitchr_next(bs, ' ', bs7);
+       ok1(!bs7.ptr);
+
+       bs7 = bytestring_splitchr_first(bs2, 'f');
+       ok1(bs7.ptr = bs2.ptr);
+       ok1(bytestring_eq(bs7, BYTESTRING("abc\0de")));
+       bs7 = bytestring_splitchr_next(bs2, 'f', bs7);
+       ok1(bs7.ptr == bs2.ptr + 7);
+       ok1(bs7.len == 0);
+       bs7 = bytestring_splitchr_next(bs2, 'f', bs7);
+       ok1(!bs7.ptr);
+       bs7 = bytestring_splitchr_next(bs2, 'f', bs7);
+       ok1(!bs7.ptr);
+
+       bs7 = bytestring_splitchr_first(BYTESTRING(""), 'q');
+       ok1(!bs7.ptr);
+
+       n = 0;
+       bytestring_foreach_splitchr(bs7, BYTESTRING("  "), ' ') {
+               n++;
+               ok1(bs7.ptr && !bs7.len);
+       }
+       ok1(n == 3);
+
+       n = 0;
+       bytestring_foreach_splitchr(bs7, BYTESTRING("\0\0\0"), '\0') {
+               n++;
+               ok1(bs7.ptr && !bs7.len);
+       }
+       ok1(n == 4);
+
        /* This exits depending on whether all tests passed */
        return exit_status();
 }