]> git.ozlabs.org Git - ccan/commitdiff
bytestring: Split bytestrings by a set of character delimiters
authorDavid Gibson <david@gibson.dropbear.id.au>
Mon, 28 Jul 2014 09:31:36 +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_splitchrs_first() and
bytestring_splitchrs_next() which can be used to iterate through substrings
of a bytestring separated by any of a given set of delimiter characters.

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

index ddc5dcd06e9e1427b6776fc8a7b92c5164f51f5a..81cca4edbe66354779cb977058d659cde5d7ad08 100644 (file)
@@ -66,3 +66,41 @@ struct bytestring bytestring_splitchr_next(struct bytestring whole,
 
        return _splitchr(whole, delim, (prev.ptr - whole.ptr) + prev.len + 1);
 }
+
+static struct bytestring _splitchrs(struct bytestring whole,
+                                   struct bytestring delim, size_t start)
+{
+       struct bytestring remainder;
+       size_t n;
+
+       assert(start <= whole.len);
+
+       remainder = bytestring_slice(whole, start, whole.len);
+       n = bytestring_cspn(remainder, delim);
+       return bytestring_slice(whole, start, start + n);
+}
+
+struct bytestring bytestring_splitchrs_first(struct bytestring whole,
+                                            struct bytestring delim)
+{
+       if (whole.len == 0)
+               return bytestring_NULL;
+
+       return _splitchrs(whole, delim, 0);
+}
+
+struct bytestring bytestring_splitchrs_next(struct bytestring whole,
+                                           struct bytestring 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 _splitchrs(whole, delim, (prev.ptr - whole.ptr) + prev.len + 1);
+}
index 8aa272755caa90b2ad1c18655a8195093d7ec7cf..5a8d3cd896fb003704bada0f535d4e11c5354171 100644 (file)
@@ -246,4 +246,34 @@ struct bytestring bytestring_splitchr_next(struct bytestring whole,
             (_s).ptr;                                         \
             (_s) = bytestring_splitchr_next((_w), (_delim), (_s)))
 
+/**
+ * bytestring_splitchrs_first - split a bytestring on a set of delimiter
+ *                              characters
+ * @whole: a bytestring
+ * @delim: delimiter characters
+ *
+ * Returns the first substring of @whole delimited by any character in
+ * @delim.
+ */
+struct bytestring bytestring_splitchrs_first(struct bytestring whole,
+                                            struct bytestring delim);
+
+/**
+ * bytestring_splitchr_next - split a bytestring on a set of delimiter
+ *                            characters
+ * @whole: a bytestring
+ * @delim: delimiter character
+ * @prev: last substring
+ *
+ * Returns the next @delim delimited substring of @whole after @prev.
+ */
+struct bytestring bytestring_splitchrs_next(struct bytestring whole,
+                                           struct bytestring delim,
+                                           struct bytestring prev);
+
+#define bytestring_foreach_splitchrs(_s, _w, _delim) \
+       for ((_s) = bytestring_splitchrs_first((_w), (_delim)); \
+            (_s).ptr;                                         \
+            (_s) = bytestring_splitchrs_next((_w), (_delim), (_s)))
+
 #endif /* CCAN_BYTESTRING_H_ */
index 3b563de3466812229ecf2b3a7bede2083a43de5a..f1f68d6620b96f2b7be01ba03ebd10e72a7c16ac 100644 (file)
@@ -17,7 +17,7 @@ int main(void)
        int n;
 
        /* This is how many tests you plan to run */
-       plan_tests(89);
+       plan_tests(109);
 
        bs = bytestring(str1, sizeof(str1) - 1);
        ok1(bs.ptr == str1);
@@ -165,6 +165,39 @@ int main(void)
        }
        ok1(n == 4);
 
+       bs7 = bytestring_splitchrs_first(bs, BYTESTRING(" \0"));
+       ok1(bs7.ptr == bs.ptr);
+       ok1(bytestring_eq(bs7, BYTESTRING("test")));
+       bs7 = bytestring_splitchrs_next(bs, BYTESTRING(" \0"), bs7);
+       ok1(bs7.ptr == bs.ptr + 5);
+       ok1(bytestring_eq(bs7, BYTESTRING("string")));
+       bs7 = bytestring_splitchrs_next(bs, BYTESTRING(" \0"), bs7);
+       ok1(!bs7.ptr);
+       bs7 = bytestring_splitchrs_next(bs, BYTESTRING(" \0"), bs7);
+       ok1(!bs7.ptr);
+
+       bs7 = bytestring_splitchrs_first(bs2, BYTESTRING(" \0"));
+       ok1(bs7.ptr == bs2.ptr);
+       ok1(bytestring_eq(bs7, BYTESTRING("abc")));
+       bs7 = bytestring_splitchrs_next(bs2, BYTESTRING(" \0"), bs7);
+       ok1(bs7.ptr == bs2.ptr + 4);
+       ok1(bytestring_eq(bs7, BYTESTRING("def")));
+       bs7 = bytestring_splitchrs_next(bs2, BYTESTRING(" \0"), bs7);
+       ok1(!bs7.ptr);
+       bs7 = bytestring_splitchrs_next(bs2, BYTESTRING(" \0"), bs7);
+       ok1(!bs7.ptr);
+
+       bs7 = bytestring_splitchrs_first(BYTESTRING(""), BYTESTRING(" \0"));
+       ok1(!bs7.ptr);
+
+       n = 0;
+       bytestring_foreach_splitchrs(bs7, BYTESTRING(" \0 \0 "),
+                                    BYTESTRING("\0 ")) {
+               n++;
+               ok1(bs7.ptr && !bs7.len);
+       }
+       ok1(n == 6);
+
        /* This exits depending on whether all tests passed */
        return exit_status();
 }