Browse Source

bytestring: Split bytestrings by a character delimiter

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>
David Gibson 11 years ago
parent
commit
cb2d540f76
3 changed files with 135 additions and 2 deletions
  1. 42 0
      ccan/bytestring/bytestring.c
  2. 26 0
      ccan/bytestring/bytestring.h
  3. 67 2
      ccan/bytestring/test/run.c

+ 42 - 0
ccan/bytestring/bytestring.c

@@ -24,3 +24,45 @@ size_t bytestring_cspn(struct bytestring s, struct bytestring reject)
 
 
 	return s.len;
 	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);
+}

+ 26 - 0
ccan/bytestring/bytestring.h

@@ -220,4 +220,30 @@ size_t bytestring_spn(struct bytestring s, struct bytestring accept);
  */
  */
 size_t bytestring_cspn(struct bytestring s, struct bytestring reject);
 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_ */
 #endif /* CCAN_BYTESTRING_H_ */

+ 67 - 2
ccan/bytestring/test/run.c

@@ -13,10 +13,11 @@ const char *str2 = TEST_STRING;
 
 
 int main(void)
 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 */
 	/* This is how many tests you plan to run */
-	plan_tests(53);
+	plan_tests(89);
 
 
 	bs = bytestring(str1, sizeof(str1) - 1);
 	bs = bytestring(str1, sizeof(str1) - 1);
 	ok1(bs.ptr == str1);
 	ok1(bs.ptr == str1);
@@ -100,6 +101,70 @@ int main(void)
 	ok1(bytestring_spn(bs1, BYTESTRING("eginrst ")) == bs1.len);
 	ok1(bytestring_spn(bs1, BYTESTRING("eginrst ")) == bs1.len);
 	ok1(bytestring_cspn(bs2, BYTESTRING("z")) == bs2.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 */
 	/* This exits depending on whether all tests passed */
 	return exit_status();
 	return exit_status();
 }
 }