]> git.ozlabs.org Git - ccan/blobdiff - ccan/bytestring/bytestring.h
bytestring: Add bytestring_index and bytestring_rindex() functions
[ccan] / ccan / bytestring / bytestring.h
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_ */