skip to content
==> include/mesh/rust_fmt.h <==
1/* @ajax/rust-fmt: text exactly as Rust's formatting machinery writes it, where C's printf differs.
2 *
3 * The Rust this repo is ported from builds its wire formats and messages with `format!`. Most of
4 * it maps onto printf (`{:.2}` is "%.2f": both round the exact binary value to nearest, ties to
5 * even), but three things do not:
6 * - `{}` of a float (Display): the shortest digits that read back as the same value, never an
7 *   exponent, and no ".0" (1.0 is "1", 1e-7 is "0.0000001", NaN is "NaN");
8 * - `{:?}` of a string (Debug): quoted, with Rust's escapes;
9 * - `{:#x}`: "0x0" for zero, where C's "%#x" writes "0". */
10#ifndef MESH_RUST_FMT_H
11#define MESH_RUST_FMT_H
12
13#include <stddef.h>
14#include <stdint.h>
15
16/* Room for any f32/f64 Display (f64::MAX is 309 digits, the smallest subnormal ~330 chars). */
17#define RF_FLOAT_MAX 400
18
19/* `{}` of an f32 / f64 into out (RF_FLOAT_MAX bytes); returns out. */
20char *rf_f32(char out[RF_FLOAT_MAX], float x);
21char *rf_f64(char out[RF_FLOAT_MAX], double x);
22
23/* `{:?}` of a string: a new malloc'd string ("\"a\\nb\""). NULL only on out of memory. */
24char *rf_debug_str(const char *s);
25
26/* `{:#x}` of a u32 / u64: "0x" then lowercase hex digits, "0x0" for zero; returns out (19+ bytes). */
27char *rf_hex_alt(char out[24], uint64_t x);
28
29#endif