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>
27 int bstrcmp(struct bstr str1
, struct bstr str2
)
29 int ret
= memcmp(str1
.start
, str2
.start
, FFMIN(str1
.len
, str2
.len
));
32 if (str1
.len
== str2
.len
)
34 else if (str1
.len
> str2
.len
)
42 int bstrcasecmp(struct bstr str1
, struct bstr str2
)
44 int ret
= strncasecmp(str1
.start
, str2
.start
, FFMIN(str1
.len
, str2
.len
));
47 if (str1
.len
== str2
.len
)
49 else if (str1
.len
> str2
.len
)
57 int bstrchr(struct bstr str
, int c
)
59 for (int i
= 0; i
< str
.len
; i
++)
60 if (str
.start
[i
] == c
)
65 int bstrrchr(struct bstr str
, int c
)
67 for (int i
= str
.len
- 1; i
>= 0; i
--)
68 if (str
.start
[i
] == c
)
73 struct bstr
bstr_strip(struct bstr str
)
75 while (str
.len
&& isspace(*str
.start
)) {
79 while (str
.len
&& isspace(str
.start
[str
.len
- 1]))
84 struct bstr
bstr_split(struct bstr str
, char *sep
, struct bstr
*rest
)
87 for (start
= 0; start
< str
.len
; start
++)
88 if (!strchr(sep
, str
.start
[start
]))
90 for (end
= start
; end
< str
.len
; end
++)
91 if (strchr(sep
, str
.start
[end
]))
94 *rest
= bstr_cut(str
, end
);
97 str
.len
= end
- start
;
102 struct bstr
bstr_splice(struct bstr str
, int start
, int end
)
108 end
= FFMIN(end
, str
.len
);
109 start
= FFMAX(start
, 0);
111 return (struct bstr
){NULL
, 0};
113 str
.len
= end
- start
;
117 long long bstrtoll(struct bstr str
, struct bstr
*rest
, int base
)
120 int len
= FFMIN(str
.len
, 50);
121 memcpy(buf
, str
.start
, len
);
124 long long r
= strtoll(buf
, &endptr
, base
);
126 *rest
= bstr_cut(str
, endptr
- buf
);
130 struct bstr
*bstr_splitlines(void *talloc_ctx
, struct bstr str
)
135 for (int i
= 0; i
< str
.len
; i
++)
136 if (str
.start
[i
] == '\n')
138 if (str
.start
[str
.len
- 1] != '\n')
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
++) {
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
;