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 struct bstr
bstr_strip(struct bstr str
)
67 while (str
.len
&& isspace(*str
.start
)) {
71 while (str
.len
&& isspace(str
.start
[str
.len
- 1]))
76 struct bstr
bstr_split(struct bstr str
, char *sep
, struct bstr
*rest
)
79 for (start
= 0; start
< str
.len
; start
++)
80 if (!strchr(sep
, str
.start
[start
]))
82 for (end
= start
; end
< str
.len
; end
++)
83 if (strchr(sep
, str
.start
[end
]))
86 *rest
= bstr_cut(str
, end
);
89 str
.len
= end
- start
;
94 struct bstr
bstr_splice(struct bstr str
, int start
, int end
)
100 end
= FFMIN(end
, str
.len
);
101 start
= FFMAX(start
, 0);
103 return (struct bstr
){NULL
, 0};
105 str
.len
= end
- start
;
109 long long bstrtoll(struct bstr str
, struct bstr
*rest
, int base
)
112 int len
= FFMIN(str
.len
, 50);
113 memcpy(buf
, str
.start
, len
);
116 long long r
= strtoll(buf
, &endptr
, base
);
118 *rest
= bstr_cut(str
, endptr
- buf
);
122 struct bstr
*bstr_splitlines(void *talloc_ctx
, struct bstr str
)
127 for (int i
= 0; i
< str
.len
; i
++)
128 if (str
.start
[i
] == '\n')
130 if (str
.start
[str
.len
- 1] != '\n')
132 struct bstr
*r
= talloc_array_ptrtype(talloc_ctx
, r
, count
);
133 unsigned char *p
= str
.start
;
134 for (int i
= 0; i
< count
- 1; i
++) {
136 while (*p
++ != '\n');
137 r
[i
].len
= p
- r
[i
].start
;
139 r
[count
- 1].start
= p
;
140 r
[count
- 1].len
= str
.start
+ str
.len
- p
;