zena:string-convert
import {…} from 'zena:string-convert';
String conversion functions for primitive types
These provide both low-level buffer writing (for StringBuilder) and high-level String creation (for template literals).
Functions
writeI32
function writeI32(value: i32, buffer: ByteArray, bufferEnd: i32): inline (i32, i32)
Writes an i32 to a buffer. Buffer must have at least 11 bytes. Returns (startPos, endPos) where the digits are in buffer[startPos..endPos).
writeU32
function writeU32(value: u32, buffer: ByteArray, bufferEnd: i32): inline (i32, i32)
Writes a u32 to a buffer. Buffer must have at least 10 bytes. Returns (startPos, endPos).
writeI64
function writeI64(value: i64, buffer: ByteArray, bufferEnd: i32): inline (i32, i32)
Writes an i64 to a buffer. Buffer must have at least 20 bytes. Returns (startPos, endPos).
writeU64
function writeU64(value: u64, buffer: ByteArray, bufferEnd: i32): inline (i32, i32)
Writes a u64 to a buffer. Buffer must have at least 20 bytes. Returns (startPos, endPos).
i32ToString
function i32ToString(value: i32): String
Converts an i32 to its decimal string representation.
u32ToString
function u32ToString(value: u32): String
Converts a u32 to its decimal string representation.
i64ToString
function i64ToString(value: i64): String
Converts an i64 to its decimal string representation.
u64ToString
function u64ToString(value: u64): String
Converts a u64 to its decimal string representation.
boolToString
function boolToString(value: boolean): String
Converts a bool to "true" or "false".
parseI32
function parseI32(s: String): inline (true, i32) | inline (false, _)
Parse a decimal integer from a string, with optional leading sign.
Returns inline (true, value) on success, inline (false, _) on failure.
Fails if the string is empty or contains non-digit characters (after optional sign).
decimalToF64
function decimalToF64(d: u64, p: i32, negative: boolean, inexact: boolean, tail: u64): f64
The f64 nearest to d * 10^p, correctly rounded. d is the decimal
significand as an integer below 2^64 and p its decimal exponent.
inexact says the true value lies strictly between d and d + 1
— the caller had more digits than it could keep — and tail is that
leftover fraction scaled by 2^11.
With 19 or fewer significant digits inexact is false and the
result is the correctly rounded one, always. Beyond that the
significand no longer fits: normalizing d to 64 bits leaves only
64 - bits(d) low bits for the remainder, so the value is placed to
within half of the last of them and can land on the wrong side of a
rounding boundary that falls inside that gap. Measured against a
correctly-rounded implementation this is about one input in five
thousand of more than 19 significant digits, always by one ulp
(dev/fp-difftest.js measures it).
Closing it needs an exact-arithmetic fallback, which the algorithm
this comes from also leaves to its caller.
f32ToString
function f32ToString(value: f32): String
The shortest decimal string that reads back as this f32. The f32
interval is used, not the f64 one the promoted value would have, so
0.1 as f32 prints "0.1" rather than "0.10000000149011612".
f64ToString
function f64ToString(value: f64): String
The shortest decimal string that reads back as this f64.
f64ToPrecision
function f64ToPrecision(value: f64, sigDigits: i32): String
value rounded to sigDigits significant decimal digits, in the
same format f64ToString uses. sigDigits is clamped to [1, 17].
parseF64
function parseF64(s: String): f64
Parses a decimal (or hexadecimal) number string to the NEAREST f64, correctly rounded.
The significand is accumulated as an integer and the exponent
tracked alongside, so no rounding happens until the single
decimalToF64 at the end. Digits beyond the nineteenth cannot
change the result except through the tie they might break, so they
are folded into a sticky low digit rather than dropped.
parseI64
function parseI64(s: String): inline (true, i64) | inline (false, _)
Parse an i64 integer from a string, supporting optional leading sign,
optional hex prefix (0x or 0X), and optional underscores (_).
Returns inline (true, value) on success, inline (false, _) on failure.