mirror of
https://github.com/mpv-player/mpv
synced 2025-04-09 03:02:13 +00:00
bstr: use mp_append_utf8_bstr()
Drop usage of the ugly PUT_UTF8() macro.
This commit is contained in:
parent
b0efd3f36a
commit
7639e05ea8
23
bstr/bstr.c
23
bstr/bstr.c
@ -25,6 +25,7 @@
|
|||||||
|
|
||||||
#include "talloc.h"
|
#include "talloc.h"
|
||||||
|
|
||||||
|
#include "common/common.h"
|
||||||
#include "bstr/bstr.h"
|
#include "bstr/bstr.h"
|
||||||
|
|
||||||
int bstrcmp(struct bstr str1, struct bstr str2)
|
int bstrcmp(struct bstr str1, struct bstr str2)
|
||||||
@ -318,13 +319,6 @@ int bstr_validate_utf8(struct bstr s)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void append_bstr(bstr *buf, bstr s)
|
|
||||||
{
|
|
||||||
buf->start = talloc_realloc(NULL, buf->start, unsigned char, buf->len + s.len);
|
|
||||||
memcpy(buf->start + buf->len, s.start, s.len);
|
|
||||||
buf->len += s.len;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct bstr bstr_sanitize_utf8_latin1(void *talloc_ctx, struct bstr s)
|
struct bstr bstr_sanitize_utf8_latin1(void *talloc_ctx, struct bstr s)
|
||||||
{
|
{
|
||||||
bstr new = {0};
|
bstr new = {0};
|
||||||
@ -333,13 +327,8 @@ struct bstr bstr_sanitize_utf8_latin1(void *talloc_ctx, struct bstr s)
|
|||||||
while (left.len) {
|
while (left.len) {
|
||||||
int r = bstr_decode_utf8(left, &left);
|
int r = bstr_decode_utf8(left, &left);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
append_bstr(&new, (bstr){first_ok, left.start - first_ok});
|
bstr_xappend(talloc_ctx, &new, (bstr){first_ok, left.start - first_ok});
|
||||||
uint32_t codepoint = (unsigned char)left.start[0];
|
mp_append_utf8_bstr(talloc_ctx, &new, (unsigned char)left.start[0]);
|
||||||
char data[8];
|
|
||||||
uint8_t tmp;
|
|
||||||
char *output = data;
|
|
||||||
PUT_UTF8(codepoint, tmp, *output++ = tmp;);
|
|
||||||
append_bstr(&new, (bstr){data, output - data});
|
|
||||||
left.start += 1;
|
left.start += 1;
|
||||||
left.len -= 1;
|
left.len -= 1;
|
||||||
first_ok = left.start;
|
first_ok = left.start;
|
||||||
@ -348,11 +337,7 @@ struct bstr bstr_sanitize_utf8_latin1(void *talloc_ctx, struct bstr s)
|
|||||||
if (!new.start)
|
if (!new.start)
|
||||||
return s;
|
return s;
|
||||||
if (first_ok != left.start)
|
if (first_ok != left.start)
|
||||||
append_bstr(&new, (bstr){first_ok, left.start - first_ok});
|
bstr_xappend(talloc_ctx, &new, (bstr){first_ok, left.start - first_ok});
|
||||||
// For convenience
|
|
||||||
append_bstr(&new, (bstr){"\0", 1});
|
|
||||||
new.len -= 1;
|
|
||||||
talloc_steal(talloc_ctx, new.start);
|
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user