From 066ecfcbfb0b7120183338c5382e98c609a9d89a Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 30 Dec 2013 20:28:32 +0100 Subject: 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. --- options/m_option.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'options/m_option.c') diff --git a/options/m_option.c b/options/m_option.c index 9f98008c64..b6cfbdf0d0 100644 --- a/options/m_option.c +++ b/options/m_option.c @@ -746,20 +746,17 @@ const m_option_type_t m_option_type_float = { static char *unescape_string(void *talloc_ctx, bstr str) { - char *res = talloc_strdup(talloc_ctx, ""); + bstr dst = {0}; while (str.len) { - bstr rest; - bool esc = bstr_split_tok(str, "\\", &str, &rest); - res = talloc_strndup_append_buffer(res, str.start, str.len); - if (esc) { - if (!mp_parse_escape(&rest, &res)) { - talloc_free(res); - return NULL; - } + if (!mp_append_escaped_string(talloc_ctx, &dst, &str)) { + talloc_free(dst.start); + return NULL; } - str = rest; + if (!bstr_eatstart0(&str, "\"")) + break; + bstr_xappend(talloc_ctx, &dst, bstr0("\"")); } - return res; + return dst.start; } static char *escape_string(char *str0) -- cgit v1.2.3