2010-05-20 20:50:18 +00:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <libavutil/avutil.h>
|
2011-02-23 14:18:09 +00:00
|
|
|
#include <assert.h>
|
2011-04-19 03:53:56 +00:00
|
|
|
#include <ctype.h>
|
2011-07-28 08:07:47 +00:00
|
|
|
#include <stdarg.h>
|
2011-02-23 14:18:09 +00:00
|
|
|
|
|
|
|
#include "talloc.h"
|
|
|
|
|
2010-05-20 20:50:18 +00:00
|
|
|
#include "bstr.h"
|
|
|
|
|
|
|
|
int bstrcmp(struct bstr str1, struct bstr str2)
|
|
|
|
{
|
|
|
|
int ret = memcmp(str1.start, str2.start, FFMIN(str1.len, str2.len));
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
if (str1.len == str2.len)
|
|
|
|
return 0;
|
|
|
|
else if (str1.len > str2.len)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bstrcasecmp(struct bstr str1, struct bstr str2)
|
|
|
|
{
|
|
|
|
int ret = strncasecmp(str1.start, str2.start, FFMIN(str1.len, str2.len));
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
if (str1.len == str2.len)
|
|
|
|
return 0;
|
|
|
|
else if (str1.len > str2.len)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2011-02-23 14:18:09 +00:00
|
|
|
|
|
|
|
int bstrchr(struct bstr str, int c)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < str.len; i++)
|
|
|
|
if (str.start[i] == c)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-04-09 02:14:55 +00:00
|
|
|
int bstrrchr(struct bstr str, int c)
|
|
|
|
{
|
|
|
|
for (int i = str.len - 1; i >= 0; i--)
|
|
|
|
if (str.start[i] == c)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-28 08:07:47 +00:00
|
|
|
int bstrcspn(struct bstr str, const char *reject)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < str.len; i++)
|
|
|
|
if (strchr(reject, str.start[i]))
|
|
|
|
break;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2011-04-19 03:53:56 +00:00
|
|
|
int bstr_find(struct bstr haystack, struct bstr needle)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < haystack.len; i++)
|
|
|
|
if (bstr_startswith(bstr_splice(haystack, i, haystack.len), needle))
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-07-20 01:41:51 +00:00
|
|
|
struct bstr bstr_lstrip(struct bstr str)
|
2011-02-23 14:18:09 +00:00
|
|
|
{
|
|
|
|
while (str.len && isspace(*str.start)) {
|
|
|
|
str.start++;
|
|
|
|
str.len--;
|
|
|
|
}
|
2011-07-20 01:41:51 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct bstr bstr_strip(struct bstr str)
|
|
|
|
{
|
|
|
|
str = bstr_lstrip(str);
|
2011-02-23 14:18:09 +00:00
|
|
|
while (str.len && isspace(str.start[str.len - 1]))
|
|
|
|
str.len--;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-07-28 08:07:47 +00:00
|
|
|
struct bstr bstr_split(struct bstr str, const char *sep, struct bstr *rest)
|
2011-02-23 14:18:09 +00:00
|
|
|
{
|
2011-07-28 08:07:47 +00:00
|
|
|
int start;
|
2011-02-23 14:18:09 +00:00
|
|
|
for (start = 0; start < str.len; start++)
|
|
|
|
if (!strchr(sep, str.start[start]))
|
|
|
|
break;
|
2011-07-28 08:07:47 +00:00
|
|
|
str = bstr_cut(str, start);
|
|
|
|
int end = bstrcspn(str, sep);
|
2011-02-23 14:18:09 +00:00
|
|
|
if (rest) {
|
|
|
|
*rest = bstr_cut(str, end);
|
|
|
|
}
|
2011-07-28 08:07:47 +00:00
|
|
|
return bstr_splice(str, 0, end);
|
2011-02-23 14:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct bstr bstr_splice(struct bstr str, int start, int end)
|
|
|
|
{
|
|
|
|
if (start < 0)
|
|
|
|
start += str.len;
|
|
|
|
if (end < 0)
|
|
|
|
end += str.len;
|
|
|
|
end = FFMIN(end, str.len);
|
|
|
|
start = FFMAX(start, 0);
|
2011-07-31 17:25:22 +00:00
|
|
|
end = FFMAX(end, start);
|
2011-02-23 14:18:09 +00:00
|
|
|
str.start += start;
|
|
|
|
str.len = end - start;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long bstrtoll(struct bstr str, struct bstr *rest, int base)
|
|
|
|
{
|
2011-07-28 08:07:47 +00:00
|
|
|
str = bstr_lstrip(str);
|
2011-02-23 14:18:09 +00:00
|
|
|
char buf[51];
|
|
|
|
int len = FFMIN(str.len, 50);
|
|
|
|
memcpy(buf, str.start, len);
|
|
|
|
buf[len] = 0;
|
|
|
|
char *endptr;
|
|
|
|
long long r = strtoll(buf, &endptr, base);
|
|
|
|
if (rest)
|
|
|
|
*rest = bstr_cut(str, endptr - buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-07-28 08:07:47 +00:00
|
|
|
double bstrtod(struct bstr str, struct bstr *rest)
|
|
|
|
{
|
|
|
|
str = bstr_lstrip(str);
|
|
|
|
char buf[101];
|
|
|
|
int len = FFMIN(str.len, 100);
|
|
|
|
memcpy(buf, str.start, len);
|
|
|
|
buf[len] = 0;
|
|
|
|
char *endptr;
|
|
|
|
double r = strtod(buf, &endptr);
|
|
|
|
if (rest)
|
|
|
|
*rest = bstr_cut(str, endptr - buf);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2011-02-23 14:18:09 +00:00
|
|
|
struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str)
|
|
|
|
{
|
|
|
|
if (str.len == 0)
|
|
|
|
return NULL;
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < str.len; i++)
|
|
|
|
if (str.start[i] == '\n')
|
|
|
|
count++;
|
|
|
|
if (str.start[str.len - 1] != '\n')
|
|
|
|
count++;
|
|
|
|
struct bstr *r = talloc_array_ptrtype(talloc_ctx, r, count);
|
|
|
|
unsigned char *p = str.start;
|
|
|
|
for (int i = 0; i < count - 1; i++) {
|
|
|
|
r[i].start = p;
|
|
|
|
while (*p++ != '\n');
|
|
|
|
r[i].len = p - r[i].start;
|
|
|
|
}
|
|
|
|
r[count - 1].start = p;
|
|
|
|
r[count - 1].len = str.start + str.len - p;
|
|
|
|
return r;
|
|
|
|
}
|
2011-04-19 03:53:56 +00:00
|
|
|
|
|
|
|
void bstr_lower(struct bstr str)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < str.len; i++)
|
|
|
|
str.start[i] = tolower(str.start[i]);
|
|
|
|
}
|
2011-07-28 08:07:47 +00:00
|
|
|
|
|
|
|
int bstr_sscanf(struct bstr str, const char *format, ...)
|
|
|
|
{
|
|
|
|
char *ptr = bstrdup0(NULL, str);
|
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
|
|
|
int ret = vsscanf(ptr, format, va);
|
|
|
|
va_end(va);
|
|
|
|
talloc_free(ptr);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-01-13 06:38:40 +00:00
|
|
|
|
|
|
|
int bstr_parse_utf8_code_length(unsigned char b)
|
|
|
|
{
|
|
|
|
if (b < 128)
|
|
|
|
return 1;
|
|
|
|
int bytes = 7 - av_log2(b ^ 255);
|
|
|
|
return (bytes >= 2 && bytes <= 4) ? bytes : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bstr_decode_utf8(struct bstr s, struct bstr *out_next)
|
|
|
|
{
|
|
|
|
if (s.len == 0)
|
|
|
|
return -1;
|
|
|
|
unsigned int codepoint = s.start[0];
|
|
|
|
s.start++; s.len--;
|
|
|
|
if (codepoint >= 128) {
|
|
|
|
int bytes = bstr_parse_utf8_code_length(codepoint);
|
|
|
|
if (bytes < 0 || s.len < bytes - 1)
|
|
|
|
return -1;
|
|
|
|
codepoint &= 127 >> bytes;
|
|
|
|
for (int n = 1; n < bytes; n++) {
|
|
|
|
int tmp = s.start[0];
|
|
|
|
if ((tmp & 0xC0) != 0x80)
|
|
|
|
return -1;
|
|
|
|
codepoint = (codepoint << 6) | (tmp & ~0xC0);
|
|
|
|
s.start++; s.len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out_next)
|
|
|
|
*out_next = s;
|
|
|
|
return codepoint;
|
|
|
|
}
|