summaryrefslogtreecommitdiffstats
path: root/input
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2013-12-30 20:28:32 +0100
committerwm4 <wm4@nowhere>2013-12-30 22:49:50 +0100
commit066ecfcbfb0b7120183338c5382e98c609a9d89a (patch)
treec03269c234b3b7edcc3bf19374134e341628dc42 /input
parent097fe8ea6fb25df68077c25c08f29fb57a9d2bd6 (diff)
downloadmpv-066ecfcbfb0b7120183338c5382e98c609a9d89a.tar.bz2
mpv-066ecfcbfb0b7120183338c5382e98c609a9d89a.tar.xz
common: simplify and optimize string escape parsing
This code is shared between input.conf parser and option parser. Until now, the performance didn't really matter. But I want to use this code for JSON parsing too, and since JSON will have to be parsed a lot, it should probably try to avoid realloc'ing too much. This commit moves parsing of C-style escaped strings into a common function, and allows using it in a way realloc can be completely avoided, if the already allocated buffer is large enough.
Diffstat (limited to 'input')
-rw-r--r--input/cmd_parse.c27
1 files changed, 1 insertions, 26 deletions
diff --git a/input/cmd_parse.c b/input/cmd_parse.c
index c9a70035fc..2369ff5a8e 100644
--- a/input/cmd_parse.c
+++ b/input/cmd_parse.c
@@ -41,31 +41,6 @@ static bool read_token(bstr str, bstr *out_rest, bstr *out_token)
return true;
}
-static bool read_escaped_string(void *talloc_ctx, bstr *str, bstr *literal)
-{
- bstr t = *str;
- char *new = talloc_strdup(talloc_ctx, "");
- while (t.len) {
- if (t.start[0] == '"')
- break;
- if (t.start[0] == '\\') {
- t = bstr_cut(t, 1);
- if (!mp_parse_escape(&t, &new))
- goto error;
- } else {
- new = talloc_strndup_append_buffer(new, t.start, 1);
- t = bstr_cut(t, 1);
- }
- }
- int len = str->len - t.len;
- *literal = new ? bstr0(new) : bstr_splice(*str, 0, len);
- *str = bstr_cut(*str, len);
- return true;
-error:
- talloc_free(new);
- return false;
-}
-
// Somewhat awkward; the main purpose is supporting both strings and
// pre-split string arrays as input.
struct parse_ctx {
@@ -92,7 +67,7 @@ static int pctx_read_token(struct parse_ctx *ctx, bstr *out)
ctx->str = bstr_lstrip(ctx->str);
bstr start = ctx->str;
if (bstr_eatstart0(&ctx->str, "\"")) {
- if (!read_escaped_string(ctx->tmp, &ctx->str, out)) {
+ if (!mp_append_escaped_string_noalloc(ctx->tmp, out, &ctx->str)) {
MP_ERR(ctx, "Broken string escapes: ...>%.*s<.\n", BSTR_P(start));
return -1;
}