Skip to content

Commit 9b017b2

Browse files
cborpretty.c: Implement numeric encoding indicators for floating point
RFC 7049 section 6 has an encoding mechanism that I had not noticed before. It says: An underscore followed by a decimal digit n indicates that the preceding item [...] was encoded with an additional information value of 24+n. For example, 1.5_1 is a half-precision floating-point number, while 1.5_3 is encoded as double precision. This flag only changes the suffix used by floating point. We don't show the encoding indicator in other contexts, including that for double precision. That's a job for another flag. Signed-off-by: Thiago Macieira <[email protected]>
1 parent 23ab30a commit 9b017b2

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/cbor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ CBOR_INLINE_API CborError cbor_value_get_double(const CborValue *value, double *
484484
/* Human-readable (dump) API */
485485

486486
enum CborPrettyFlags {
487+
CborPrettyNumericEncodingIndicators = 0x01,
488+
CborPrettyTextualEncodingIndicators = 0,
489+
487490
CborPrettyDefaultFlags = 0
488491
};
489492

src/cborpretty.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@
9999
* \c true or \c false
100100
* \par Floating point:
101101
* If NaN or infinite, the actual words \c NaN or \c infinite.
102-
* Otherwise, the decimal representation with as many digits as necessary to ensure no loss of information,
103-
* with float values suffixed by "f" and half-float values suffixed by "f16" (doubles have no suffix). A dot is always present.
102+
* Otherwise, the decimal representation with as many digits as necessary to ensure no loss of information.
103+
* By default, float values are suffixed by "f" and half-float values suffixed by "f16" (doubles have no suffix).
104+
* If the CborPrettyNumericEncodingIndicators flag is active, the values instead are encoded following the
105+
* Section 6 recommended encoding indicators: float values are suffixed with "_2" and half-float with "_1".
106+
* A dot is always present.
104107
* \par Arrays:
105108
* Comma-separated list of elements, enclosed in square brackets ("[" and "]").
106109
* If the array length is indeterminate, an underscore ("_") appears immediately after the opening bracket.
@@ -114,6 +117,8 @@
114117
* \enum CborPrettyFlags
115118
* The CborPrettyFlags enum contains flags that control the conversion of CBOR to text format.
116119
*
120+
* \value CborPrettyNumericEncodingIndicators Use numeric encoding indicators instead of textual for float and half-float.
121+
* \value CborPrettyTextualEncodingIndicators Use textual encoding indicators for float ("f") and half-float ("f16").
117122
* \value CborPrettyDefaultFlags Default conversion flags.
118123
*/
119124

@@ -404,26 +409,29 @@ static CborError value_to_pretty(FILE *out, CborValue *it, int flags)
404409
case CborDoubleType: {
405410
const char *suffix;
406411
double val;
412+
int r;
407413
if (false) {
408414
float f;
409415
case CborFloatType:
410416
cbor_value_get_float(it, &f);
411417
val = f;
412-
suffix = "f";
418+
suffix = flags & CborPrettyNumericEncodingIndicators ? "_2" : "f";
413419
} else if (false) {
414420
uint16_t f16;
415421
case CborHalfFloatType:
416422
cbor_value_get_half_float(it, &f16);
417423
val = decode_half(f16);
418-
suffix = "f16";
424+
suffix = flags & CborPrettyNumericEncodingIndicators ? "_1" : "f16";
419425
} else {
420426
cbor_value_get_double(it, &val);
421427
suffix = "";
422428
}
423429

424-
int r = fpclassify(val);
425-
if (r == FP_NAN || r == FP_INFINITE)
426-
suffix = "";
430+
if ((flags & CborPrettyNumericEncodingIndicators) == 0) {
431+
r = fpclassify(val);
432+
if (r == FP_NAN || r == FP_INFINITE)
433+
suffix = "";
434+
}
427435

428436
uint64_t ival = (uint64_t)fabs(val);
429437
if (ival == fabs(val)) {

0 commit comments

Comments
 (0)