core: support timeline with audio-only files
[mplayer.git] / bstr.c
blobd86b48891280ba82eaaa53fb6098c3792f34cfcc
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>
23 #include "talloc.h"
25 #include "bstr.h"
27 int bstrcmp(struct bstr str1, struct bstr str2)
29 int ret = memcmp(str1.start, str2.start, FFMIN(str1.len, str2.len));
31 if (!ret) {
32 if (str1.len == str2.len)
33 return 0;
34 else if (str1.len > str2.len)
35 return 1;
36 else
37 return -1;
39 return ret;
42 int bstrcasecmp(struct bstr str1, struct bstr str2)
44 int ret = strncasecmp(str1.start, str2.start, FFMIN(str1.len, str2.len));
46 if (!ret) {
47 if (str1.len == str2.len)
48 return 0;
49 else if (str1.len > str2.len)
50 return 1;
51 else
52 return -1;
54 return ret;
57 int bstrchr(struct bstr str, int c)
59 for (int i = 0; i < str.len; i++)
60 if (str.start[i] == c)
61 return i;
62 return -1;
65 struct bstr bstr_strip(struct bstr str)
67 while (str.len && isspace(*str.start)) {
68 str.start++;
69 str.len--;
71 while (str.len && isspace(str.start[str.len - 1]))
72 str.len--;
73 return str;
76 struct bstr bstr_split(struct bstr str, char *sep, struct bstr *rest)
78 int start, end;
79 for (start = 0; start < str.len; start++)
80 if (!strchr(sep, str.start[start]))
81 break;
82 for (end = start; end < str.len; end++)
83 if (strchr(sep, str.start[end]))
84 break;
85 if (rest) {
86 *rest = bstr_cut(str, end);
88 str.start += start;
89 str.len = end - start;
90 return str;
94 struct bstr bstr_splice(struct bstr str, int start, int end)
96 if (start < 0)
97 start += str.len;
98 if (end < 0)
99 end += str.len;
100 end = FFMIN(end, str.len);
101 start = FFMAX(start, 0);
102 if (start >= end)
103 return (struct bstr){NULL, 0};
104 str.start += start;
105 str.len = end - start;
106 return str;
109 long long bstrtoll(struct bstr str, struct bstr *rest, int base)
111 char buf[51];
112 int len = FFMIN(str.len, 50);
113 memcpy(buf, str.start, len);
114 buf[len] = 0;
115 char *endptr;
116 long long r = strtoll(buf, &endptr, base);
117 if (rest)
118 *rest = bstr_cut(str, endptr - buf);
119 return r;
122 struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str)
124 if (str.len == 0)
125 return NULL;
126 int count = 0;
127 for (int i = 0; i < str.len; i++)
128 if (str.start[i] == '\n')
129 count++;
130 if (str.start[str.len - 1] != '\n')
131 count++;
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++) {
135 r[i].start = p;
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;
141 return r;