Module fmt/index
Formatted output to stdout and stderr.
Four printers over one body: println / print write to stdout,
eprintln / eprint to stderr, and each takes any T <: ToString, so a
template string (`x = ${v}`) and a bare value are the same call.
Output goes straight to the C FILE* with fwrite — line-buffered on a
tty, block-buffered into a pipe, and NOT flushed here, so interleaving
stdout with stderr in a redirected run can reorder.
This is also the front door for the formatting vocabulary: ToString and
Debug (./to_string.yo), Format and the value.format(spec) protocol
(./format.yo), FormatSpec (./spec.yo), and — re-exported from
std/string because the dependency may only run that way —
StringBuilder and Alignment.
Stability
unstable — the four printers are frozen in practice (they are Rust's
println! family minus the macro, every name in the tree calls them, and
nothing about their signature is in question), but this module's job is to
BE the surface of std/fmt, and three of the four things it re-exports are
themselves unstable: Debug is one release old (D15), FormatSpec has no
dynamic width or precision, and StringBuilder still carries the number
renderers that retired into it from Writer. Each says so in its own
## Stability section. There is no open question about print/println
itself; freezing this file means freezing what it forwards.
Types
A parsed format specification. Produced by parse, never written by hand —
which is why the optional parts are Option rather than sentinels.
Fields
| Name | Type | Description |
|---|---|---|
fill | rune | Padding character. Defaults to a space. |
align | Option(Alignment) | Explicit alignment, if the spec gave one. |
plus | bool |
|
alt | bool |
|
zero | bool |
|
width | usize | Minimum width in CHARACTERS. |
precision | Option(usize) |
|
kind | rune | Radix selector: |
Methods
empty : (FormatSpec) fn() -> FormatSpecThe default spec: no width, no precision, no radix.
Returns: FormatSpec
parse : (FormatSpec) fn(s : str) -> FormatSpecParse a spec. TOTAL: anything unrecognised is ignored rather than
rejected, so a malformed spec degrades to plain to_string() output
instead of failing at runtime.
Parameters
| Name | Type | Notes |
|---|---|---|
s | str |
Returns: FormatSpec
pad : (FormatSpec) fn(self : FormatSpec, body : String, dflt : Alignment) -> StringApply width/fill/alignment (and text truncation) to an already-rendered
body. dflt is the alignment used when the spec did not give one —
Left for text, Right for numbers, as in Rust.
Parameters
| Name | Type | Notes |
|---|---|---|
self | FormatSpec | |
body | String | |
dflt | Alignment |
Returns: String
pad_numeric : (FormatSpec) fn(self : FormatSpec, sign : String, prefix : String, digits : String) -> StringNumber-aware padding: with 0 set and no explicit alignment, the zero
fill goes BETWEEN the sign/prefix and the digits, so -0042 and
0x0000002a are reachable. Otherwise this is ordinary right-aligned
padding over the whole rendering.
Parameters
| Name | Type | Notes |
|---|---|---|
self | FormatSpec | |
sign | String | |
prefix | String | |
digits | String |
Returns: String
Mutable buffer for building a String incrementally.
Use StringBuilder when you need to construct a string from many parts,
appending bytes or strings in a loop, before converting to an immutable
String with to_string().
Example
sb := StringBuilder.new();
sb.write_str("Hello");
sb.write_str(", ");
sb.write_string(`world`);
sb.write_byte(u8(33)); // '!'
result := sb.to_string();
assert(result == `Hello, world!`, "built string");
Fields
| Name | Type | Description |
|---|---|---|
_buf | ArrayList(u8) |
Methods
new : (StringBuilder) fn() -> StringBuilderCreate a new, empty StringBuilder.
Returns: StringBuilder
with_capacity : (StringBuilder) fn(capacity : usize) -> StringBuilderCreate a StringBuilder pre-allocated for capacity bytes.
Parameters
| Name | Type | Notes |
|---|---|---|
capacity | usize |
Returns: StringBuilder
len : (StringBuilder) fn(self : StringBuilder) -> usizeReturns the current number of bytes in the buffer.
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder |
Returns: usize
is_empty : (StringBuilder) fn(self : StringBuilder) -> boolwrite_str : (StringBuilder) fn(self : StringBuilder, s : str) -> unitAppend a str (raw byte slice) to the buffer.
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder | |
s | str |
Returns: unit
write_string : (StringBuilder) fn(self : StringBuilder, s : String) -> unitwrite_byte : (StringBuilder) fn(self : StringBuilder, b : u8) -> unitwrite_rune : (StringBuilder) fn(self : StringBuilder, r : rune) -> unitAppend a single Unicode code point, encoded as UTF-8.
Example
sb := StringBuilder.new();
sb.write_rune(rune(0x1F600)); // 😀
sb.write_rune(rune(0x41)); // 'A'
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder | |
r | rune |
Returns: unit
write_line : (StringBuilder) fn(self : StringBuilder, s : String) -> unitAppend a String followed by a newline byte (\n).
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder | |
s | String |
Returns: unit
to_string : (StringBuilder) fn(self : StringBuilder) -> StringDetach the accumulated bytes as a String, leaving the builder EMPTY and
still usable.
It does not copy and it does not consume the builder: the receiver is a
reference, and it starts over with a fresh buffer, so building an N-byte
string costs O(N) once. The returned String is therefore independent —
a later write_* on the same builder cannot mutate a string already
handed to a caller.
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder |
Returns: String
write_hex : (StringBuilder) fn(self : StringBuilder, n : u64) -> unitAppend an unsigned 64-bit integer in lowercase hexadecimal.
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder | |
n | u64 |
Returns: unit
write_f64 : (StringBuilder) fn(self : StringBuilder, n : f64, precision : i32) -> unitAppend n with exactly precision decimal places.
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder | |
n | f64 | |
precision | i32 |
Returns: unit
write_padded : (StringBuilder) fn(self : StringBuilder, s : str, width : usize, pad : rune, align : Alignment) -> unitAppend s padded to width RUNES with pad, aligned per align.
A s already at or over the width is written unpadded.
Width is a RUNE count, matching FormatSpec's {:width$} and Rust's.
It used to be a BYTE count here, so write_padded("héllo", 8, ' ', .Left)
emitted two spaces instead of three and the column did not line up — the
two width bases in one module disagreed
(issues/fixed/write-padded-counted-bytes-where-formatspec-counts-runes.md).
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder | |
s | str | |
width | usize | |
pad | rune | |
align | Alignment |
Returns: unit
clear : (StringBuilder) fn(self : StringBuilder) -> unitEmpty the builder.
This RELEASES the buffer rather than retaining its capacity — unlike
Rust's String::clear and unlike ArrayList.clear, both of which keep the
allocation for reuse. A builder cleared in a loop therefore reallocates
from zero on every pass; when that matters, keep a fresh builder per
iteration instead (it costs the same) or hand the bytes off with
to_string(), which detaches for the same price.
(issues/stddoc-str-string-builder-clear-drops-capacity.md)
Parameters
| Name | Type | Notes |
|---|---|---|
self | StringBuilder |
Returns: unit
default : (StringBuilder) fn() -> StringBuilderThe default value of the type.
Returns: StringBuilder
Where the text sits inside a padded field — the argument to
write_padded, and the same three cases FormatSpec parses from
< / > / ^.
It lives here rather than in std/fmt because write_padded does, and
std/fmt already depends on std/string; the other direction would be a
cycle. std/fmt re-exports it, so importing it from std/fmt works too.
Variants
| Variant | Fields | Description |
|---|---|---|
Left | ||
Right | ||
Center |
Traits / Modules
Methods
to_string : fn(self : Self) -> StringImplementors
Methods
debug_string : fn(self : Self) -> StringMethods
format : fn(self : Self, spec : str) -> StringImplementors
Functions
Print a value that implements ToString to stdout, followed by a newline.
Type Parameters
| Name | Type | Notes |
|---|---|---|
T | Type | comptime |
Parameters
| Name | Type | Notes |
|---|---|---|
v | T |
Returns: unit
Print a value that implements ToString to stdout, without a trailing newline.
Type Parameters
| Name | Type | Notes |
|---|---|---|
T | Type | comptime |
Parameters
| Name | Type | Notes |
|---|---|---|
v | T |
Returns: unit
Print a value that implements ToString to stderr, followed by a newline.
Type Parameters
| Name | Type | Notes |
|---|---|---|
T | Type | comptime |
Parameters
| Name | Type | Notes |
|---|---|---|
v | T |
Returns: unit
Print a value that implements ToString to stderr, without a trailing newline.
Type Parameters
| Name | Type | Notes |
|---|---|---|
T | Type | comptime |
Parameters
| Name | Type | Notes |
|---|---|---|
v | T |
Returns: unit