]> git.ozlabs.org Git - ccan/commitdiff
bytestring: Add bytestring_index and bytestring_rindex() functions
authorDavid Gibson <david@gibson.dropbear.id.au>
Wed, 22 Oct 2014 13:35:26 +0000 (15:35 +0200)
committerDavid Gibson <david@gibson.dropbear.id.au>
Thu, 30 Oct 2014 00:35:15 +0000 (11:35 +1100)
Add bytestring equivalents of the index() and rindex() standard functions
which search for characters/bytes within a bytestring.

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

index c2aab2d8d562a7b4440c8ee868fe98bcc05bf1e6..f68a4c73393c956251f680836fc9ec5ac1aab607 100644 (file)
@@ -35,6 +35,7 @@ int main(int argc, char *argv[])
 
        if (strcmp(argv[1], "depends") == 0) {
                printf("ccan/array_size\n");
+               printf("ccan/mem\n");
                return 0;
        }
 
index 54e9fa8735122a70f7d7b032b8c776ead3096297..042b1843220192bd1c4d2614cdd75df303812bc0 100644 (file)
@@ -2,12 +2,15 @@
 #ifndef CCAN_BYTESTRING_H_
 #define CCAN_BYTESTRING_H_
 
+#include "config.h"
+
 #include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
 #include <assert.h>
 
 #include <ccan/array_size/array_size.h>
+#include <ccan/mem/mem.h>
 
 struct bytestring {
        const char *ptr;
@@ -150,4 +153,32 @@ static inline bool bytestring_ends(struct bytestring s,
                                                suffix.ptr, suffix.len) == 0);
 }
 
+/**
+ * bytestring_index - locate character in bytestring
+ * @haystack: a bytestring
+ * @needle: a character or byte value
+ *
+ * Returns a pointer to the first occurrence of @needle within
+ * @haystack, or NULL if @needle does not appear in @haystack.
+ */
+static inline const char *bytestring_index(struct bytestring haystack,
+                                          char needle)
+{
+       return memchr(haystack.ptr, needle, haystack.len);
+}
+
+/**
+ * bytestring_rindex - locate character in bytestring
+ * @haystack: a bytestring
+ * @needle: a character or byte value
+ *
+ * Returns a pointer to the last occurrence of @needle within
+ * @haystack, or NULL if @needle does not appear in @haystack.
+ */
+static inline const char *bytestring_rindex(struct bytestring haystack,
+                                          char needle)
+{
+       return memrchr(haystack.ptr, needle, haystack.len);
+}
+
 #endif /* CCAN_BYTESTRING_H_ */
index 7ab06f82163d9cf3fe60be33a8accf20cf1e7415..3c62f94173d354ed41364d3a26b48850a15c7737 100644 (file)
@@ -1,3 +1,5 @@
+#include "config.h"
+
 #include <stdio.h>
 
 #include <ccan/bytestring/bytestring.h>
index f3fcb20d6208fdbf1e1388de188503fc7dc6b5bc..1e885751b9efe9600f39e58a24da6ad9ea05aed4 100644 (file)
@@ -1,3 +1,5 @@
+#include "config.h"
+
 #include <stdio.h>
 
 #include <ccan/bytestring/bytestring.h>
index ac6368d87495b67894ce69b869d64b82025b175c..50d288537fc77533fd103de71c1d6c876fd172d8 100644 (file)
@@ -1,3 +1,5 @@
+#include "config.h"
+
 #include <ccan/bytestring/bytestring.h>
 #include <ccan/tap/tap.h>
 
@@ -12,7 +14,7 @@ int main(void)
        struct bytestring bs, bs1, bs2, bs3, bs4, bs5;
 
        /* This is how many tests you plan to run */
-       plan_tests(30);
+       plan_tests(42);
 
        bs = bytestring(str1, sizeof(str1) - 1);
        ok1(bs.ptr == str1);
@@ -59,6 +61,20 @@ int main(void)
        ok1(!bytestring_starts(bs2, BYTESTRING("def")));
        ok1(!bytestring_ends(bs2, BYTESTRING("abc")));
 
+       ok1(bytestring_index(bs1, ' ') == (bs1.ptr + 4));
+       ok1(bytestring_index(bs1, 't') == (bs1.ptr + 0));
+       ok1(bytestring_index(bs1, 0) == NULL);
+       ok1(bytestring_index(bs2, 0) == (bs2.ptr + 3));
+       ok1(bytestring_index(bs2, 'f') == (bs2.ptr + 6));
+       ok1(bytestring_index(bs2, 'q') == NULL);
+
+       ok1(bytestring_rindex(bs1, ' ') == (bs1.ptr + 4));
+       ok1(bytestring_rindex(bs1, 't') == (bs1.ptr + 6));
+       ok1(bytestring_rindex(bs1, 0) == NULL);
+       ok1(bytestring_rindex(bs2, 0) == (bs2.ptr + 3));
+       ok1(bytestring_rindex(bs2, 'f') == (bs2.ptr + 6));
+       ok1(bytestring_rindex(bs2, 'q') == NULL);
+
        /* This exits depending on whether all tests passed */
        return exit_status();
 }