From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53844) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8U8d-0005D8-Hh for qemu-devel@nongnu.org; Thu, 02 Jun 2016 11:02:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b8U8Z-0002dT-3E for qemu-devel@nongnu.org; Thu, 02 Jun 2016 11:02:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40600) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8U8Y-0002dA-RX for qemu-devel@nongnu.org; Thu, 02 Jun 2016 11:02:19 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (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 1BF1C61A0E for ; Thu, 2 Jun 2016 15:02:18 +0000 (UTC) From: Markus Armbruster References: <1463632874-28559-1-git-send-email-eblake@redhat.com> <1463632874-28559-18-git-send-email-eblake@redhat.com> Date: Thu, 02 Jun 2016 17:02:15 +0200 In-Reply-To: <1463632874-28559-18-git-send-email-eblake@redhat.com> (Eric Blake's message of "Wed, 18 May 2016 22:41:03 -0600") Message-ID: <8737oveirs.fsf@dusky.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH v4 17/28] qapi: Factor out JSON number formatting List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: qemu-devel@nongnu.org, Luiz Capitulino Eric Blake writes: > Pull out a new qstring_append_json_number() helper, so that all > JSON output producers can use a consistent style for printing > floating point without duplicating code (since we are doing more > data massaging than a simple printf format can handle). (For > now, there is only one client, but later patches will use it.) > > Adding a return value will let callers that care diagnose the > situation where an attempt was made to output invalid JSON (which > does not specify Infinity or NaN). None of the current callers > care, but a future patch wants to make it possible to turn this > situation into an error. > > Signed-off-by: Eric Blake [...] > @@ -303,3 +282,39 @@ int qstring_append_json_string(QString *qstring, const char *str) > qstring_append(qstring, "\""); > return result; > } > + > +/** > + * Append a JSON representation of @number to @qstring. > + * > + * Returns -1 if the added text is not strict JSON, or 0 if the number > + * was finite. > + */ Suggest: * Return 0 if the number is finite, as required by RFC 7159, else -1. The return value makes some sense only for symmetry with qstring_append_json_string(). Without that, I'd ask you to keep this function simple. Callers could just as easily test isfinite() themselves. > +int qstring_append_json_number(QString *qstring, double number) > +{ > + char buffer[1024]; > + int len; > + > + /* FIXME: snprintf() is locale dependent; but JSON requires > + * numbers to be formatted as if in the C locale. Dependence > + * on C locale is a pervasive issue in QEMU. */ > + /* FIXME: This risks printing Inf or NaN, which are not valid > + * JSON values. */ > + /* FIXME: the default precision of 6 for %f often causes > + * rounding errors; we should be using DBL_DECIMAL_DIG (17), > + * and only rounding to a shorter number if the result would > + * still produce the same floating point value. */ > + len = snprintf(buffer, sizeof(buffer), "%f", number); > + assert(len > 0 && len < sizeof(buffer)); > + while (len > 0 && buffer[len - 1] == '0') { > + len--; > + } > + > + if (len && buffer[len - 1] == '.') { > + buffer[len - 1] = 0; > + } else { > + buffer[len] = 0; > + } > + > + qstring_append(qstring, buffer); > + return isfinite(number) ? 0 : -1; > +}