mirror of https://github.com/mpv-player/mpv
bstr: add some utility functions
bstr_strip_ext and bstr_get_ext were taken from find_subfiles.c. bstr_cut is extended to work like bstr_splice: passing a negative argument will start counting from the end of the string, e.g. bstr_cut("abc", -2) == "bc"
This commit is contained in:
parent
bba96eca42
commit
f88674509e
41
bstr.c
41
bstr.c
|
@ -185,6 +185,19 @@ struct bstr *bstr_splitlines(void *talloc_ctx, struct bstr str)
|
|||
return r;
|
||||
}
|
||||
|
||||
struct bstr bstr_getline(struct bstr str, struct bstr *rest)
|
||||
{
|
||||
int pos = bstrchr(str, '\n');
|
||||
if (pos < 0)
|
||||
pos = str.len;
|
||||
if (rest)
|
||||
*rest = bstr_cut(str, pos + 1);
|
||||
str.len = pos;
|
||||
if (str.len > 0 && str.start[str.len - 1] == '\r')
|
||||
str.len -= 1;
|
||||
return str;
|
||||
}
|
||||
|
||||
void bstr_lower(struct bstr str)
|
||||
{
|
||||
for (int i = 0; i < str.len; i++)
|
||||
|
@ -233,3 +246,31 @@ int bstr_decode_utf8(struct bstr s, struct bstr *out_next)
|
|||
*out_next = s;
|
||||
return codepoint;
|
||||
}
|
||||
|
||||
bool bstr_case_startswith(struct bstr s, struct bstr prefix)
|
||||
{
|
||||
struct bstr start = bstr_splice(s, 0, prefix.len);
|
||||
return start.len == prefix.len && bstrcasecmp(start, prefix) == 0;
|
||||
}
|
||||
|
||||
bool bstr_case_endswith(struct bstr s, struct bstr suffix)
|
||||
{
|
||||
struct bstr end = bstr_cut(s, -suffix.len);
|
||||
return end.len == suffix.len && bstrcasecmp(end, suffix) == 0;
|
||||
}
|
||||
|
||||
struct bstr bstr_strip_ext(struct bstr str)
|
||||
{
|
||||
int dotpos = bstrrchr(str, '.');
|
||||
if (dotpos < 0)
|
||||
return str;
|
||||
return (struct bstr){str.start, dotpos};
|
||||
}
|
||||
|
||||
struct bstr bstr_get_ext(struct bstr s)
|
||||
{
|
||||
int dotpos = bstrrchr(s, '.');
|
||||
if (dotpos < 0)
|
||||
return (struct bstr){NULL, 0};
|
||||
return bstr_splice(s, dotpos + 1, s.len);
|
||||
}
|
||||
|
|
17
bstr.h
17
bstr.h
|
@ -82,8 +82,25 @@ int bstr_decode_utf8(struct bstr str, struct bstr *out_next);
|
|||
// 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);
|
||||
|
||||
// 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.)
|
||||
// Unlike bstr_splitlines, possible \r characters coming from files with CR+LF
|
||||
// line breaks are stripped.
|
||||
struct bstr bstr_getline(struct bstr str, struct bstr *rest);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
static inline struct bstr bstr_cut(struct bstr str, int n)
|
||||
{
|
||||
if (n < 0) {
|
||||
n += str.len;
|
||||
if (n < 0)
|
||||
n = 0;
|
||||
}
|
||||
if (n > str.len)
|
||||
n = str.len;
|
||||
return (struct bstr){str.start + n, str.len - n};
|
||||
|
|
Loading…
Reference in New Issue