diff mbox

[v3,for-2.5,06/12] qjson: Inline token_is_keyword() and simplify

Message ID 1448486613-17634-7-git-send-email-armbru@redhat.com
State New
Headers show

Commit Message

Markus Armbruster Nov. 25, 2015, 9:23 p.m. UTC
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 qobject/json-parser.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

Comments

Eric Blake Nov. 25, 2015, 10:09 p.m. UTC | #1
On 11/25/2015 02:23 PM, Markus Armbruster wrote:
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  qobject/json-parser.c | 20 +++++++-------------
>  1 file changed, 7 insertions(+), 13 deletions(-)
> 

>  
> -    if (token_is_keyword(token, "true")) {
> +    val = token_get_value(token);
> +
> +    if (!strcmp(val, "true")) {
>          ret = QOBJECT(qbool_from_bool(true));
> -    } else if (token_is_keyword(token, "false")) {
> +    } else if (!strcmp(val, "false")) {
>          ret = QOBJECT(qbool_from_bool(false));
> -    } else if (token_is_keyword(token, "null")) {
> +    } else if (!strcmp(val, "null")) {
>          ret = qnull();
>      } else {
> -        parse_error(ctxt, token, "invalid keyword `%s'", token_get_value(token));
> +        parse_error(ctxt, token, "invalid keyword '%s'", val);

Yay - fewer `' in error messages.  (Great back in the day when fonts
rendered them symmetrically, and still useful in m4; but lousy for
pasting into shell code and in modern fonts)

Reviewed-by: Eric Blake <eblake@redhat.com>
Markus Armbruster Nov. 26, 2015, 8:26 a.m. UTC | #2
Eric Blake <eblake@redhat.com> writes:

> On 11/25/2015 02:23 PM, Markus Armbruster wrote:
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>>  qobject/json-parser.c | 20 +++++++-------------
>>  1 file changed, 7 insertions(+), 13 deletions(-)
>> 
>
>>  
>> -    if (token_is_keyword(token, "true")) {
>> +    val = token_get_value(token);
>> +
>> +    if (!strcmp(val, "true")) {
>>          ret = QOBJECT(qbool_from_bool(true));
>> -    } else if (token_is_keyword(token, "false")) {
>> +    } else if (!strcmp(val, "false")) {
>>          ret = QOBJECT(qbool_from_bool(false));
>> -    } else if (token_is_keyword(token, "null")) {
>> +    } else if (!strcmp(val, "null")) {
>>          ret = qnull();
>>      } else {
>> -        parse_error(ctxt, token, "invalid keyword `%s'", token_get_value(token));
>> +        parse_error(ctxt, token, "invalid keyword '%s'", val);
>
> Yay - fewer `' in error messages.  (Great back in the day when fonts
> rendered them symmetrically, and still useful in m4; but lousy for
> pasting into shell code and in modern fonts)

Perhaps we can some day use the proper U+2018 (LEFT SINGLE QUOTATION
MARK) and U+2019 (RIGHT SINGLE QUOTATION MARK).

> Reviewed-by: Eric Blake <eblake@redhat.com>

Thanks!
diff mbox

Patch

diff --git a/qobject/json-parser.c b/qobject/json-parser.c
index 020c6e1..df76cc3 100644
--- a/qobject/json-parser.c
+++ b/qobject/json-parser.c
@@ -63,15 +63,6 @@  static JSONTokenType token_get_type(QObject *obj)
     return qdict_get_int(qobject_to_qdict(obj), "type");
 }
 
-static int token_is_keyword(QObject *obj, const char *value)
-{
-    if (token_get_type(obj) != JSON_KEYWORD) {
-        return 0;
-    }
-
-    return strcmp(token_get_value(obj), value) == 0;
-}
-
 static int token_is_escape(QObject *obj, const char *value)
 {
     if (token_get_type(obj) != JSON_ESCAPE) {
@@ -533,6 +524,7 @@  static QObject *parse_keyword(JSONParserContext *ctxt)
 {
     QObject *token, *ret;
     JSONParserContext saved_ctxt = parser_context_save(ctxt);
+    const char *val;
 
     token = parser_context_pop_token(ctxt);
     if (token == NULL) {
@@ -543,14 +535,16 @@  static QObject *parse_keyword(JSONParserContext *ctxt)
         goto out;
     }
 
-    if (token_is_keyword(token, "true")) {
+    val = token_get_value(token);
+
+    if (!strcmp(val, "true")) {
         ret = QOBJECT(qbool_from_bool(true));
-    } else if (token_is_keyword(token, "false")) {
+    } else if (!strcmp(val, "false")) {
         ret = QOBJECT(qbool_from_bool(false));
-    } else if (token_is_keyword(token, "null")) {
+    } else if (!strcmp(val, "null")) {
         ret = qnull();
     } else {
-        parse_error(ctxt, token, "invalid keyword `%s'", token_get_value(token));
+        parse_error(ctxt, token, "invalid keyword '%s'", val);
         goto out;
     }