]> git.ozlabs.org Git - ccan/blobdiff - ccan/bytestring/bytestring.h
bytestring: Use CONST_FUNCTION
[ccan] / ccan / bytestring / bytestring.h
index 5a8d3cd896fb003704bada0f535d4e11c5354171..fd5187701b3430bd03f5cc60be080f0dfdc57b49 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <ccan/array_size/array_size.h>
 #include <ccan/mem/mem.h>
+#include <ccan/compiler/compiler.h>
 
 struct bytestring {
        const char *ptr;
@@ -29,7 +30,8 @@ struct bytestring {
  *     struct bytestring bs = bytestring(x, 5);
  *     assert(bs.len == 5);
  */
-static inline struct bytestring bytestring(const char *p, size_t l)
+static inline CONST_FUNCTION struct bytestring
+bytestring(const char *p, size_t l)
 {
        struct bytestring bs = {
                .ptr = p,
@@ -276,4 +278,32 @@ struct bytestring bytestring_splitchrs_next(struct bytestring whole,
             (_s).ptr;                                         \
             (_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_ */