X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fbytestring%2Fbytestring.h;h=62141bea41149a30bd61214e8804d50c23e4c971;hb=f1e31c66572ef5997e08a1ecadf2e45b2fb04532;hp=54e9fa8735122a70f7d7b032b8c776ead3096297;hpb=4a234682efc2a968564b86821435930d221a8218;p=ccan diff --git a/ccan/bytestring/bytestring.h b/ccan/bytestring/bytestring.h index 54e9fa87..62141bea 100644 --- a/ccan/bytestring/bytestring.h +++ b/ccan/bytestring/bytestring.h @@ -2,12 +2,15 @@ #ifndef CCAN_BYTESTRING_H_ #define CCAN_BYTESTRING_H_ +#include "config.h" + #include #include #include #include #include +#include struct bytestring { const char *ptr; @@ -150,4 +153,51 @@ 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; +} + #endif /* CCAN_BYTESTRING_H_ */