X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fbytestring%2Fbytestring.h;fp=ccan%2Fbytestring%2Fbytestring.h;h=54e9fa8735122a70f7d7b032b8c776ead3096297;hp=abc7dd7bb0424bb684bdd5000006cfce97cc8578;hb=4a234682efc2a968564b86821435930d221a8218;hpb=4008ec0eb0f9a42827db7dd36d6e00d3c50409e7 diff --git a/ccan/bytestring/bytestring.h b/ccan/bytestring/bytestring.h index abc7dd7b..54e9fa87 100644 --- a/ccan/bytestring/bytestring.h +++ b/ccan/bytestring/bytestring.h @@ -122,4 +122,32 @@ static inline struct bytestring bytestring_slice(struct bytestring s, return bytestring(s.ptr + start, end - start); } +/** + * bytestring_starts - test if the start of one bytestring matches another + * @s, @prefix: bytestrings + * + * Returns true if @prefix appears as a substring at the beginning of + * @s, false otherwise. + */ +static inline bool bytestring_starts(struct bytestring s, + struct bytestring prefix) +{ + return (s.len >= prefix.len) && (memcmp(s.ptr, + prefix.ptr, prefix.len) == 0); +} + +/** + * bytestring_ends - test if the end of one bytestring matches another + * @s, @suffix: bytestrings + * + * Returns true if @suffix appears as a substring at the end of @s, + * false otherwise. + */ +static inline bool bytestring_ends(struct bytestring s, + struct bytestring suffix) +{ + return (s.len >= suffix.len) && (memcmp(s.ptr + s.len - suffix.len, + suffix.ptr, suffix.len) == 0); +} + #endif /* CCAN_BYTESTRING_H_ */