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>
28 int bstrcmp(struct bstr str1
, struct bstr str2
)
30 int ret
= memcmp(str1
.start
, str2
.start
, FFMIN(str1
.len
, str2
.len
));
33 if (str1
.len
== str2
.len
)
35 else if (str1
.len
> str2
.len
)
43 int bstrcasecmp(struct bstr str1
, struct bstr str2
)
45 int ret
= strncasecmp(str1
.start
, str2
.start
, FFMIN(str1
.len
, str2
.len
));
48 if (str1
.len
== str2
.len
)
50 else if (str1
.len
> str2
.len
)
58 int bstrchr(struct bstr str
, int c
)
60 for (int i
= 0; i
< str
.len
; i
++)
61 if (str
.start
[i
] == c
)
66 int bstrrchr(struct bstr str
, int c
)
68 for (int i
= str
.len
- 1; i
>= 0; i
--)
69 if (str
.start
[i
] == c
)
74 int bstr_find(struct bstr haystack
, struct bstr needle
)
76 for (int i
= 0; i
< haystack
.len
; i
++)
77 if (bstr_startswith(bstr_splice(haystack
, i
, haystack
.len
), needle
))
82 struct bstr
bstr_strip(struct bstr str
)
84 while (str
.len
&& isspace(*str
.start
)) {
88 while (str
.len
&& isspace(str
.start
[str
.len
- 1]))
93 struct bstr
bstr_split(struct bstr str
, char *sep
, struct bstr
*rest
)
96 for (start
= 0; start
< str
.len
; start
++)
97 if (!strchr(sep
, str
.start
[start
]))
99 for (end
= start
; end
< str
.len
; end
++)
100 if (strchr(sep
, str
.start
[end
]))
103 *rest
= bstr_cut(str
, end
);
106 str
.len
= end
- start
;
111 struct bstr
bstr_splice(struct bstr str
, int start
, int end
)
117 end
= FFMIN(end
, str
.len
);
118 start
= FFMAX(start
, 0);
120 return (struct bstr
){NULL
, 0};
122 str
.len
= end
- start
;
126 long long bstrtoll(struct bstr str
, struct bstr
*rest
, int base
)
129 int len
= FFMIN(str
.len
, 50);
130 memcpy(buf
, str
.start
, len
);
133 long long r
= strtoll(buf
, &endptr
, base
);
135 *rest
= bstr_cut(str
, endptr
- buf
);
139 struct bstr
*bstr_splitlines(void *talloc_ctx
, struct bstr str
)
144 for (int i
= 0; i
< str
.len
; i
++)
145 if (str
.start
[i
] == '\n')
147 if (str
.start
[str
.len
- 1] != '\n')
149 struct bstr
*r
= talloc_array_ptrtype(talloc_ctx
, r
, count
);
150 unsigned char *p
= str
.start
;
151 for (int i
= 0; i
< count
- 1; i
++) {
153 while (*p
++ != '\n');
154 r
[i
].len
= p
- r
[i
].start
;
156 r
[count
- 1].start
= p
;
157 r
[count
- 1].len
= str
.start
+ str
.len
- p
;
161 void bstr_lower(struct bstr str
)
163 for (int i
= 0; i
< str
.len
; i
++)
164 str
.start
[i
] = tolower(str
.start
[i
]);