find_subfiles: select subtitle files matching -slang
[mplayer/greg.git] / bstr.h
blobcbd96f49c945911a597ace1e5fda18d0dd1a6250
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #ifndef MPLAYER_BSTR_H
20 #define MPLAYER_BSTR_H
22 #include <stdint.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdbool.h>
27 #include "talloc.h"
29 /* NOTE: 'len' is size_t, but most string-handling functions below assume
30 * that input size has been sanity checked and len fits in an int.
32 struct bstr {
33 unsigned char *start;
34 size_t len;
37 int bstrcmp(struct bstr str1, struct bstr str2);
38 int bstrcasecmp(struct bstr str1, struct bstr str2);
39 int bstrchr(struct bstr str, int c);
40 int bstrrchr(struct bstr str, int c);
41 struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str);
42 struct bstr bstr_strip(struct bstr str);
43 struct bstr bstr_split(struct bstr str, char *sep, struct bstr *rest);
44 struct bstr bstr_splice(struct bstr str, int start, int end);
45 long long bstrtoll(struct bstr str, struct bstr *rest, int base);
47 static inline struct bstr bstr_cut(struct bstr str, int n)
49 return (struct bstr){str.start + n, str.len - n};
52 static inline bool bstr_startswith(struct bstr str, struct bstr prefix)
54 if (str.len < prefix.len)
55 return false;
56 return !memcmp(str.start, prefix.start, prefix.len);
59 static inline char *bstrdup0(void *talloc_ctx, struct bstr str)
61 // cast is live555 C++ compilation workaround
62 return talloc_strndup(talloc_ctx, (char *)str.start, str.len);
65 // Create bstr compound literal from null-terminated string
66 #define BSTR(s) (struct bstr){(char *)(s), (s) ? strlen(s) : 0}
67 // create a pair (not single value!) for "%.*s" printf syntax
68 #define BSTR_P(bstr) (int)((bstr).len), (bstr).start
70 #define WHITESPACE " \f\n\r\t\v"
72 #endif /* MPLAYER_BSTR_H */