summaryrefslogtreecommitdiffstats
path: root/bstr.h
diff options
context:
space:
mode:
authorUoti Urpala <uau@glyph.nonexistent.invalid>2011-02-23 16:18:09 +0200
committerUoti Urpala <uau@glyph.nonexistent.invalid>2011-02-26 16:40:31 +0200
commit962eec0440ad65a71c09a9e206e110c62d9de3c8 (patch)
tree7e1f2b9406fa34859dfe67de8d4ea6b80bc09408 /bstr.h
parente8af22db818f839f5ac25a309e8838ea53e831b2 (diff)
downloadmpv-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.h36
1 files changed, 34 insertions, 2 deletions
diff --git a/bstr.h b/bstr.h
index 459bf70bef..33a47c0abc 100644
--- a/bstr.h
+++ b/bstr.h
@@ -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 */