Browse Source

bytestring: Split bytestrings by a string delimiter

This introduces the functions bytestring_splitstr_first() and
bytestring_splitstr_next() which can be used to iterate through substrings
of a bytestring separated by a specified substring.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
David Gibson 11 years ago
parent
commit
3f60b879d0
3 changed files with 104 additions and 1 deletions
  1. 42 0
      ccan/bytestring/bytestring.c
  2. 28 0
      ccan/bytestring/bytestring.h
  3. 34 1
      ccan/bytestring/test/run.c

+ 42 - 0
ccan/bytestring/bytestring.c

@@ -104,3 +104,45 @@ struct bytestring bytestring_splitchrs_next(struct bytestring whole,
 
 
 	return _splitchrs(whole, delim, (prev.ptr - whole.ptr) + prev.len + 1);
 	return _splitchrs(whole, delim, (prev.ptr - whole.ptr) + prev.len + 1);
 }
 }
+
+static struct bytestring _splitstr(struct bytestring whole,
+				   struct bytestring delim, size_t start)
+{
+	struct bytestring remainder, nextdelim;
+
+	assert(start <= whole.len);
+
+	remainder = bytestring_slice(whole, start, whole.len);
+	nextdelim = bytestring_bytestring(remainder, delim);
+	if (nextdelim.ptr)
+		return bytestring_slice(whole, start,
+					nextdelim.ptr - whole.ptr);
+	else
+		return remainder;
+}
+
+struct bytestring bytestring_splitstr_first(struct bytestring whole,
+					     struct bytestring delim)
+{
+	if (whole.len == 0)
+		return bytestring_NULL;
+
+	return _splitstr(whole, delim, 0);
+}
+
+struct bytestring bytestring_splitstr_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 _splitstr(whole, delim,
+			 (prev.ptr - whole.ptr) + prev.len + delim.len);
+}

+ 28 - 0
ccan/bytestring/bytestring.h

@@ -276,4 +276,32 @@ struct bytestring bytestring_splitchrs_next(struct bytestring whole,
 	     (_s).ptr;					       \
 	     (_s).ptr;					       \
 	     (_s) = bytestring_splitchrs_next((_w), (_delim), (_s)))
 	     (_s) = bytestring_splitchrs_next((_w), (_delim), (_s)))
 
 
+/**
+ * bytestring_splitstr_first - split a bytestring on a delimiter string
+ * @whole: a bytestring
+ * @delim: delimiter substring
+ *
+ * Returns the first substring of @whole delimited by the substring in
+ * @delim.
+ */
+struct bytestring bytestring_splitstr_first(struct bytestring whole,
+					     struct bytestring delim);
+
+/**
+ * bytestring_splitstr_next - split a bytestring on a delimiter string
+ * @whole: a bytestring
+ * @delim: delimiter string
+ * @prev: last substring
+ *
+ * Returns the next @delim delimited substring of @whole after @prev.
+ */
+struct bytestring bytestring_splitstr_next(struct bytestring whole,
+					   struct bytestring delim,
+					   struct bytestring prev);
+
+#define bytestring_foreach_splitstr(_s, _w, _delim) \
+	for ((_s) = bytestring_splitstr_first((_w), (_delim)); \
+	     (_s).ptr;					       \
+	     (_s) = bytestring_splitstr_next((_w), (_delim), (_s)))
+
 #endif /* CCAN_BYTESTRING_H_ */
 #endif /* CCAN_BYTESTRING_H_ */

+ 34 - 1
ccan/bytestring/test/run.c

@@ -17,7 +17,7 @@ int main(void)
 	int n;
 	int n;
 
 
 	/* This is how many tests you plan to run */
 	/* This is how many tests you plan to run */
-	plan_tests(109);
+	plan_tests(126);
 
 
 	bs = bytestring(str1, sizeof(str1) - 1);
 	bs = bytestring(str1, sizeof(str1) - 1);
 	ok1(bs.ptr == str1);
 	ok1(bs.ptr == str1);
@@ -198,6 +198,39 @@ int main(void)
 	}
 	}
 	ok1(n == 6);
 	ok1(n == 6);
 
 
+	bs7 = bytestring_splitstr_first(bs, BYTESTRING("t "));
+	ok1(bs7.ptr == bs.ptr);
+	ok1(bytestring_eq(bs7, BYTESTRING("tes")));
+	bs7 = bytestring_splitstr_next(bs, BYTESTRING("t "), bs7);
+	ok1(bs7.ptr == bs.ptr + 5);
+	ok1(bytestring_eq(bs7, BYTESTRING("string")));
+	bs7 = bytestring_splitstr_next(bs, BYTESTRING("t "), bs7);
+	ok1(!bs7.ptr);
+	bs7 = bytestring_splitstr_next(bs, BYTESTRING("t "), bs7);
+	ok1(!bs7.ptr);
+
+	bs7 = bytestring_splitstr_first(bs2, BYTESTRING("abc"));
+	ok1(bs7.ptr == bs2.ptr);
+	ok1(bs7.len == 0);
+	bs7 = bytestring_splitstr_next(bs2, BYTESTRING("abc"), bs7);
+	ok1(bs7.ptr == bs2.ptr + 3);
+	ok1(bytestring_eq(bs7, BYTESTRING("\0def")));
+	bs7 = bytestring_splitstr_next(bs2, BYTESTRING("t "), bs7);
+	ok1(!bs7.ptr);
+	bs7 = bytestring_splitstr_next(bs2, BYTESTRING("t "), bs7);
+	ok1(!bs7.ptr);
+
+	bs7 = bytestring_splitstr_first(BYTESTRING(""), BYTESTRING(""));
+	ok1(!bs7.ptr);
+
+	n = 0;
+	bytestring_foreach_splitstr(bs7, BYTESTRING("foofoo"),
+				     BYTESTRING("foo")) {
+		n++;
+		ok1(bs7.ptr && !bs7.len);
+	}
+	ok1(n == 3);
+	
 	/* This exits depending on whether all tests passed */
 	/* This exits depending on whether all tests passed */
 	return exit_status();
 	return exit_status();
 }
 }