Octets is a string of octets, each of any value 0 to 255. It is called
Octets rather than Bytes because Bytes is already the quantity
dimension (8GiB). Use it for binary records, identities built from bytes,
and file or process content that need not be UTF-8 text.
Everything on this page is pure and needs VM 4 or later; the entries marked VM 7 need generation 7.
Making octets
| Written | Gives | Notes |
|---|---|---|
Octets fromHex: t |
Result[Octets] |
every pair of characters must be a hex digit, either case; fails with #HexParseError |
Octets fromUtf8: t |
Octets |
the text's UTF-8 encoding |
Octets fromU8: n |
Octets |
n unsigned, 1 octet |
Octets fromU16: n |
Octets |
unsigned big-endian, 2 octets |
Octets fromU32: n |
Octets |
unsigned big-endian, 4 octets |
Octets fromU64: n |
Octets |
unsigned big-endian, 8 octets; reaches 2^63 − 1, the largest i64 |
Octets leb128: n |
Octets |
unsigned LEB128 |
The integer encoders take i32 or i64, and a literal given to one is read
as i64, so Octets fromU32: 4294967295 can be written as it is. A value
that is negative or does not fit is a run-time fault (CAP-RUN-RANGE), not a
Result: encoding a length that does not fit is a bug in the program. Hex
from data, on the other hand, is input, so fromHex: is a Result.
Reading octets
| Written | Gives | Notes |
|---|---|---|
(o count) |
i32 |
the number of octets |
(o hex) |
Text |
lowercase, two digits an octet |
(o sha256) |
Octets |
the SHA-256 digest, 32 octets |
a append: b |
Octets |
a then b |
a = b, a != b |
bool |
octet for octet |
o at: i |
i32 |
the octet at i, 0 to 255; out of range faults with CAP-RUN-INDEX. VM 7 |
o sliceFrom: i to: j |
Octets |
a copy of the half-open range i to j; an empty range is allowed; a negative, reversed or out-of-range bound faults rather than clamps. VM 7 |
(o utf8Lossy) |
Text |
UTF-8 decoding, each invalid sequence replaced by U+FFFD. VM 7 |
(o latin1) |
Text |
each octet mapped to the code point of the same value (U+0000 to U+00FF). VM 7 |
There is no strict UTF-8 decoder in the prelude. readFileText refuses a
file that is not valid UTF-8; for octets already in hand,
(Octets fromUtf8: (o utf8Lossy)) = o is true exactly when o is valid
UTF-8.
An entry point may take and return Octets. captain run takes them as hex
(body=68656c6c6f) and prints them as octets 68656c6c6f.
Cost
Most builtins cost one VM instruction whatever they read. An octets operation costs one more for every 64 octets it reads (the bounded JSON operations are charged by input size too), so digesting or joining a large value is paid for in the caller's instruction budget.
Digests as text
(o sha256) is octets; ((o sha256) hex) is the usual lowercase hex. The
file effects return a Digest, a tagged value, and Text from: digest gives
the same lowercase hex. SHA-256 is the only digest the prelude computes.
Example
A length-prefixed record and its identity, the digest of a domain tag followed by the record:
module Example::Record.
export record kind: i32 body: Text -> Octets [
b := Octets fromUtf8: body.
ret ((Octets fromU8: kind) append: (Octets leb128: (b count))) append: b.
].
export identity kind: i32 body: Text -> Text [
tagged := (Octets fromUtf8: "example.record/1") append: (record kind: kind body: body).
ret ((tagged sha256) hex).
].
export header hex: Text -> Result[i32] [
(Octets fromHex: hex)
ifOk: [ :o | ret Ok { v: (o at: 0) } ]
ifFail: [ :e | ret Fail { err: e } ].
].$ captain build record.capt -o record.capb
built record.capb ([..] bytes, sha256 [..])
$ captain run record.capb record kind=7 body=hi
run log: record.runlog
octets 07026869
$ captain run record.capb identity kind=7 body=hi
run log: identity.runlog
"[..]"
$ captain run record.capb header hex=07026869
run log: header.runlog
Ok { v: 7 }
$ captain run record.capb header hex=0g
run log: header.runlog
Fail { err: Err { kind: #HexParseError, message: "not hexadecimal at position 0" } }
$ captain run record.capb header hex=
run log: header.runlog
error[CAP-RUN-INDEX]: byte index out of rangeThe last run faults rather than failing: indexing past the end is a bug in
the program, so it stops the run instead of producing a Fail value.