summaryrefslogtreecommitdiffstats
path: root/core/mp_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/mp_common.c')
-rw-r--r--core/mp_common.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/core/mp_common.c b/core/mp_common.c
index 611cb22bcf..03e3e988fd 100644
--- a/core/mp_common.c
+++ b/core/mp_common.c
@@ -19,6 +19,7 @@
#include <libavutil/common.h>
#include "talloc.h"
+#include "core/bstr.h"
#include "core/mp_common.h"
char *mp_format_time(double time, bool fractions)
@@ -64,3 +65,61 @@ bool mp_rect_intersection(struct mp_rect *rc, const struct mp_rect *rc2)
return rc->x1 > rc->x0 && rc->y1 > rc->y0;
}
+
+// Encode the unicode codepoint as UTF-8, and append to the end of the
+// talloc'ed buffer.
+char *mp_append_utf8_buffer(char *buffer, uint32_t codepoint)
+{
+ char data[8];
+ uint8_t tmp;
+ char *output = data;
+ PUT_UTF8(codepoint, tmp, *output++ = tmp;);
+ return talloc_strndup_append_buffer(buffer, data, output - data);
+}
+
+// Parse a C-style escape beginning at code, and append the result to *str
+// using talloc. The input string (*code) must point to the first character
+// after the initial '\', and after parsing *code is set to the first character
+// after the current escape.
+// On error, false is returned, and all input remains unchanged.
+bool mp_parse_escape(bstr *code, char **str)
+{
+ if (code->len < 1)
+ return false;
+ char replace = 0;
+ switch (code->start[0]) {
+ case '"': replace = '"'; break;
+ case '\\': replace = '\\'; break;
+ case 'b': replace = '\b'; break;
+ case 'f': replace = '\f'; break;
+ case 'n': replace = '\n'; break;
+ case 'r': replace = '\r'; break;
+ case 't': replace = '\t'; break;
+ case 'e': replace = '\x1b'; break;
+ case '\'': replace = '\''; break;
+ }
+ if (replace) {
+ *str = talloc_strndup_append_buffer(*str, &replace, 1);
+ *code = bstr_cut(*code, 1);
+ return true;
+ }
+ if (code->start[0] == 'x' && code->len >= 3) {
+ bstr num = bstr_splice(*code, 1, 3);
+ char c = bstrtoll(num, &num, 16);
+ if (!num.len)
+ return false;
+ *str = talloc_strndup_append_buffer(*str, &c, 1);
+ *code = bstr_cut(*code, 3);
+ return true;
+ }
+ if (code->start[0] == 'u' && code->len >= 5) {
+ bstr num = bstr_splice(*code, 1, 5);
+ int c = bstrtoll(num, &num, 16);
+ if (num.len)
+ return false;
+ *str = mp_append_utf8_buffer(*str, c);
+ *code = bstr_cut(*code, 5);
+ return true;
+ }
+ return false;
+}