diff options
Diffstat (limited to 'bstr.h')
-rw-r--r-- | bstr.h | 36 |
1 files changed, 34 insertions, 2 deletions
@@ -22,18 +22,50 @@ #include <stdint.h> #include <stddef.h> #include <string.h> +#include <stdbool.h> +#include "talloc.h" + +/* NOTE: 'len' is size_t, but most string-handling functions below assume + * that input size has been sanity checked and len fits in an int. + */ struct bstr { - const uint8_t *start; + unsigned char *start; size_t len; }; int bstrcmp(struct bstr str1, struct bstr str2); int bstrcasecmp(struct bstr str1, struct bstr str2); +int bstrchr(struct bstr str, int c); +struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str); +struct bstr bstr_strip(struct bstr str); +struct bstr bstr_split(struct bstr str, char *sep, struct bstr *rest); +struct bstr bstr_splice(struct bstr str, int start, int end); +long long bstrtoll(struct bstr str, struct bstr *rest, int base); + +static inline struct bstr bstr_cut(struct bstr str, int n) +{ + return (struct bstr){str.start + n, str.len - n}; +} + +static inline bool bstr_startswith(struct bstr str, struct bstr prefix) +{ + if (str.len < prefix.len) + return false; + return !memcmp(str.start, prefix.start, prefix.len); +} + +static inline char *bstrdup0(void *talloc_ctx, struct bstr str) +{ + // cast is live555 C++ compilation workaround + return talloc_strndup(talloc_ctx, (char *)str.start, str.len); +} // Create bstr compound literal from null-terminated string -#define BSTR(s) (struct bstr){(s), (s) ? strlen(s) : 0} +#define BSTR(s) (struct bstr){(char *)(s), (s) ? strlen(s) : 0} // create a pair (not single value!) for "%.*s" printf syntax #define BSTR_P(bstr) (int)((bstr).len), (bstr).start +#define WHITESPACE " \f\n\r\t\v" + #endif /* MPLAYER_BSTR_H */ |