vo_gl: remove "noaspect" suboption
[mplayer.git] / bstr.c
blob219c136d7cf9801d55c21f7366f532a92f96d919
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>
22 #include <ctype.h>
23 #include <stdarg.h>
25 #include "talloc.h"
27 #include "bstr.h"
29 int bstrcmp(struct bstr str1, struct bstr str2)
31 int ret = memcmp(str1.start, str2.start, FFMIN(str1.len, str2.len));
33 if (!ret) {
34 if (str1.len == str2.len)
35 return 0;
36 else if (str1.len > str2.len)
37 return 1;
38 else
39 return -1;
41 return ret;
44 int bstrcasecmp(struct bstr str1, struct bstr str2)
46 int ret = strncasecmp(str1.start, str2.start, FFMIN(str1.len, str2.len));
48 if (!ret) {
49 if (str1.len == str2.len)
50 return 0;
51 else if (str1.len > str2.len)
52 return 1;
53 else
54 return -1;
56 return ret;
59 int bstrchr(struct bstr str, int c)
61 for (int i = 0; i < str.len; i++)
62 if (str.start[i] == c)
63 return i;
64 return -1;
67 int bstrrchr(struct bstr str, int c)
69 for (int i = str.len - 1; i >= 0; i--)
70 if (str.start[i] == c)
71 return i;
72 return -1;
75 int bstrcspn(struct bstr str, const char *reject)
77 int i;
78 for (i = 0; i < str.len; i++)
79 if (strchr(reject, str.start[i]))
80 break;
81 return i;
84 int bstr_find(struct bstr haystack, struct bstr needle)
86 for (int i = 0; i < haystack.len; i++)
87 if (bstr_startswith(bstr_splice(haystack, i, haystack.len), needle))
88 return i;
89 return -1;
92 struct bstr bstr_lstrip(struct bstr str)
94 while (str.len && isspace(*str.start)) {
95 str.start++;
96 str.len--;
98 return str;
101 struct bstr bstr_strip(struct bstr str)
103 str = bstr_lstrip(str);
104 while (str.len && isspace(str.start[str.len - 1]))
105 str.len--;
106 return str;
109 struct bstr bstr_split(struct bstr str, const char *sep, struct bstr *rest)
111 int start;
112 for (start = 0; start < str.len; start++)
113 if (!strchr(sep, str.start[start]))
114 break;
115 str = bstr_cut(str, start);
116 int end = bstrcspn(str, sep);
117 if (rest) {
118 *rest = bstr_cut(str, end);
120 return bstr_splice(str, 0, end);
124 struct bstr bstr_splice(struct bstr str, int start, int end)
126 if (start < 0)
127 start += str.len;
128 if (end < 0)
129 end += str.len;
130 end = FFMIN(end, str.len);
131 start = FFMAX(start, 0);
132 end = FFMAX(end, start);
133 str.start += start;
134 str.len = end - start;
135 return str;
138 long long bstrtoll(struct bstr str, struct bstr *rest, int base)
140 str = bstr_lstrip(str);
141 char buf[51];
142 int len = FFMIN(str.len, 50);
143 memcpy(buf, str.start, len);
144 buf[len] = 0;
145 char *endptr;
146 long long r = strtoll(buf, &endptr, base);
147 if (rest)
148 *rest = bstr_cut(str, endptr - buf);
149 return r;
152 double bstrtod(struct bstr str, struct bstr *rest)
154 str = bstr_lstrip(str);
155 char buf[101];
156 int len = FFMIN(str.len, 100);
157 memcpy(buf, str.start, len);
158 buf[len] = 0;
159 char *endptr;
160 double r = strtod(buf, &endptr);
161 if (rest)
162 *rest = bstr_cut(str, endptr - buf);
163 return r;
166 struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str)
168 if (str.len == 0)
169 return NULL;
170 int count = 0;
171 for (int i = 0; i < str.len; i++)
172 if (str.start[i] == '\n')
173 count++;
174 if (str.start[str.len - 1] != '\n')
175 count++;
176 struct bstr *r = talloc_array_ptrtype(talloc_ctx, r, count);
177 unsigned char *p = str.start;
178 for (int i = 0; i < count - 1; i++) {
179 r[i].start = p;
180 while (*p++ != '\n');
181 r[i].len = p - r[i].start;
183 r[count - 1].start = p;
184 r[count - 1].len = str.start + str.len - p;
185 return r;
188 void bstr_lower(struct bstr str)
190 for (int i = 0; i < str.len; i++)
191 str.start[i] = tolower(str.start[i]);
194 int bstr_sscanf(struct bstr str, const char *format, ...)
196 char *ptr = bstrdup0(NULL, str);
197 va_list va;
198 va_start(va, format);
199 int ret = vsscanf(ptr, format, va);
200 va_end(va);
201 talloc_free(ptr);
202 return ret;