X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fbytestring%2Fbytestring.h;h=dd1f913ca45ee2f0dbe7d66f50f175256927095b;hb=712dc43d55741bc11490abae8f7edbb8ec982f0d;hp=62141bea41149a30bd61214e8804d50c23e4c971;hpb=f1e31c66572ef5997e08a1ecadf2e45b2fb04532;p=ccan diff --git a/ccan/bytestring/bytestring.h b/ccan/bytestring/bytestring.h index 62141bea..dd1f913c 100644 --- a/ccan/bytestring/bytestring.h +++ b/ccan/bytestring/bytestring.h @@ -11,6 +11,7 @@ #include #include +#include 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, @@ -53,6 +55,18 @@ static inline struct bytestring bytestring(const char *p, size_t l) */ #define BYTESTRING(s) (bytestring((s), ARRAY_SIZE(s) - 1)) +/** + * BYTESTRING_INIT - bytestring initializer + * @s: string literal + * + * Produces an initializer for a bytestring from a literal string. + * The resulting bytestring will not include the terminating \0, but + * will include any internal \0s. + * + * Example: + * static const struct bytestring CONSTANT = BYTESTRING_INIT("CONSTANT"); + */ +#define BYTESTRING_INIT(s) { .ptr = (s), .len = ARRAY_SIZE(s) - 1} /** * bytestring_from_string - construct a bytestring from a NUL terminated string @@ -200,4 +214,108 @@ static inline struct bytestring bytestring_bytestring(struct bytestring haystack 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))) + +/** + * bytestring_splitchrs_first - split a bytestring on a set of delimiter + * characters + * @whole: a bytestring + * @delim: delimiter characters + * + * Returns the first substring of @whole delimited by any character in + * @delim. + */ +struct bytestring bytestring_splitchrs_first(struct bytestring whole, + struct bytestring delim); + +/** + * bytestring_splitchr_next - split a bytestring on a set of delimiter + * characters + * @whole: a bytestring + * @delim: delimiter character + * @prev: last substring + * + * Returns the next @delim delimited substring of @whole after @prev. + */ +struct bytestring bytestring_splitchrs_next(struct bytestring whole, + struct bytestring delim, + struct bytestring prev); + +#define bytestring_foreach_splitchrs(_s, _w, _delim) \ + for ((_s) = bytestring_splitchrs_first((_w), (_delim)); \ + (_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_ */