find_subfiles: select subtitle files matching -slang
[mplayer/greg.git] / bstr.c
blobf4d3bdef425ffeec8739e110ad1a7e1c72196445
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 #include <string.h>
20 #include <libavutil/avutil.h>
21 #include <assert.h>
23 #include "talloc.h"
25 #include "bstr.h"
27 int bstrcmp(struct bstr str1, struct bstr str2)
29 int ret = memcmp(str1.start, str2.start, FFMIN(str1.len, str2.len));
31 if (!ret) {
32 if (str1.len == str2.len)
33 return 0;
34 else if (str1.len > str2.len)
35 return 1;
36 else
37 return -1;
39 return ret;
42 int bstrcasecmp(struct bstr str1, struct bstr str2)
44 int ret = strncasecmp(str1.start, str2.start, FFMIN(str1.len, str2.len));
46 if (!ret) {
47 if (str1.len == str2.len)
48 return 0;
49 else if (str1.len > str2.len)
50 return 1;
51 else
52 return -1;
54 return ret;
57 int bstrchr(struct bstr str, int c)
59 for (int i = 0; i < str.len; i++)
60 if (str.start[i] == c)
61 return i;
62 return -1;
65 int bstrrchr(struct bstr str, int c)
67 for (int i = str.len - 1; i >= 0; i--)
68 if (str.start[i] == c)
69 return i;
70 return -1;
73 struct bstr bstr_strip(struct bstr str)
75 while (str.len && isspace(*str.start)) {
76 str.start++;
77 str.len--;
79 while (str.len && isspace(str.start[str.len - 1]))
80 str.len--;
81 return str;
84 struct bstr bstr_split(struct bstr str, char *sep, struct bstr *rest)
86 int start, end;
87 for (start = 0; start < str.len; start++)
88 if (!strchr(sep, str.start[start]))
89 break;
90 for (end = start; end < str.len; end++)
91 if (strchr(sep, str.start[end]))
92 break;
93 if (rest) {
94 *rest = bstr_cut(str, end);
96 str.start += start;
97 str.len = end - start;
98 return str;
102 struct bstr bstr_splice(struct bstr str, int start, int end)
104 if (start < 0)
105 start += str.len;
106 if (end < 0)
107 end += str.len;
108 end = FFMIN(end, str.len);
109 start = FFMAX(start, 0);
110 if (start >= end)
111 return (struct bstr){NULL, 0};
112 str.start += start;
113 str.len = end - start;
114 return str;
117 long long bstrtoll(struct bstr str, struct bstr *rest, int base)
119 char buf[51];
120 int len = FFMIN(str.len, 50);
121 memcpy(buf, str.start, len);
122 buf[len] = 0;
123 char *endptr;
124 long long r = strtoll(buf, &endptr, base);
125 if (rest)
126 *rest = bstr_cut(str, endptr - buf);
127 return r;
130 struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str)
132 if (str.len == 0)
133 return NULL;
134 int count = 0;
135 for (int i = 0; i < str.len; i++)
136 if (str.start[i] == '\n')
137 count++;
138 if (str.start[str.len - 1] != '\n')
139 count++;
140 struct bstr *r = talloc_array_ptrtype(talloc_ctx, r, count);
141 unsigned char *p = str.start;
142 for (int i = 0; i < count - 1; i++) {
143 r[i].start = p;
144 while (*p++ != '\n');
145 r[i].len = p - r[i].start;
147 r[count - 1].start = p;
148 r[count - 1].len = str.start + str.len - p;
149 return r;