diff mbox

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

Message ID 1448486613-17634-8-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 | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)

Comments

Eric Blake Nov. 25, 2015, 10:14 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 | 32 +++++++++++++++-----------------
>  1 file changed, 15 insertions(+), 17 deletions(-)
> 

> +    if (!strcmp(val, "%p")) {
>          obj = va_arg(*ap, QObject *);
> -    } else if (token_is_escape(token, "%i")) {
> +    } else if (!strcmp(val, "%i")) {
>          obj = QOBJECT(qbool_from_bool(va_arg(*ap, int)));
> -    } else if (token_is_escape(token, "%d")) {
> +    } else if (!strcmp(val, "%d")) {
>          obj = QOBJECT(qint_from_int(va_arg(*ap, int)));
> -    } else if (token_is_escape(token, "%ld")) {
> +    } else if (!strcmp(val, "%ld")) {

Not for this patch, but I'd love to kill our support for "%ld" - it has
behavior that differs between 32-bit and 64-bit platforms, and is
therefore useless for our goal of using fixed-width integer types.

>          obj = QOBJECT(qint_from_int(va_arg(*ap, long)));
> -    } else if (token_is_escape(token, "%lld") ||
> -               token_is_escape(token, "%I64d")) {
> +    } else if (!strcmp(val, "%lld") ||
> +               !strcmp(val, "%I64d")) {

Not for this patch, but I'd love to kill our support for "%I64d". Isn't
modern mingw friendlier to using POSIX escape sequences in printf()?
I'm assuming mingw is the only reason we have this hold-out?

Reviewed-by: Eric Blake <eblake@redhat.com>
Markus Armbruster Nov. 26, 2015, 8:34 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 | 32 +++++++++++++++-----------------
>>  1 file changed, 15 insertions(+), 17 deletions(-)
>> 
>
>> +    if (!strcmp(val, "%p")) {
>>          obj = va_arg(*ap, QObject *);
>> -    } else if (token_is_escape(token, "%i")) {
>> +    } else if (!strcmp(val, "%i")) {
>>          obj = QOBJECT(qbool_from_bool(va_arg(*ap, int)));
>> -    } else if (token_is_escape(token, "%d")) {
>> +    } else if (!strcmp(val, "%d")) {
>>          obj = QOBJECT(qint_from_int(va_arg(*ap, int)));
>> -    } else if (token_is_escape(token, "%ld")) {
>> +    } else if (!strcmp(val, "%ld")) {
>
> Not for this patch, but I'd love to kill our support for "%ld" - it has
> behavior that differs between 32-bit and 64-bit platforms, and is
> therefore useless for our goal of using fixed-width integer types.
>
>>          obj = QOBJECT(qint_from_int(va_arg(*ap, long)));
>> -    } else if (token_is_escape(token, "%lld") ||
>> -               token_is_escape(token, "%I64d")) {
>> +    } else if (!strcmp(val, "%lld") ||
>> +               !strcmp(val, "%I64d")) {
>
> Not for this patch, but I'd love to kill our support for "%I64d". Isn't
> modern mingw friendlier to using POSIX escape sequences in printf()?
> I'm assuming mingw is the only reason we have this hold-out?

These escapes are chosen so we can get type checking from gcc via format
attribute.  The code here is somewhat unclean; it should arguably
compare to "%" PRId64, whatever that may be.  Instead, we accept
anything we anticipate it could be.

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

Thanks!
diff mbox

Patch

diff --git a/qobject/json-parser.c b/qobject/json-parser.c
index df76cc3..b57cac7 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_escape(QObject *obj, const char *value)
-{
-    if (token_get_type(obj) != JSON_ESCAPE) {
-        return 0;
-    }
-
-    return (strcmp(token_get_value(obj), value) == 0);
-}
-
 /**
  * Error handler
  */
@@ -560,6 +551,7 @@  static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
 {
     QObject *token = NULL, *obj;
     JSONParserContext saved_ctxt = parser_context_save(ctxt);
+    const char *val;
 
     if (ap == NULL) {
         goto out;
@@ -570,20 +562,26 @@  static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
         goto out;
     }
 
-    if (token_is_escape(token, "%p")) {
+    if (token_get_type(token) != JSON_ESCAPE) {
+        goto out;
+    }
+
+    val = token_get_value(token);
+
+    if (!strcmp(val, "%p")) {
         obj = va_arg(*ap, QObject *);
-    } else if (token_is_escape(token, "%i")) {
+    } else if (!strcmp(val, "%i")) {
         obj = QOBJECT(qbool_from_bool(va_arg(*ap, int)));
-    } else if (token_is_escape(token, "%d")) {
+    } else if (!strcmp(val, "%d")) {
         obj = QOBJECT(qint_from_int(va_arg(*ap, int)));
-    } else if (token_is_escape(token, "%ld")) {
+    } else if (!strcmp(val, "%ld")) {
         obj = QOBJECT(qint_from_int(va_arg(*ap, long)));
-    } else if (token_is_escape(token, "%lld") ||
-               token_is_escape(token, "%I64d")) {
+    } else if (!strcmp(val, "%lld") ||
+               !strcmp(val, "%I64d")) {
         obj = QOBJECT(qint_from_int(va_arg(*ap, long long)));
-    } else if (token_is_escape(token, "%s")) {
+    } else if (!strcmp(val, "%s")) {
         obj = QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
-    } else if (token_is_escape(token, "%f")) {
+    } else if (!strcmp(val, "%f")) {
         obj = QOBJECT(qfloat_from_double(va_arg(*ap, double)));
     } else {
         goto out;