|
| 1 | +#ifndef Py_INTERNAL_RYU_H |
| 2 | +#define Py_INTERNAL_RYU_H |
| 3 | +#ifdef __cplusplus |
| 4 | +extern "C" { |
| 5 | +#endif |
| 6 | + |
| 7 | +#ifndef Py_BUILD_CORE |
| 8 | +# error "this header requires Py_BUILD_CORE define" |
| 9 | +#endif |
| 10 | + |
| 11 | +#include "pycore_pymath.h" // _PY_SHORT_FLOAT_REPR |
| 12 | + |
| 13 | +#if _PY_SHORT_FLOAT_REPR == 1 |
| 14 | + |
| 15 | +/* Space for the longest possible result of _Py_ryu_dtoa: 17 digits + nul, |
| 16 | + or "Infinity" + nul. */ |
| 17 | +#define _Py_RYU_DTOA_BUFSIZE 18 |
| 18 | + |
| 19 | +/* Compute the shortest decimal representation of 'd' that round-trips |
| 20 | + when parsed back as a double. |
| 21 | +
|
| 22 | + Writes 1 to 17 ASCII digits into 'buf' (no sign, no decimal point, |
| 23 | + no exponent), null-terminated. For NaN and infinities writes "NaN" |
| 24 | + or "Infinity" instead. 'buf' must have room for at least |
| 25 | + _Py_RYU_DTOA_BUFSIZE bytes. |
| 26 | +
|
| 27 | + On return, *decpt is set to the position of the decimal point relative |
| 28 | + to the start of the digit string (9999 for NaN/Inf), and *sign is set |
| 29 | + to 0 for positive values and 1 for negative values. |
| 30 | +
|
| 31 | + This produces the same (digits, decpt, sign) triple as |
| 32 | + _Py_dg_dtoa(d, 0, 0, decpt, sign, ...), but uses only fixed-size |
| 33 | + integer arithmetic and writes into a caller-provided buffer. |
| 34 | +
|
| 35 | + Returns the number of bytes written, not counting the trailing nul. */ |
| 36 | +// Export for '_testinternalcapi' shared extension. |
| 37 | +PyAPI_FUNC(int) _Py_ryu_dtoa(double d, char *buf, int *decpt, int *sign); |
| 38 | + |
| 39 | + |
| 40 | +/* Space for the longest possible Python repr() of a float plus a |
| 41 | + trailing nul: |
| 42 | +
|
| 43 | + sign 1 + digit 1 + '.' 1 + digits 16 + 'e' 1 + sign 1 + exp 3 |
| 44 | + = 24 chars + nul = 25. |
| 45 | +
|
| 46 | + Rounded up to the next 4-byte boundary. */ |
| 47 | +#define _Py_DOUBLE_REPR_BUFSIZE 28 |
| 48 | + |
| 49 | +/* Write the Python repr of 'val' (as PyOS_double_to_string with format |
| 50 | + code 'r' and the given Py_DTSF_* flags) into the caller-provided |
| 51 | + buffer 'buf' of size 'bufsize', which must be at least |
| 52 | + _Py_DOUBLE_REPR_BUFSIZE. The result is null-terminated. |
| 53 | +
|
| 54 | + Returns the number of bytes written, not counting the trailing nul. */ |
| 55 | +// Export for '_json' shared extension. |
| 56 | +PyAPI_FUNC(Py_ssize_t) _Py_double_repr_buffered(double val, char *buf, |
| 57 | + Py_ssize_t bufsize, int flags); |
| 58 | + |
| 59 | +#endif // _PY_SHORT_FLOAT_REPR == 1 |
| 60 | + |
| 61 | +#ifdef __cplusplus |
| 62 | +} |
| 63 | +#endif |
| 64 | +#endif /* !Py_INTERNAL_RYU_H */ |
0 commit comments