Hexadecimal v0.1.0

Source
Bytes as hexadecimal digits in either case, and a view that prints them.

HexCase

pub enum HexCase {
    Lower,
    Upper
}

Which case hexadecimal digits above nine are written in. Copy, carrying no payload. Decoding accepts both regardless.

Source

Cases

NameDescription
Lowera through f, the form most text formats and every cryptographic test vector use.
UpperA through F, the form RFC 4648 prints its alphabet in.

HexEncodedLength

pub func HexEncodedLength(length: uint) -> uint

How many characters the hexadecimal form of length bytes occupies: exactly twice as many. Cannot fail for any length a slice in memory can have. Runs in constant time and allocates nothing.

Source

Parameters

NameTypeDescription
lengthuintthe number of bytes to encode

Returns: the number of characters HexEncode writes

HexEncode

pub func HexEncode(
    bytes: byte[..],
    into: var char8[..],
    letterCase: HexCase
) -> uint ! FormatError

pub func HexEncode(
    text: char8[..],
    into: var char8[..],
    letterCase: HexCase
) -> uint ! FormatError

Writes bytes as hexadecimal digits into into, two per byte, high nibble first. Runs in time that depends only on the length of bytes. Reports InsufficientCapacity when into is shorter than HexEncodedLength(bytes.length), leaving it exactly as it was; room past the encoded form is not touched.

OverloadSummary
pub func HexEncode(bytes: byte[..], into: var char8[..], letterCase: HexCase) -> uint ! FormatErrorWrites bytes as hexadecimal digits into into, two per byte, high nibble first.
pub func HexEncode(text: char8[..], into: var char8[..], letterCase: HexCase) -> uint ! FormatErrorWrites the bytes of text as hexadecimal digits into into, two per byte.

Source

Parameters

NameTypeDescription
bytesbyte[..]the bytes to encode
intovar char8[..]the destination, at least HexEncodedLength(bytes.length) characters
letterCaseHexCasewhich case the digits above nine are written in
textchar8[..]the text whose bytes to encode

Returns: the number of characters written

HexDecodedLength

pub func HexDecodedLength(text: char8[..]) -> uint ! DecodeError

How many bytes the hexadecimal text decodes to, after checking that its length is even. Only the length is checked; the digits themselves are checked by HexDecode. Reports InvalidLength at the end of the text when it has an odd number of characters. Runs in constant time and allocates nothing.

Source

Parameters

NameTypeDescription
textchar8[..]the hexadecimal text

Returns: the number of bytes HexDecode writes for it

HexDecode

pub func HexDecode(
    text: char8[..],
    into: var byte[..]
) -> uint ! DecodeError

Reads hexadecimal digits of either case from text into into, one byte per pair. Runs in time that depends only on the length of text when the text is valid. Reports InvalidLength for an odd number of characters and InvalidCharacter at the first character that is not a hexadecimal digit, leaving into untouched in both cases, because the whole text is checked before anything is written. Reports InsufficientCapacity with the length needed when into is too short, also leaving it untouched.

Source

Parameters

NameTypeDescription
textchar8[..]the hexadecimal text to decode
intovar byte[..]the destination, at least HexDecodedLength(text) bytes

Returns: the number of bytes written

HexView

pub struct HexView {
    // private fields
}

A borrowed byte sequence that renders as hexadecimal under a format placeholder. Lets a caller write Render("{}", ViewHex(digest)) instead of sizing a buffer first. The view borrows the bytes and must not outlive them. Copy, owning nothing.

Source

Implements Display

HexView conforms to Display.

WriteDisplay

pub func WriteDisplay(
    self: &HexView,
    writer: &var TextWriter,
    spec: FormatSpec
) -> ! FormatError

Writes the bytes as hexadecimal digits, padded to the placeholder's width. The style x selects lower case and X upper case; with neither, the view's own case is used. The digits are produced in blocks, so rendering allocates nothing however long the bytes are. Reports whatever the writer reported, and UnsupportedRequest for a precision or any other style.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole rendering was accepted

Source

ViewHex

pub func ViewHex(
    bytes: byte[..],
    letterCase: HexCase
) -> HexView

Borrows bytes for rendering as hexadecimal. Cannot fail and allocates nothing; the view reads bytes each time it is rendered, so it must not outlive them.

Source

Parameters

NameTypeDescription
bytesbyte[..]the bytes to render
letterCaseHexCasethe case used when the placeholder does not name one

Returns: a view that renders bytes as hexadecimal