[ccan] [PATCH 7/9] bytestring: Split bytestrings by a character delimiter

David Gibson david at gibson.dropbear.id.au
Mon Jul 28 19:59:55 EST 2014


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 at gibson.dropbear.id.au>
---
 ccan/bytestring/bytestring.c | 42 +++++++++++++++++++++++++++
 ccan/bytestring/bytestring.h | 26 +++++++++++++++++
 ccan/bytestring/test/run.c   | 69 ++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 135 insertions(+), 2 deletions(-)

diff --git a/ccan/bytestring/bytestring.c b/ccan/bytestring/bytestring.c
index 2e205b7..45c1297 100644
--- a/ccan/bytestring/bytestring.c
+++ b/ccan/bytestring/bytestring.c
@@ -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);
+}
diff --git a/ccan/bytestring/bytestring.h b/ccan/bytestring/bytestring.h
index e93c634..9dfa9ed 100644
--- a/ccan/bytestring/bytestring.h
+++ b/ccan/bytestring/bytestring.h
@@ -201,4 +201,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_ */
diff --git a/ccan/bytestring/test/run.c b/ccan/bytestring/test/run.c
index 7e7b2f0..f6f8c5a 100644
--- a/ccan/bytestring/test/run.c
+++ b/ccan/bytestring/test/run.c
@@ -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(46);
+	plan_tests(82);
 
 	bs = bytestring(str1, sizeof(str1) - 1);
 	ok1(bs.ptr == str1);
@@ -92,6 +93,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();
 }
-- 
1.9.3



More information about the ccan mailing list