All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: armbru@redhat.com
Subject: [Qemu-devel] [PATCH v6 06/15] qapi: Use qstring_append_chr() where appropriate
Date: Mon, 10 Oct 2016 08:23:48 -0500	[thread overview]
Message-ID: <1476105837-9861-7-git-send-email-eblake@redhat.com> (raw)
In-Reply-To: <1476105837-9861-1-git-send-email-eblake@redhat.com>

qstring_append_chr() is more efficient than qstring_append()
when dealing with a one-byte string (including the case of a
temporary 2-byte buffer just for creating a dynamic one-byte
string).

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

---
v6: no change
[no v5 due to series split]
v4: also convert qstring_append() of one-byte strings
v3: no change
v2: no change
---
 qobject/json-parser.c | 25 ++++++++++---------------
 qobject/qjson.c       | 12 ++++++------
 2 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/qobject/json-parser.c b/qobject/json-parser.c
index c18e48a..5c23740 100644
--- a/qobject/json-parser.c
+++ b/qobject/json-parser.c
@@ -143,39 +143,39 @@ static QString *qstring_from_escaped_str(JSONParserContext *ctxt,

             switch (*ptr) {
             case '"':
-                qstring_append(str, "\"");
+                qstring_append_chr(str, '"');
                 ptr++;
                 break;
             case '\'':
-                qstring_append(str, "'");
+                qstring_append_chr(str, '\'');
                 ptr++;
                 break;
             case '\\':
-                qstring_append(str, "\\");
+                qstring_append_chr(str, '\\');
                 ptr++;
                 break;
             case '/':
-                qstring_append(str, "/");
+                qstring_append_chr(str, '/');
                 ptr++;
                 break;
             case 'b':
-                qstring_append(str, "\b");
+                qstring_append_chr(str, '\b');
                 ptr++;
                 break;
             case 'f':
-                qstring_append(str, "\f");
+                qstring_append_chr(str, '\f');
                 ptr++;
                 break;
             case 'n':
-                qstring_append(str, "\n");
+                qstring_append_chr(str, '\n');
                 ptr++;
                 break;
             case 'r':
-                qstring_append(str, "\r");
+                qstring_append_chr(str, '\r');
                 ptr++;
                 break;
             case 't':
-                qstring_append(str, "\t");
+                qstring_append_chr(str, '\t');
                 ptr++;
                 break;
             case 'u': {
@@ -204,12 +204,7 @@ static QString *qstring_from_escaped_str(JSONParserContext *ctxt,
                 goto out;
             }
         } else {
-            char dummy[2];
-
-            dummy[0] = *ptr++;
-            dummy[1] = 0;
-
-            qstring_append(str, dummy);
+            qstring_append_chr(str, *ptr++);
         }
     }

diff --git a/qobject/qjson.c b/qobject/qjson.c
index 7dc694c..a705aba 100644
--- a/qobject/qjson.c
+++ b/qobject/qjson.c
@@ -140,12 +140,12 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
         s.str = str;
         s.indent = indent + 1;
         s.pretty = pretty;
-        qstring_append(str, "{");
+        qstring_append_chr(str, '{');
         qdict_iter(val, to_json_dict_iter, &s);
         if (pretty) {
             qstring_append_printf(str, "\n%*s", 4 * indent, "");
         }
-        qstring_append(str, "}");
+        qstring_append_chr(str, '}');
         break;
     }
     case QTYPE_QLIST: {
@@ -156,12 +156,12 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent)
         s.str = str;
         s.indent = indent + 1;
         s.pretty = pretty;
-        qstring_append(str, "[");
+        qstring_append_chr(str, '[');
         qlist_iter(val, (void *)to_json_list_iter, &s);
         if (pretty) {
             qstring_append_printf(str, "\n%*s", 4 * indent, "");
         }
-        qstring_append(str, "]");
+        qstring_append_chr(str, ']');
         break;
     }
     case QTYPE_QFLOAT: {
@@ -218,7 +218,7 @@ int qstring_append_json_string(QString *qstring, const char *str)
     char *end;
     int result = 0;

-    qstring_append(qstring, "\"");
+    qstring_append_chr(qstring, '"');

     for (ptr = str; *ptr; ptr = end) {
         cp = mod_utf8_codepoint(ptr, 6, &end);
@@ -262,7 +262,7 @@ int qstring_append_json_string(QString *qstring, const char *str)
         }
     };

-    qstring_append(qstring, "\"");
+    qstring_append_chr(qstring, '"');
     return result;
 }

-- 
2.7.4

  parent reply	other threads:[~2016-10-10 13:24 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-10 13:23 [Qemu-devel] [PATCH v6 00/15] Add qapi-to-JSON visitor Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 01/15] qapi: Visitor documentation tweak Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 02/15] qapi: Assert finite use of 'number' Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 03/15] qapi: Factor out JSON string escaping Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 04/15] qapi: Factor out JSON number formatting Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 05/15] qapi: Add qstring_append_printf() Eric Blake
2016-10-10 13:23 ` Eric Blake [this message]
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 07/15] qstring: Add qstring_consume_str() Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 08/15] qstring: Add qstring_wrap_str() Eric Blake
2016-10-11 11:08   ` Marc-André Lureau
2016-10-11 15:04     ` Eric Blake
2016-10-11 15:05       ` Eric Blake
2016-10-11 15:25         ` Marc-André Lureau
2016-10-11 15:24   ` [Qemu-devel] [PATCH v6 08.5/15] fixup! " Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 09/15] qobject: Consolidate qobject_to_json() calls Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 10/15] tests: Test qobject_to_json() pretty formatting Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 11/15] qapi: Add JSON output visitor Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 12/15] qapi: Support pretty printing in " Eric Blake
2016-10-11 11:20   ` Marc-André Lureau
2016-10-11 15:09     ` Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 13/15] qobject: Implement qobject_to_json() atop JSON visitor Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 14/15] qapi: Add 'any' support to JSON output Eric Blake
2016-10-10 13:23 ` [Qemu-devel] [PATCH v6 15/15] qemu-img: Use new JSON output formatter Eric Blake
2016-10-11 12:14 ` [Qemu-devel] [PATCH v6 00/15] Add qapi-to-JSON visitor Marc-André Lureau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1476105837-9861-7-git-send-email-eblake@redhat.com \
    --to=eblake@redhat.com \
    --cc=armbru@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.