|
99 | 99 | * \c true or \c false
|
100 | 100 | * \par Floating point:
|
101 | 101 | * 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. |
104 | 107 | * \par Arrays:
|
105 | 108 | * Comma-separated list of elements, enclosed in square brackets ("[" and "]").
|
106 | 109 | * If the array length is indeterminate, an underscore ("_") appears immediately after the opening bracket.
|
|
114 | 117 | * \enum CborPrettyFlags
|
115 | 118 | * The CborPrettyFlags enum contains flags that control the conversion of CBOR to text format.
|
116 | 119 | *
|
| 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"). |
117 | 122 | * \value CborPrettyDefaultFlags Default conversion flags.
|
118 | 123 | */
|
119 | 124 |
|
@@ -404,26 +409,29 @@ static CborError value_to_pretty(FILE *out, CborValue *it, int flags)
|
404 | 409 | case CborDoubleType: {
|
405 | 410 | const char *suffix;
|
406 | 411 | double val;
|
| 412 | + int r; |
407 | 413 | if (false) {
|
408 | 414 | float f;
|
409 | 415 | case CborFloatType:
|
410 | 416 | cbor_value_get_float(it, &f);
|
411 | 417 | val = f;
|
412 |
| - suffix = "f"; |
| 418 | + suffix = flags & CborPrettyNumericEncodingIndicators ? "_2" : "f"; |
413 | 419 | } else if (false) {
|
414 | 420 | uint16_t f16;
|
415 | 421 | case CborHalfFloatType:
|
416 | 422 | cbor_value_get_half_float(it, &f16);
|
417 | 423 | val = decode_half(f16);
|
418 |
| - suffix = "f16"; |
| 424 | + suffix = flags & CborPrettyNumericEncodingIndicators ? "_1" : "f16"; |
419 | 425 | } else {
|
420 | 426 | cbor_value_get_double(it, &val);
|
421 | 427 | suffix = "";
|
422 | 428 | }
|
423 | 429 |
|
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 | + } |
427 | 435 |
|
428 | 436 | uint64_t ival = (uint64_t)fabs(val);
|
429 | 437 | if (ival == fabs(val)) {
|
|
0 commit comments