From patchwork Fri Apr 29 04:23:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 8977061 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id F3B319F39D for ; Fri, 29 Apr 2016 04:30:05 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1EADA2021A for ; Fri, 29 Apr 2016 04:30:05 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0CD6420219 for ; Fri, 29 Apr 2016 04:30:04 +0000 (UTC) Received: from localhost ([::1]:52249 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aw043-0003GO-50 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 29 Apr 2016 00:30:03 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37745) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avzy3-0007Ae-0h for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1avzy1-0000sC-EY for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57554) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avzy1-0000s0-7N for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:49 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E6A2E7F6A8 for ; Fri, 29 Apr 2016 04:23:48 +0000 (UTC) Received: from red.redhat.com (ovpn-113-21.phx2.redhat.com [10.3.113.21]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3T4Ngf3016324; Fri, 29 Apr 2016 00:23:48 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Thu, 28 Apr 2016 22:23:27 -0600 Message-Id: <1461903820-3092-7-git-send-email-eblake@redhat.com> In-Reply-To: <1461903820-3092-1-git-send-email-eblake@redhat.com> References: <1461903820-3092-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 06/18] qapi: Add qstring_append_format() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: famz@redhat.com, armbru@redhat.com, Luiz Capitulino Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Back in commit 764c1ca (Nov 2009), we added qstring_append_int(). However, it did not see any use until commit 190c882 (Jan 2015). Furthermore, it has a rather limited use case - to print anything else, callers still have to format into a temporary buffer, unless we want to introduce an explosion of new qstring_append_* methods for each useful type to print. A much better approach is to add a wrapper that merges printf behavior onto qstring_append, via the new qstring_append_format() (and its vararg counterpart). In fact, with that in place, we no longer need qstring_append_int(). Other immediate uses for the new function include simplifying two existing clients of qstring_append() on a just-formatted buffer, and the fact that we can take advantage of printf width manipulations for more efficient indentation. Signed-off-by: Eric Blake Reviewed-by: Fam Zheng --- v3: rebase to master v2: also simplify qstring_append_json_string(), add assertion that format is well-formed --- include/qapi/qmp/qstring.h | 7 +++++-- qjson.c | 2 +- qobject/qobject-json.c | 25 +++++-------------------- qobject/qstring.c | 37 +++++++++++++++++++++++++------------ 4 files changed, 36 insertions(+), 35 deletions(-) diff --git a/include/qapi/qmp/qstring.h b/include/qapi/qmp/qstring.h index f00e3df..10afa5d 100644 --- a/include/qapi/qmp/qstring.h +++ b/include/qapi/qmp/qstring.h @@ -1,7 +1,7 @@ /* * QString Module * - * Copyright (C) 2009, 2015 Red Hat Inc. + * Copyright (C) 2009-2016 Red Hat Inc. * * Authors: * Luiz Capitulino @@ -28,9 +28,12 @@ QString *qstring_from_str(const char *str); QString *qstring_from_substr(const char *str, int start, int end); size_t qstring_get_length(const QString *qstring); const char *qstring_get_str(const QString *qstring); -void qstring_append_int(QString *qstring, int64_t value); void qstring_append(QString *qstring, const char *str); void qstring_append_chr(QString *qstring, int c); +void qstring_append_format(QString *qstring, const char *fmt, ...) + GCC_FMT_ATTR(2, 3); +void qstring_append_vformat(QString *qstring, const char *fmt, va_list ap) + GCC_FMT_ATTR(2, 0); void qstring_append_json_string(QString *qstring, const char *raw); void qstring_append_json_number(QString *qstring, double number, Error **errp); QString *qobject_to_qstring(const QObject *obj); diff --git a/qjson.c b/qjson.c index b9a9a36..d172b1f 100644 --- a/qjson.c +++ b/qjson.c @@ -70,7 +70,7 @@ void json_end_array(QJSON *json) void json_prop_int(QJSON *json, const char *name, int64_t val) { json_emit_element(json, name); - qstring_append_int(json->str, val); + qstring_append_format(json->str, "%" PRId64, val); } void json_prop_str(QJSON *json, const char *name, const char *str) diff --git a/qobject/qobject-json.c b/qobject/qobject-json.c index 97bccb7..963de07 100644 --- a/qobject/qobject-json.c +++ b/qobject/qobject-json.c @@ -80,16 +80,13 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent); static void to_json_dict_iter(const char *key, QObject *obj, void *opaque) { ToJsonIterState *s = opaque; - int j; if (s->count) { qstring_append(s->str, s->pretty ? "," : ", "); } if (s->pretty) { - qstring_append(s->str, "\n"); - for (j = 0 ; j < s->indent ; j++) - qstring_append(s->str, " "); + qstring_append_format(s->str, "\n%*s", 4 * s->indent, ""); } qstring_append_json_string(s->str, key); @@ -102,16 +99,13 @@ static void to_json_dict_iter(const char *key, QObject *obj, void *opaque) static void to_json_list_iter(QObject *obj, void *opaque) { ToJsonIterState *s = opaque; - int j; if (s->count) { qstring_append(s->str, s->pretty ? "," : ", "); } if (s->pretty) { - qstring_append(s->str, "\n"); - for (j = 0 ; j < s->indent ; j++) - qstring_append(s->str, " "); + qstring_append_format(s->str, "\n%*s", 4 * s->indent, ""); } to_json(obj, s->str, s->pretty, s->indent); @@ -126,10 +120,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) break; case QTYPE_QINT: { QInt *val = qobject_to_qint(obj); - char buffer[1024]; - - snprintf(buffer, sizeof(buffer), "%" PRId64, qint_get_int(val)); - qstring_append(str, buffer); + qstring_append_format(str, "%" PRId64, qint_get_int(val)); break; } case QTYPE_QSTRING: { @@ -148,10 +139,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) qstring_append(str, "{"); qdict_iter(val, to_json_dict_iter, &s); if (pretty) { - int j; - qstring_append(str, "\n"); - for (j = 0 ; j < indent ; j++) - qstring_append(str, " "); + qstring_append_format(str, "\n%*s", 4 * indent, ""); } qstring_append(str, "}"); break; @@ -167,10 +155,7 @@ static void to_json(const QObject *obj, QString *str, int pretty, int indent) qstring_append(str, "["); qlist_iter(val, (void *)to_json_list_iter, &s); if (pretty) { - int j; - qstring_append(str, "\n"); - for (j = 0 ; j < indent ; j++) - qstring_append(str, " "); + qstring_append_format(str, "\n%*s", 4 * indent, ""); } qstring_append(str, "]"); break; diff --git a/qobject/qstring.c b/qobject/qstring.c index 618dd8f..0285d53 100644 --- a/qobject/qstring.c +++ b/qobject/qstring.c @@ -90,12 +90,28 @@ void qstring_append(QString *qstring, const char *str) qstring->string[qstring->length] = 0; } -void qstring_append_int(QString *qstring, int64_t value) +void qstring_append_format(QString *qstring, const char *fmt, ...) { - char num[32]; + va_list ap; - snprintf(num, sizeof(num), "%" PRId64, value); - qstring_append(qstring, num); + va_start(ap, fmt); + qstring_append_vformat(qstring, fmt, ap); + va_end(ap); +} + +void qstring_append_vformat(QString *qstring, const char *fmt, va_list ap) +{ + va_list ap2; + int len; + + va_copy(ap2, ap); + len = vsnprintf(qstring->string + qstring->length, 0, fmt, ap2); + assert(len >= 0); + va_end(ap2); + + capacity_increase(qstring, len); + vsnprintf(qstring->string + qstring->length, len + 1, fmt, ap); + qstring->length += len; } /** @@ -116,7 +132,6 @@ void qstring_append_json_string(QString *qstring, const char *raw) { const char *ptr = raw; int cp; - char buf[16]; char *end; qstring_append(qstring, "\""); @@ -151,16 +166,14 @@ void qstring_append_json_string(QString *qstring, const char *raw) } if (cp > 0xFFFF) { /* beyond BMP; need a surrogate pair */ - snprintf(buf, sizeof(buf), "\\u%04X\\u%04X", - 0xD800 + ((cp - 0x10000) >> 10), - 0xDC00 + ((cp - 0x10000) & 0x3FF)); + qstring_append_format(qstring, "\\u%04X\\u%04X", + 0xD800 + ((cp - 0x10000) >> 10), + 0xDC00 + ((cp - 0x10000) & 0x3FF)); } else if (cp < 0x20 || cp >= 0x7F) { - snprintf(buf, sizeof(buf), "\\u%04X", cp); + qstring_append_format(qstring, "\\u%04X", cp); } else { - buf[0] = cp; - buf[1] = 0; + qstring_append_chr(qstring, cp); } - qstring_append(qstring, buf); } };