Writing characters and booleans v0.1.0
ReplacementCharacter
pub const ReplacementCharacter: char32 = 0xFFFDu32 as char32;
The character written in place of anything that is not one.
MaximumScalarValue
pub const MaximumScalarValue: uint64 = 0x10FFFF;
The largest value a Unicode scalar can have, past which nothing is a character.
Named because a char64 has room for a great deal more than this, and the check that it does not hold one has
to happen at the full width: narrowing first would turn 0x100000041 into A.
IsWideScalarValue
pub func IsWideScalarValue(value: uint64) -> bool
Whether value, at whatever width it was read from, is a Unicode scalar value.
Takes a uint64 so that a char64 is checked before it is narrowed rather than after. A value past the last
scalar and a value in the surrogate range are both refused, which is the same rule Text::IsScalarValue
applies at thirty-two bits.
Cannot fail; runs in constant time and allocates nothing.
Parameters
| Name | Type | Description |
|---|---|---|
value | uint64 | the numeric value of a character, at its own width |
Returns: true when a character of that value exists
WriteInvalidEscape
pub func WriteInvalidEscape(
writer: &var TextWriter,
value: uint64
) -> ! FormatError
Writes the escape for a value that is not a character, preserving exactly what it was.
A value that fits a byte is written \x{nn}, because a stray byte is not a scalar and should not be shown as
one; anything else is written \u{...} with as many digits as its width needs, in fours. No valid character
can collide with either: the escapes are only ever written for values that are not characters.
Reports whatever the writer reported, stopping at the first refusal.
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
value | uint64 | the numeric value to spell |
Returns: success when the whole escape was accepted
IsScalarValue512
pub func IsScalarValue512(value: uint512) -> bool
Whether value, read from a character wider than sixty-four bits, is a Unicode scalar value.
The counterpart of IsWideScalarValue for char128, char256 and char512, which widen into a uint512 to
be checked. Any bit above the sixty-fourth makes the value no character at all, and the check sees it because
nothing is narrowed until the value is known to be at most U+10FFFF.
Cannot fail; runs in constant time and allocates nothing.
Parameters
| Name | Type | Description |
|---|---|---|
value | uint512 | the numeric value of a character, at its own width |
Returns: true when a character of that value exists
WriteWideInvalidEscape
pub func WriteWideInvalidEscape(
writer: &var TextWriter,
value: uint512
) -> ! FormatError
Writes the escape for a value that is not a character, from a character wider than sixty-four bits.
A value that fits sixty-four bits is spelled exactly as WriteInvalidEscape spells it, so a char128 and a
char64 holding the same number render alike. A wider value keeps going in the same doubling steps: 32 hex
digits past sixty-four bits, 64 past 128 and 128 past 256, so every bit the value holds is shown. The digits
are written one at a time from the value itself, and nothing is allocated.
Reports whatever the writer reported, stopping at the first refusal.
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
value | uint512 | the numeric value to spell |
Returns: success when the whole escape was accepted
WriteWideCharacterDebug
pub func WriteWideCharacterDebug(
writer: &var TextWriter,
value: uint512
) -> ! FormatError
Writes the Debug form of a character wider than sixty-four bits.
A value that fits sixty-four bits is written exactly as a char64 of the same number would be, quoted, and a
wider one as its full escape, so no two values of any character width share a rendering they should not.
Reports whatever the writer reported, stopping at the first refusal.
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
value | uint512 | the numeric value of the character, at its own width |
Returns: success when the whole rendering was accepted
WriteCharacterDebug
pub func WriteCharacterDebug(
writer: &var TextWriter,
value: uint64,
whole: bool
) -> ! FormatError
Writes one character's Debug form: quoted, and preserved exactly when it is not a character.
whole is the caller's own answer to whether the value is a whole character, because the widths disagree about
it. A char8 of 0xC3 is a valid scalar value as a number and a piece of a character as a byte, and it is the
byte's answer that governs: Display writes U+FFFD for it, so Debug must preserve the byte rather than write
the character that number happens to name.
Reports whatever the writer reported, stopping at the first refusal.
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
value | uint64 | the numeric value of the character, at its own width |
whole | bool | whether the width this value came from calls it a whole character |
Returns: success when the whole rendering was accepted
WriteEscapedScalar
pub func WriteEscapedScalar(
writer: &var TextWriter,
scalar: uint32
) -> ! FormatError
Writes one scalar as it appears inside a quoted rendering, escaping what would otherwise be ambiguous.
The quote, the backslash and the control characters get the escapes Rux/Text gives them everywhere else, so a
quoted character reads the same here as inside a quoted string.
Reports whatever the writer reported, and TextFailure(InvalidUtf8) for a value that is not a scalar, which a
caller checks before calling.
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
scalar | uint32 | the Unicode scalar value to write |
Returns: success when the whole character was accepted
MeasurePadding
pub func MeasurePadding(
spec: FormatSpec,
occupied: uint,
before: *var uint,
after: *var uint
)
Reports how much fill a spec asks for on each side of a rendering occupied characters wide.
A character slice cannot hand its rendering to WriteAligned, because the bytes do not exist until they are
transcoded and transcoding into a buffer would bound how long a slice may be. So the width is measured in one
pass over the units and the fill is written around the transcoding instead. Both counts are zero when the
rendering already fills the width, which is the common case and costs nothing.
Cannot fail; runs in constant time and allocates nothing.
Parameters
| Name | Type | Description |
|---|---|---|
spec | FormatSpec | what the placeholder asked for |
occupied | uint | how many characters the rendering will occupy |
before | *var uint | receives the fill to write first |
after | *var uint | receives the fill to write last |
FormatScalar
pub func FormatScalar(
writer: &var TextWriter,
spec: FormatSpec,
scalar: char32
) -> ! FormatError
Writes one character under spec's width, fill and alignment.
Reports TextFailure(InvalidUtf8) when scalar is not a Unicode scalar value, and otherwise whatever the
writer reported.
Parameters
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
spec | FormatSpec | what the placeholder asked for |
scalar | char32 | the Unicode scalar value to render |
Returns: success when the padded character was accepted