Module fmt/format

fmt/format
Stability: unstable — it is a thin dispatch layer over `./spec.yo`, and it inherits that module's two open questions (no dynamic width or precision, and a `parse` that never reports a bad spec). It has one of its own: fixed-point float rendering goes through `StringBuilder.write_f64`, which truncates at 63 bytes, so `f64(1.0e300).format(".2")` returns 63 digits of a 302-digit number (`issues/stddoc-str-string-builder-write-f64-truncates.md`). Fixing that changes output. The `Format` trait itself — one method, `spec` as a plain `str`, an unrecognised spec degrading to `to_string()` — is settled. — stable modules only change additively; this one may still change.

The Format protocol — value.format(spec) (STD_API_AUDIT D3.10).

The spec grammar lives in ./spec.yo; this module is the dispatch layer. Numeric types get radix and sign-aware zero padding; everything else that implements ToString gets width, fill, alignment and truncation through one blanket impl — Rust's model, where fill/align/width are post-processing over whatever the value rendered as.

Example

{ Format } :: import("std/fmt/format");

i32(255).format("#06x");   // "0x00ff"
f64(3.14159).format(".2"); // "3.14"
`ada`.format("<8");        // "ada     "

Dispatch is by impl specificity: the eighteen numeric impls and the four text ones are concrete, and a single blanket impl(T <: ToString) at the bottom of the file catches everything else — so any type that can render itself gets width, fill, alignment and truncation for free, and a concrete impl always wins over the blanket one. f32 delegates to f64; every integer funnels into one _format_int after splitting itself into sign and magnitude in its own width.

Stability

unstable — it is a thin dispatch layer over ./spec.yo, and it inherits that module's two open questions (no dynamic width or precision, and a parse that never reports a bad spec). It has one of its own: fixed-point float rendering goes through StringBuilder.write_f64, which truncates at 63 bytes, so f64(1.0e300).format(".2") returns 63 digits of a 302-digit number (issues/stddoc-str-string-builder-write-f64-truncates.md). Fixing that changes output. The Format trait itself — one method, spec as a plain str, an unrecognised spec degrading to to_string() — is settled.

Traits / Modules

Format trait
Format

Format a value according to a spec string.

Methods

format : fn(self : Self, spec : str) -> String

Render self under spec. An unrecognised spec degrades to the plain to_string() rendering rather than failing.

Parameters

NameTypeNotes
selfSelf
specstr

Returns: String

Implementors