Rust-compatible text formatting in C: a float's Display (shortest round-trip, no exponent) and a string's Debug
$ sexc add @ajax/rust-fmt- total 3
- drwxr-xr-x-../
- drwxr-xr-x-Oct 27 04:08v/# 1 version
- drwxr-xr-x-Oct 27 04:08files/# the files of 0.1.0
- latest
- 0.1.0, published Oct 27 04:08 by ajax
- owners
- ajax
- license
- MIT
- dependencies
- @ajax/mesh-check ^0.1.0
- dependents
- @ajax/mesh-cluster-conf, @ajax/mesh-fleet-doc, @ajax/mesh-fleet-edit, @ajax/mesh-fleet-persist, @ajax/mesh-member-list, @ajax/mesh-peer-codec, @ajax/mesh-peer-relay, @ajax/mesh-place-compose, @ajax/mesh-place-mode, @ajax/mesh-place-wire, @ajax/mesh-placer, @ajax/mesh-sched-needs, @ajax/mesh-sched-report, @ajax/mesh-sched-weights
- keywords
- mesh alphaos fleet rust format float
- homepage
- https://github.com/thomasdavis/alpha-c-mesh/tree/main/packages/rust-fmt
- repository
- https://github.com/thomasdavis/alpha-c-mesh
1# @ajax/rust-fmt 2 3Text exactly as Rust's `format!` writes it, where C's `printf` differs: 4 5- `rf_f32` / `rf_f64`: a float's Display (`{}`): the shortest digits that read back as the same value, 6 never an exponent, no trailing `.0` (`1.0` is `1`, `1e-7` is `0.0000001`, NaN is `NaN`). 7- `rf_debug_str`: a string's Debug (`{:?}`): quoted, with `\n`, `\t`, `\r`, `\\`, `\"`, `\0` and 8 `\u{..}` escapes. 9- `rf_hex_alt`: `{:#x}` (`0x0` for zero, where C's `%#x` prints `0`). 10 11Everything else in Rust's formatting maps onto printf: `{:.2}` is `%.2f` (both round the exact binary 12value to nearest, ties to even), `{:016x}` is `%016llx`, `{:+.2}` is `%+.2f`. 13 14## Ported from 15 16The formatting the [alpha-rust-mesh](https://github.com/rsafier/alpha-rust-mesh) crates rely on 17(`core::fmt`'s float Display and `str::escape_debug`), for the fleet's wire formats and messages 18(`fleet.rs`, `sched.rs`), which must match the Rust byte for byte. 19 20## Tests 21 22`tests/fmt.c`: outputs checked against rustc, and a sweep showing every f32 Display reads back as itself. 23 24## Differences 25 26`rf_debug_str` escapes control bytes only; Rust also escapes some non-printable Unicode (e.g. lone 27combining marks at the start). Nothing in the fleet's messages contains them. 28 29## Licence 30 31MIT. See the repository's LICENSE.