]> git.ozlabs.org Git - ccan/blobdiff - ccan/bytestring/bytestring.h
bytestring: Split bytestrings by a character delimiter
[ccan] / ccan / bytestring / bytestring.h
index 54e9fa8735122a70f7d7b032b8c776ead3096297..8aa272755caa90b2ad1c18655a8195093d7ec7cf 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,97 @@ 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);
+}
+
+/*
+ * bytestring_bytestring - search for a bytestring in another bytestring
+ * @haystack, @needle: bytestrings
+ *
+ * Returns a bytestring corresponding to the first occurrence of
+ * @needle in @haystack, or bytestring_NULL if @needle is not found
+ * within @haystack.
+ */
+static inline struct bytestring bytestring_bytestring(struct bytestring haystack,
+                                                     struct bytestring needle)
+{
+       const char *p = memmem(haystack.ptr, haystack.len,
+                              needle.ptr, needle.len);
+       if (p)
+               return bytestring(p, needle.len);
+       else
+               return bytestring_NULL;
+}
+
+/**
+ * bytestring_spn - search a bytestring for a set of bytes
+ * @s: a bytestring
+ * @accept: a bytestring containing a set of bytes to accept
+ *
+ * Returns the length, in bytes, of the initial segment of @s which
+ * consists entirely of characters in @accept.
+ */
+size_t bytestring_spn(struct bytestring s, struct bytestring accept);
+
+/**
+ * bytestring_cspn - search a bytestring for a set of bytes (complemented)
+ * @s: a bytestring
+ * @reject: a bytestring containing a set of bytes to reject
+ *
+ * Returns the length, in bytes, of the initial segment of @s which
+ * consists entirely of characters not in @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_ */