Formatting utilities for converting values to strings.
def format_i32(buf: ptr[char], value: int32) -> uint64
Format a signed 32-bit integer as a decimal string into buf. Writes a null
terminator. Returns the number of characters written (excluding the null
terminator).
from std.fmt import format_i32
from std.alloc import alloc
from std.io import puts
pub def main() -> int32:
let buf: ptr[char] = alloc(16)
let len = format_i32(buf, -42)
# buf == "-42\0", len == 3
puts(buf)
free(buf)
return 0
def format_f64(buf: ptr[char], value: float64) -> uint64
Format a 64-bit float as a decimal string into buf. Writes a null
terminator. Returns the number of characters written. x86_64 only.
let len = format_f64(buf, 3.14)
int32: 12 bytes (“-2147483648” + null)format_u32, format_i64, format_u64, format_ptr, and sprintf-style
formatting are future work.
All targets: pure software, no syscalls needed (format_f64 is x86_64 only).