Writing integers v0.1.0

Source
Spelling an integer as text: sign, base prefix, zero padding and digits.

MaxDecimalDigits

pub const MaxDecimalDigits: uint = 20;

The most digits a uint64 ever needs, for 18446744073709551615.

Source

WriteUint

pub func WriteUint(cursor: &var ByteCursor, value: uint64)

Writes the decimal digits of an unsigned value. A division loop produces them least significant first, so they land in a small buffer and go back reversed.

Source

Parameters

NameTypeDescription
cursor&var ByteCursorthe buffer the digits are written into
valueuint64the value to act on

WriteInt

pub func WriteInt(cursor: &var ByteCursor, value: int64)

Writes the sign, then the magnitude. The magnitude is taken in uint64 rather than by negating within int64, because the most negative int64 has no positive counterpart there: negating it overflows straight back to itself. Two's complement gives the right answer for that value and for every other one.

Source

Parameters

NameTypeDescription
cursor&var ByteCursorthe buffer the digits are written into
valueint64the value to act on

WriteIntegerDigits

pub func WriteIntegerDigits(
    writer: &var TextWriter,
    spec: FormatSpec,
    digits: char8[..],
    count: uint,
    negative: bool
) -> ! FormatError

Writes an integer whose digits are already produced, applying sign, prefix, zero-padding and alignment. digits holds the digits least significant first, which is the order a division loop makes them; count says how many. Zero-padding puts its zeros between the sign and the digits and wins over alignment when both are asked for. Nothing here allocates or copies the digits: each piece goes to the writer as it is reached. Reports whatever the writer reported, stopping at the first refusal.

Source

Parameters

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for
digitschar8[..]the decimal or based digits, least significant first
countuinthow many entries of digits are meaningful
negativeboolwhether a minus sign precedes the digits

Returns: success when the complete rendering was accepted

WriteIntegerSpec

pub func WriteIntegerSpec(
    writer: &var TextWriter,
    spec: FormatSpec,
    magnitude: uint64,
    negative: bool
) -> ! FormatError

Writes an integer under the whole of spec: base, sign, alternate prefix, zero-padding and alignment. magnitude is the absolute value and negative its sign, kept apart because the most negative value of a signed width has no positive counterpart to negate into. Reports whatever the writer reported.

Source

Parameters

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for
magnitudeuint64the absolute value to render
negativeboolwhether the value is negative

Returns: success when the complete rendering was accepted