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.
20 #include <libavutil/avutil.h>
29 int bstrcmp(struct bstr str1
, struct bstr str2
)
31 int ret
= memcmp(str1
.start
, str2
.start
, FFMIN(str1
.len
, str2
.len
));
34 if (str1
.len
== str2
.len
)
36 else if (str1
.len
> str2
.len
)
44 int bstrcasecmp(struct bstr str1
, struct bstr str2
)
46 int ret
= strncasecmp(str1
.start
, str2
.start
, FFMIN(str1
.len
, str2
.len
));
49 if (str1
.len
== str2
.len
)
51 else if (str1
.len
> str2
.len
)
59 int bstrchr(struct bstr str
, int c
)
61 for (int i
= 0; i
< str
.len
; i
++)
62 if (str
.start
[i
] == c
)
67 int bstrrchr(struct bstr str
, int c
)
69 for (int i
= str
.len
- 1; i
>= 0; i
--)
70 if (str
.start
[i
] == c
)
75 int bstrcspn(struct bstr str
, const char *reject
)
78 for (i
= 0; i
< str
.len
; i
++)
79 if (strchr(reject
, str
.start
[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
))
92 struct bstr
bstr_lstrip(struct bstr str
)
94 while (str
.len
&& isspace(*str
.start
)) {
101 struct bstr
bstr_strip(struct bstr str
)
103 str
= bstr_lstrip(str
);
104 while (str
.len
&& isspace(str
.start
[str
.len
- 1]))
109 struct bstr
bstr_split(struct bstr str
, const char *sep
, struct bstr
*rest
)
112 for (start
= 0; start
< str
.len
; start
++)
113 if (!strchr(sep
, str
.start
[start
]))
115 str
= bstr_cut(str
, start
);
116 int end
= bstrcspn(str
, sep
);
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
)
130 end
= FFMIN(end
, str
.len
);
131 start
= FFMAX(start
, 0);
132 end
= FFMAX(end
, start
);
134 str
.len
= end
- start
;
138 long long bstrtoll(struct bstr str
, struct bstr
*rest
, int base
)
140 str
= bstr_lstrip(str
);
142 int len
= FFMIN(str
.len
, 50);
143 memcpy(buf
, str
.start
, len
);
146 long long r
= strtoll(buf
, &endptr
, base
);
148 *rest
= bstr_cut(str
, endptr
- buf
);
152 double bstrtod(struct bstr str
, struct bstr
*rest
)
154 str
= bstr_lstrip(str
);
156 int len
= FFMIN(str
.len
, 100);
157 memcpy(buf
, str
.start
, len
);
160 double r
= strtod(buf
, &endptr
);
162 *rest
= bstr_cut(str
, endptr
- buf
);
166 struct bstr
*bstr_splitlines(void *talloc_ctx
, struct bstr str
)
171 for (int i
= 0; i
< str
.len
; i
++)
172 if (str
.start
[i
] == '\n')
174 if (str
.start
[str
.len
- 1] != '\n')
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
++) {
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
;
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
);
198 va_start(va
, format
);
199 int ret
= vsscanf(ptr
, format
, va
);