Skip to reference

std.hex

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: core

Module

std.hex — base-16 encoding and decoding. Stability tier: core.

**The byte sequence is List[byte], and the interim this header carried for eleven sprints is DISCHARGED (sc35).** The currency was List[int] with every element in 0..255 — documented, checked at the boundary, and promised to be "trivially replaced when the type exists, since every signature keeps its shape" (F-0035). It was, and they did: D72/s135 gave the language byte and s136 (wolf-lang#231) moved the eight byte builtins to List[byte], so encode and decode took the new element type and nothing else here moved. std.bytes is the module to reach for when a program wants to LOOK at those bytes rather than encode them.

What changed in the bodies, and it is two lines. encode reads an element and hands it to byte_digits, which takes an int, so the read is spelled b[i] as int ([type.byte.cast] — widening is never implicit); decode pushes (hi * 16 + lo) as byte, because [type.byte.op] makes every arithmetic result an int and narrowing back is spelled. **byte_digits' assert on an element outside 0..255 is now UNREACHABLE from every door in this module** — the type forbids the input — and the guard stays, because it is the function's own contract and the one caller that could still violate it is a future one. §11's encoder rule (an encoder TRAPS on a non-byte where an os module RAISES) is not retired; it is simply out of reach here.

What that used to cost was one function, and sc10 paid it: **encode_str is here now.** Hex-encoding text is the commonest use of this module in any language and it was missing for four sprints because nothing could read a str's bytes (F-0035); s.bytes() can, on every lane (s37, and std.bytes.from_str wraps it), so the function is one delegation.

**And decode_str is here now too, which closes §11's encoder/decoder pairing for this module.** It was a refusal for four sprints because nothing in the language turned bytes back into a str; s81's validating str_from_utf8 closed F-0057 at the sc13 pin and std.bytes.to_str wraps it, so §14's debt clause — a contract ships in the sprint AFTER its blocker closes — comes due here and is paid. The module has no reviewed contract left.

One name note: encode_str and not encode, because wolf has no overloading and encode(b: List[byte]) owns the short name. It goes to the byte form deliberately — that is the operation the round-trip property is stated over.

Round-tripping is the doctrine (the sprint contract's word): every encoder here ships beside its decoder, decode(encode(b)) is b for every byte sequence, and tests/hex/round_trip.lu holds the property over the shared adversarial fixtures rather than over a happy path.

Decoding is TOTAL over arbitrary UTF-8 input, and at the sc12 pin it is total for a simpler reason than the one it shipped with. sc03's inversion — probe the input with an ASCII literal this module owns, never slice the caller's string at a guessed offset — is what made decode("é") a parse miss rather than a bounds trap while the language had no byte accessor (F-0018). s77 gives the digit probe the byte itself (s.bytes()[0], one load rather than up to thirty-two starts_with calls), and a byte cannot be a partial code point, so the totality argument is now "a byte is a byte" and the answers are unchanged member for member. tests/hex/decode_misses.lu is what holds them to that.

Types and traits

No public declarations in this section.

Functions and methods

encode

fn · Source line 135

pub fn encode(b: List[byte]) -> str

b as lowercase hexadecimal, two digits per byte, no separators and no 0x prefix. The empty sequence encodes to the empty string.

Traps assert when an element is outside 0..255: the caller promised bytes and did not deliver them, which is a contract violation and not a datum (§2's trap rule). Total otherwise: no error row.

var b = List[byte]()
(mut b).push(0 as byte)
(mut b).push(15 as byte)
(mut b).push(255 as byte)
hex.encode(b) == "000fff"
var empty = List[byte]()
hex.encode(empty) == ""

encode_upper

fn · Source line 157

pub fn encode_upper(b: List[byte]) -> str

encode with uppercase digits ("000FFF"). The uppercasing is the implementation's upper over ASCII hex digits, where no Unicode case-mapping question arises. Same assert trap on a non-byte.

var b = List[byte]()
(mut b).push(171 as byte)
(mut b).push(205 as byte)
hex.encode_upper(b) == "ABCD"

decode

fn · Source line 189

pub fn decode(s: str) -> List[byte] ! {parse}

s decoded from hexadecimal; parse when the length is odd or any character is not a hex digit. Upper and lower case both decode, and they may be mixed. The empty string decodes to the empty sequence.

The tag is parse — sc03's "these bytes are not a value of that type" — and not the hex the sprint contract spells, for a reason worth knowing before naming any tag: a row tag that shares a name with a module in scope at the raise site resolves to the MODULE and rides out of the function as a value, silently, with no diagnostic and no else firing (F-0036). std.hex raising hex was the shape that found it. The two miss cases an even more precise row would separate — odd length and bad digit — wait for sc06's payload conventions rather than being spelled as two bare tags.

Total: every input either decodes or misses — no traps, including on non-ASCII input, which is a miss like any other non-digit.

let b = hex.decode("00ff") else List[byte]()
b.len == 2
let bad = hex.decode("0f0") else List[byte]()
bad.len == 0
let mixed = hex.decode("AbCd") else List[byte]()
mixed.len == 2
let unicode = hex.decode("é") else List[byte]()
unicode.len == 0

encode_str

fn · Source line 251

pub fn encode_str(s: str) -> str

The UTF-8 bytes of s, hex-encoded — lowercase, two digits per byte, no separators, no 0x prefix.

Total: no rows, no traps. The empty string encodes to the empty string. Every wolf str is well-formed UTF-8 by definition, so there is no input this can refuse and no encoding parameter to get wrong.

The census row this closes. Hex-encoding text is the commonest use of a hex encoder anywhere and this module shipped without it for four sprints, because nothing in the language could read a str's bytes (F-0035). s37's s.bytes() can, on every lane, and the gap was a debt rather than a wall after sc09 measured it.

**The body was encode(s.bytes()) until sc12 and is a walk now.** Passing the view to encode is exactly the position s77 materializes in — an argument — so the delegation copied the whole string into a List[byte] before rendering a digit. Consuming the view here costs the duplication of one four-line loop and no longer allocates a list per call. The digits still come from byte_digits, so there is one rendering and the two entry points cannot drift; what is duplicated is the LOOP, not the answer.

The assert encode documents is unreachable through this door: every element of a str's byte view is a byte by construction.

**Its inverse is decode_str, below**, which arrived at sc14 when §14's debt clause came due on s81's str_from_utf8. §11's encoder/decoder pairing rule is satisfied for this module: every encoder here ships beside its decoder.

Uppercase is encode_str(s).upper(); there is no encode_upper_str, because a second name for a call anyone can write is a second thing to keep in agreement (§1).

hex.encode_str("") == ""
hex.encode_str("wolf") == "776f6c66"
hex.encode_str("é") == "c3a9"
hex.encode_str("\n") == "0a"

decode_str

fn · Source line 295

pub fn decode_str(s: str) -> str ! {parse, utf8}

s decoded from hexadecimal and read back as text — encode_str's inverse, and the round trip §11 requires of every encoder here.

Two tags, and they are two different failures. parse is decode's: the text is not hexadecimal (an odd length, or a character that is not a hex digit). utf8 is std.bytes.to_str's: the text WAS hexadecimal and the bytes it denotes are not well-formed UTF-8, which is what "ff" decodes to. Coarsening them into one tag would tell a caller "that was not hex" about a document that was, so the row is the union and neither tag is renamed (§14's verbatim-row rule, and §12's). Case is not significant and may be mixed, exactly as in decode.

Total: every input either decodes or misses — no traps, on any input, including non-ASCII (a miss like any other non-digit).

The census row this closes, and the clause that made it due. A hex decoder that cannot answer in text was half a module for four sprints, and the reason was never this module's: nothing in the language turned a List[int] back into a str (F-0057). s81's validating str_from_utf8 closed it at the sc13 pin, so §14's debt clause — a contract ships in the sprint AFTER its blocker closes — fell due here. The body is decode plus one call, which is what the contract always said it would be.

Three lanes: str_from_utf8 is the compiler's prelude AND, as of lupin 0.1.12, the interpreter's (F-0075 closed).

(hex.decode_str("776f6c66") else "?") == "wolf"
(hex.decode_str("") else "?") == ""
(hex.decode_str("c3a9") else "?") == "é"
(hex.decode_str("C3A9") else "?") == "é"
(hex.decode_str("zz") else "not hex") == "not hex"
(hex.decode_str("ff") else "not text") == "not text"

Constants

No public declarations in this section.

Errors and row tags

Tags named by public signatures: parse, utf8. Their conditions are documented on the functions that return them.

No public declarations in this section.