提交 8bca4613 编写于 作者: M Markus Armbruster

json: Support %% in JSON strings when interpolating

The previous commit makes JSON strings containing '%' awkward to
express in templates: you'd have to mask the '%' with an Unicode
escape \u0025.  No template currently contains such JSON strings.
Support the printf conversion specification %% in JSON strings as a
convenience anyway, because it's trivially easy to do.
Signed-off-by: NMarkus Armbruster <armbru@redhat.com>
Reviewed-by: NEric Blake <eblake@redhat.com>
Message-Id: <20180823164025.12553-58-armbru@redhat.com>
上级 16a48599
...@@ -208,10 +208,11 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token) ...@@ -208,10 +208,11 @@ static QString *parse_string(JSONParserContext *ctxt, JSONToken *token)
} }
break; break;
case '%': case '%':
if (ctxt->ap) { if (ctxt->ap && ptr[1] != '%') {
parse_error(ctxt, token, "can't interpolate into string"); parse_error(ctxt, token, "can't interpolate into string");
goto out; goto out;
} }
ptr++;
/* fall through */ /* fall through */
default: default:
cp = mod_utf8_codepoint(ptr, 6, &end); cp = mod_utf8_codepoint(ptr, 6, &end);
......
...@@ -1270,7 +1270,7 @@ static void simple_interpolation(void) ...@@ -1270,7 +1270,7 @@ static void simple_interpolation(void)
QObject *obj; QObject *obj;
QLitObject decoded = QLIT_QLIST(((QLitObject[]){ QLitObject decoded = QLIT_QLIST(((QLitObject[]){
QLIT_QNUM(1), QLIT_QNUM(1),
QLIT_QNUM(2), QLIT_QSTR("100%"),
QLIT_QLIST(((QLitObject[]){ QLIT_QLIST(((QLitObject[]){
QLIT_QNUM(32), QLIT_QNUM(32),
QLIT_QNUM(42), QLIT_QNUM(42),
...@@ -1280,7 +1280,7 @@ static void simple_interpolation(void) ...@@ -1280,7 +1280,7 @@ static void simple_interpolation(void)
embedded_obj = qobject_from_json("[32, 42]", &error_abort); embedded_obj = qobject_from_json("[32, 42]", &error_abort);
g_assert(embedded_obj != NULL); g_assert(embedded_obj != NULL);
obj = qobject_from_jsonf_nofail("[%d, 2, %p]", 1, embedded_obj); obj = qobject_from_jsonf_nofail("[%d, '100%%', %p]", 1, embedded_obj);
g_assert(qlit_equal_qobject(&decoded, obj)); g_assert(qlit_equal_qobject(&decoded, obj));
qobject_unref(obj); qobject_unref(obj);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册