summaryrefslogtreecommitdiffstats
path: root/bstr.h
diff options
context:
space:
mode:
authorUoti Urpala <uau@mplayer2.org>2011-07-28 11:07:47 +0300
committerUoti Urpala <uau@mplayer2.org>2011-07-29 05:50:38 +0300
commite873d703e956d3e2e68b9e18562983b029b5c7a8 (patch)
tree44a2c48ae4e94bd580ffd5833cd71a037af36043 /bstr.h
parentd8374376c0d46ffab88b96eb32b52621c34f562c (diff)
downloadmpv-e873d703e956d3e2e68b9e18562983b029b5c7a8.tar.bz2
mpv-e873d703e956d3e2e68b9e18562983b029b5c7a8.tar.xz
options: change option parsing to use bstr
Using bstr allows simpler parsing code, especially because it avoids the need to modify or copy strings just to terminate extracted substrings.
Diffstat (limited to 'bstr.h')
-rw-r--r--bstr.h57
1 files changed, 47 insertions, 10 deletions
diff --git a/bstr.h b/bstr.h
index 1d26cb2728..1344f0d443 100644
--- a/bstr.h
+++ b/bstr.h
@@ -34,22 +34,45 @@ struct bstr {
size_t len;
};
+// demux_rtp.cpp (live555) C++ compilation workaround
+#ifndef __cplusplus
+static inline char *bstrdup0(void *talloc_ctx, struct bstr str)
+{
+ return talloc_strndup(talloc_ctx, (char *)str.start, str.len);
+}
+
+static inline struct bstr bstrdup(void *talloc_ctx, struct bstr str)
+{
+ struct bstr r = { talloc_strndup(talloc_ctx, str.start, str.len), str.len };
+ return r;
+}
+
+static inline struct bstr bstr(const unsigned char *s)
+{
+ return (struct bstr){(unsigned char *)s, s ? strlen(s) : 0};
+}
+
int bstrcmp(struct bstr str1, struct bstr str2);
int bstrcasecmp(struct bstr str1, struct bstr str2);
int bstrchr(struct bstr str, int c);
int bstrrchr(struct bstr str, int c);
+int bstrcspn(struct bstr str, const char *reject);
int bstr_find(struct bstr haystack, struct bstr needle);
struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str);
struct bstr bstr_lstrip(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_split(struct bstr str, const 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);
+double bstrtod(struct bstr str, struct bstr *rest);
void bstr_lower(struct bstr str);
+int bstr_sscanf(struct bstr str, const char *format, ...);
static inline struct bstr bstr_cut(struct bstr str, int n)
{
+ if (n > str.len)
+ n = str.len;
return (struct bstr){str.start + n, str.len - n};
}
@@ -60,22 +83,36 @@ static inline bool bstr_startswith(struct bstr str, struct bstr prefix)
return !memcmp(str.start, prefix.start, prefix.len);
}
-// demux_rtp.cpp (live555) C++ compilation workaround
-#ifndef __cplusplus
-static inline char *bstrdup0(void *talloc_ctx, struct bstr str)
+static inline bool bstr_startswith0(struct bstr str, const char *prefix)
{
- return talloc_strndup(talloc_ctx, (char *)str.start, str.len);
+ return bstr_startswith(str, bstr(prefix));
}
-static inline struct bstr bstrdup(void *talloc_ctx, struct bstr str)
+static inline bool bstr_endswith(struct bstr str, struct bstr suffix)
{
- struct bstr r = { talloc_strndup(talloc_ctx, str.start, str.len), str.len };
- return r;
+ if (str.len < suffix.len)
+ return false;
+ return !memcmp(str.start + str.len - suffix.len, suffix.start, suffix.len);
}
-static inline struct bstr bstr(const unsigned char *s)
+static inline bool bstr_endswith0(struct bstr str, const char *suffix)
{
- return (struct bstr){(unsigned char *)s, s ? strlen(s) : 0};
+ return bstr_endswith(str, bstr(suffix));
+}
+
+static inline int bstrcmp0(struct bstr str1, const char *str2)
+{
+ return bstrcmp(str1, bstr(str2));
+}
+
+static inline int bstrcasecmp0(struct bstr str1, const char *str2)
+{
+ return bstrcasecmp(str1, bstr(str2));
+}
+
+static inline int bstr_find0(struct bstr haystack, const char *needle)
+{
+ return bstr_find(haystack, bstr(needle));
}
#endif