Browse Source

bytestring: Add bytestring_slice function

Add a bytestring_slice() function to take a sub(byte)string of a
bytestring.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
David Gibson 11 years ago
parent
commit
4008ec0eb0
2 changed files with 36 additions and 2 deletions
  1. 27 0
      ccan/bytestring/bytestring.h
  2. 9 2
      ccan/bytestring/test/run.c

+ 27 - 0
ccan/bytestring/bytestring.h

@@ -95,4 +95,31 @@ static inline char bytestring_byte(struct bytestring s, size_t n)
 	return s.ptr[n];
 	return s.ptr[n];
 }
 }
 
 
+/**
+ * bytestring_slice - extract a substring from a bytestring
+ * @s: bytestring
+ * @start, @end: indexes
+ *
+ * Return a sub-bytestring of @s, starting at byte index @start, and
+ * running to, but not including byte @end.  If @end is before start,
+ * returns a zero-length bytestring.  If @start is out of range,
+ * return a zero length bytestring at the end of @s.
+ *
+ * Note that this doesn't copy or allocate anything - the returned
+ * bytestring occupies (some of) the same memory as the given
+ * bytestring.
+ */
+static inline struct bytestring bytestring_slice(struct bytestring s,
+						 size_t start, size_t end)
+{
+	if (start > s.len)
+		start = s.len;
+	if (end > s.len)
+		end = s.len;
+	if (end < start)
+		end = start;
+
+	return bytestring(s.ptr + start, end - start);
+}
+
 #endif /* CCAN_BYTESTRING_H_ */
 #endif /* CCAN_BYTESTRING_H_ */

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

@@ -12,7 +12,7 @@ int main(void)
 	struct bytestring bs, bs1, bs2, bs3, bs4, bs5;
 	struct bytestring bs, bs1, bs2, bs3, bs4, bs5;
 
 
 	/* This is how many tests you plan to run */
 	/* This is how many tests you plan to run */
-	plan_tests(16);
+	plan_tests(22);
 
 
 	bs = bytestring(str1, sizeof(str1) - 1);
 	bs = bytestring(str1, sizeof(str1) - 1);
 	ok1(bs.ptr == str1);
 	ok1(bs.ptr == str1);
@@ -42,7 +42,14 @@ int main(void)
 	ok1(bytestring_byte(bs2, 4) == 'd');
 	ok1(bytestring_byte(bs2, 4) == 'd');
 	ok1(bytestring_byte(bs2, 5) == 'e');
 	ok1(bytestring_byte(bs2, 5) == 'e');
 	ok1(bytestring_byte(bs2, 6) == 'f');
 	ok1(bytestring_byte(bs2, 6) == 'f');
-	
+
+	ok1(bytestring_eq(bytestring_slice(bs, 0, 4), BYTESTRING("test")));
+	ok1(bytestring_eq(bytestring_slice(bs, 5, 8), BYTESTRING("str")));
+	ok1(bytestring_eq(bytestring_slice(bs2, 2, 5), BYTESTRING("c\0d")));
+	ok1(bytestring_eq(bytestring_slice(bs2, 0, -1U), bs2));
+	ok1(bytestring_eq(bytestring_slice(bs2, 10, 20), bytestring_NULL));
+	ok1(bytestring_eq(bytestring_slice(bs2, 2, 1), bytestring_NULL));
+
 	/* This exits depending on whether all tests passed */
 	/* This exits depending on whether all tests passed */
 	return exit_status();
 	return exit_status();
 }
 }