diff options
author | Uoti Urpala <uau@glyph.nonexistent.invalid> | 2011-02-23 16:18:09 +0200 |
---|---|---|
committer | Uoti Urpala <uau@glyph.nonexistent.invalid> | 2011-02-26 16:40:31 +0200 |
commit | 962eec0440ad65a71c09a9e206e110c62d9de3c8 (patch) | |
tree | 7e1f2b9406fa34859dfe67de8d4ea6b80bc09408 /bstr.h | |
parent | e8af22db818f839f5ac25a309e8838ea53e831b2 (diff) | |
download | mpv-962eec0440ad65a71c09a9e206e110c62d9de3c8.tar.bz2 mpv-962eec0440ad65a71c09a9e206e110c62d9de3c8.tar.xz |
bstr.[ch], path.[ch]: add string and path handling functions
Add some new string and path handling functions to be used in
following commits.
Use new path handling functions to simplify find_files().
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 */ |