2010-05-20 22:50:18 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MPLAYER_BSTR_H
|
|
|
|
#define MPLAYER_BSTR_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
2011-02-23 15:18:09 +01:00
|
|
|
#include <stdbool.h>
|
2013-12-30 20:20:28 +01:00
|
|
|
#include <stdarg.h>
|
2010-05-20 22:50:18 +02:00
|
|
|
|
2011-02-23 15:18:09 +01:00
|
|
|
#include "talloc.h"
|
2013-12-30 20:20:28 +01:00
|
|
|
#include "compat/compiler.h"
|
2011-02-23 15:18:09 +01:00
|
|
|
|
|
|
|
/* NOTE: 'len' is size_t, but most string-handling functions below assume
|
|
|
|
* that input size has been sanity checked and len fits in an int.
|
|
|
|
*/
|
2012-07-28 23:47:42 +02:00
|
|
|
typedef struct bstr {
|
2011-02-23 15:18:09 +01:00
|
|
|
unsigned char *start;
|
2010-05-20 22:50:18 +02:00
|
|
|
size_t len;
|
2012-07-28 23:47:42 +02:00
|
|
|
} bstr;
|
2010-05-20 22:50:18 +02:00
|
|
|
|
2012-06-29 04:14:26 +02:00
|
|
|
// If str.start is NULL, return NULL.
|
2011-07-28 10:07:47 +02:00
|
|
|
static inline char *bstrdup0(void *talloc_ctx, struct bstr str)
|
|
|
|
{
|
|
|
|
return talloc_strndup(talloc_ctx, (char *)str.start, str.len);
|
|
|
|
}
|
|
|
|
|
2013-04-21 03:44:56 +02:00
|
|
|
// Like bstrdup0(), but always return a valid C-string.
|
|
|
|
static inline char *bstrto0(void *talloc_ctx, struct bstr str)
|
|
|
|
{
|
|
|
|
return str.start ? bstrdup0(talloc_ctx, str) : talloc_strdup(talloc_ctx, "");
|
|
|
|
}
|
|
|
|
|
2012-06-29 04:14:26 +02:00
|
|
|
// Return start = NULL iff that is true for the original.
|
2011-07-28 10:07:47 +02:00
|
|
|
static inline struct bstr bstrdup(void *talloc_ctx, struct bstr str)
|
|
|
|
{
|
2012-06-29 04:14:26 +02:00
|
|
|
struct bstr r = { NULL, str.len };
|
|
|
|
if (str.start)
|
2012-08-03 03:23:44 +02:00
|
|
|
r.start = (unsigned char *)talloc_memdup(talloc_ctx, str.start, str.len);
|
2011-07-28 10:07:47 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-08-03 03:23:44 +02:00
|
|
|
static inline struct bstr bstr0(const char *s)
|
2011-07-28 10:07:47 +02:00
|
|
|
{
|
|
|
|
return (struct bstr){(unsigned char *)s, s ? strlen(s) : 0};
|
|
|
|
}
|
|
|
|
|
2010-05-20 22:50:18 +02:00
|
|
|
int bstrcmp(struct bstr str1, struct bstr str2);
|
|
|
|
int bstrcasecmp(struct bstr str1, struct bstr str2);
|
2011-02-23 15:18:09 +01:00
|
|
|
int bstrchr(struct bstr str, int c);
|
2011-04-09 04:14:55 +02:00
|
|
|
int bstrrchr(struct bstr str, int c);
|
2013-04-21 01:03:46 +02:00
|
|
|
int bstrspn(struct bstr str, const char *accept);
|
2011-07-28 10:07:47 +02:00
|
|
|
int bstrcspn(struct bstr str, const char *reject);
|
2011-07-20 03:41:51 +02:00
|
|
|
|
2011-04-19 05:53:56 +02:00
|
|
|
int bstr_find(struct bstr haystack, struct bstr needle);
|
2011-02-23 15:18:09 +01:00
|
|
|
struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str);
|
2011-07-20 03:41:51 +02:00
|
|
|
struct bstr bstr_lstrip(struct bstr str);
|
2011-02-23 15:18:09 +01:00
|
|
|
struct bstr bstr_strip(struct bstr str);
|
2011-07-28 10:07:47 +02:00
|
|
|
struct bstr bstr_split(struct bstr str, const char *sep, struct bstr *rest);
|
2013-01-13 16:13:13 +01:00
|
|
|
bool bstr_split_tok(bstr str, const char *tok, bstr *out_left, bstr *out_right);
|
2011-02-23 15:18:09 +01:00
|
|
|
struct bstr bstr_splice(struct bstr str, int start, int end);
|
|
|
|
long long bstrtoll(struct bstr str, struct bstr *rest, int base);
|
2011-07-28 10:07:47 +02:00
|
|
|
double bstrtod(struct bstr str, struct bstr *rest);
|
2011-04-19 05:53:56 +02:00
|
|
|
void bstr_lower(struct bstr str);
|
2011-07-28 10:07:47 +02:00
|
|
|
int bstr_sscanf(struct bstr str, const char *format, ...);
|
2011-02-23 15:18:09 +01:00
|
|
|
|
2012-01-13 07:38:40 +01:00
|
|
|
// Decode the UTF-8 code point at the start of the string,, and return the
|
|
|
|
// character.
|
|
|
|
// After calling this function, *out_next will point to the next character.
|
|
|
|
// out_next can be NULL.
|
|
|
|
// On error, -1 is returned, and *out_next is not modified.
|
|
|
|
int bstr_decode_utf8(struct bstr str, struct bstr *out_next);
|
|
|
|
|
|
|
|
// Return the length of the UTF-8 sequence that starts with the given byte.
|
|
|
|
// Given a string char *s, the next UTF-8 code point is to be expected at
|
|
|
|
// s + bstr_parse_utf8_code_length(s[0])
|
|
|
|
// On error, -1 is returned. On success, it returns a value in the range [1, 4].
|
|
|
|
int bstr_parse_utf8_code_length(unsigned char b);
|
|
|
|
|
2013-08-15 18:48:05 +02:00
|
|
|
// Return >= 0 if the string is valid UTF-8, otherwise negative error code.
|
|
|
|
// Embedded \0 bytes are considered valid.
|
|
|
|
// This returns -N if the UTF-8 string was likely just cut-off in the middle of
|
|
|
|
// an UTF-8 sequence: -1 means 1 byte was missing, -5 5 bytes missing.
|
|
|
|
// If the string was likely not cut off, -8 is returned.
|
|
|
|
// Use (return_value > -8) to check whether the string is valid UTF-8 or valid
|
|
|
|
// but cut-off UTF-8.
|
|
|
|
int bstr_validate_utf8(struct bstr s);
|
|
|
|
|
|
|
|
// Force the input string to valid UTF-8. If invalid UTF-8 encoding is
|
|
|
|
// encountered, the invalid bytes are interpreted as Latin-1.
|
|
|
|
// Embedded \0 bytes are considered valid.
|
|
|
|
// If replacement happens, a newly allocated string is returned (with a \0
|
|
|
|
// byte added past its end for convenience). The string is allocated via
|
|
|
|
// talloc, with talloc_ctx as parent.
|
|
|
|
struct bstr bstr_sanitize_utf8_latin1(void *talloc_ctx, struct bstr s);
|
|
|
|
|
2012-01-01 17:28:01 +01:00
|
|
|
// Return the text before the next line break, and return it. Change *rest to
|
|
|
|
// point to the text following this line break. (rest can be NULL.)
|
2012-07-28 17:07:49 +02:00
|
|
|
// Line break characters are not stripped.
|
2012-01-01 17:28:01 +01:00
|
|
|
struct bstr bstr_getline(struct bstr str, struct bstr *rest);
|
|
|
|
|
2012-07-28 17:07:49 +02:00
|
|
|
// Strip one trailing line break. This is intended for use with bstr_getline,
|
|
|
|
// and will remove the trailing \n or \r\n sequence.
|
|
|
|
struct bstr bstr_strip_linebreaks(struct bstr str);
|
|
|
|
|
2013-12-30 20:20:28 +01:00
|
|
|
void bstr_xappend(void *talloc_ctx, bstr *s, bstr append);
|
|
|
|
void bstr_xappend_asprintf(void *talloc_ctx, bstr *s, const char *fmt, ...)
|
|
|
|
PRINTF_ATTRIBUTE(3, 4);
|
|
|
|
void bstr_xappend_vasprintf(void *talloc_ctx, bstr *s, const char *fmt, va_list va)
|
|
|
|
PRINTF_ATTRIBUTE(3, 0);
|
|
|
|
|
2012-02-09 12:43:33 +01:00
|
|
|
// If s starts with prefix, return true and return the rest of the string in s.
|
|
|
|
bool bstr_eatstart(struct bstr *s, struct bstr prefix);
|
|
|
|
|
2012-01-01 17:28:01 +01:00
|
|
|
bool bstr_case_startswith(struct bstr s, struct bstr prefix);
|
|
|
|
bool bstr_case_endswith(struct bstr s, struct bstr suffix);
|
|
|
|
struct bstr bstr_strip_ext(struct bstr str);
|
|
|
|
struct bstr bstr_get_ext(struct bstr s);
|
|
|
|
|
2011-02-23 15:18:09 +01:00
|
|
|
static inline struct bstr bstr_cut(struct bstr str, int n)
|
|
|
|
{
|
2012-01-01 17:28:01 +01:00
|
|
|
if (n < 0) {
|
|
|
|
n += str.len;
|
|
|
|
if (n < 0)
|
|
|
|
n = 0;
|
|
|
|
}
|
2012-08-03 03:23:44 +02:00
|
|
|
if (((size_t)n) > str.len)
|
2011-07-28 10:07:47 +02:00
|
|
|
n = str.len;
|
2011-02-23 15:18:09 +01:00
|
|
|
return (struct bstr){str.start + n, str.len - n};
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool bstr_startswith(struct bstr str, struct bstr prefix)
|
|
|
|
{
|
|
|
|
if (str.len < prefix.len)
|
|
|
|
return false;
|
|
|
|
return !memcmp(str.start, prefix.start, prefix.len);
|
|
|
|
}
|
|
|
|
|
2011-07-28 10:07:47 +02:00
|
|
|
static inline bool bstr_startswith0(struct bstr str, const char *prefix)
|
2011-02-23 15:18:09 +01:00
|
|
|
{
|
2012-07-28 23:47:42 +02:00
|
|
|
return bstr_startswith(str, bstr0(prefix));
|
2011-02-23 15:18:09 +01:00
|
|
|
}
|
2010-05-20 22:50:18 +02:00
|
|
|
|
2011-07-28 10:07:47 +02:00
|
|
|
static inline bool bstr_endswith(struct bstr str, struct bstr suffix)
|
2011-04-19 05:53:56 +02:00
|
|
|
{
|
2011-07-28 10:07:47 +02:00
|
|
|
if (str.len < suffix.len)
|
|
|
|
return false;
|
|
|
|
return !memcmp(str.start + str.len - suffix.len, suffix.start, suffix.len);
|
2011-04-19 05:53:56 +02:00
|
|
|
}
|
2011-04-24 02:05:24 +02:00
|
|
|
|
2011-07-28 10:07:47 +02:00
|
|
|
static inline bool bstr_endswith0(struct bstr str, const char *suffix)
|
2011-04-24 02:05:24 +02:00
|
|
|
{
|
2012-07-28 23:47:42 +02:00
|
|
|
return bstr_endswith(str, bstr0(suffix));
|
2011-07-28 10:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int bstrcmp0(struct bstr str1, const char *str2)
|
|
|
|
{
|
2012-07-28 23:47:42 +02:00
|
|
|
return bstrcmp(str1, bstr0(str2));
|
2011-07-28 10:07:47 +02:00
|
|
|
}
|
|
|
|
|
2012-09-29 18:08:47 +02:00
|
|
|
static inline bool bstr_equals(struct bstr str1, struct bstr str2)
|
|
|
|
{
|
|
|
|
return bstrcmp(str1, str2) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool bstr_equals0(struct bstr str1, const char *str2)
|
|
|
|
{
|
|
|
|
return bstrcmp(str1, bstr0(str2)) == 0;
|
|
|
|
}
|
|
|
|
|
2011-07-28 10:07:47 +02:00
|
|
|
static inline int bstrcasecmp0(struct bstr str1, const char *str2)
|
|
|
|
{
|
2012-07-28 23:47:42 +02:00
|
|
|
return bstrcasecmp(str1, bstr0(str2));
|
2011-07-28 10:07:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int bstr_find0(struct bstr haystack, const char *needle)
|
|
|
|
{
|
2012-07-28 23:47:42 +02:00
|
|
|
return bstr_find(haystack, bstr0(needle));
|
2011-04-24 02:05:24 +02:00
|
|
|
}
|
|
|
|
|
2012-09-22 14:54:57 +02:00
|
|
|
static inline int bstr_eatstart0(struct bstr *s, const char *prefix)
|
2012-07-02 01:35:57 +02:00
|
|
|
{
|
2012-07-28 23:47:42 +02:00
|
|
|
return bstr_eatstart(s, bstr0(prefix));
|
2012-07-02 01:35:57 +02:00
|
|
|
}
|
|
|
|
|
2010-05-20 22:50:18 +02:00
|
|
|
// create a pair (not single value!) for "%.*s" printf syntax
|
|
|
|
#define BSTR_P(bstr) (int)((bstr).len), (bstr).start
|
|
|
|
|
2011-02-23 15:18:09 +01:00
|
|
|
#define WHITESPACE " \f\n\r\t\v"
|
|
|
|
|
2010-05-20 22:50:18 +02:00
|
|
|
#endif /* MPLAYER_BSTR_H */
|