Captain has two kinds of string. Text is a string of characters, for
names, messages, file contents and anything else meant to be read. Octets
is a string of octets (bytes), for binary records, digests and identities
built from bytes. They are different types, and one never turns into the
other without a written conversion. This chapter explains how each behaves;
the full lists of their operations are in the standard library:
Text, lists and logic
and Octets and digests.
Text
A Text value is a sequence of Unicode scalar values, held as UTF-8. A
string literal is a Text, and so is anything read from a text file, a
process's output or an HTTP body.
Text is a value
A text never changes. Every operation that "edits" one gives a new text and leaves the original as it was. So the result must be kept:
module Example::Edit.
export tidy t: Text -> Text [
t replace: "\t" with: " ".
ret t.
].The statement on line 4 does nothing, and the checker refuses it. Write
t := t replace: "\t" with: " ".
Joining text
Text is joined with append:. There is no + on text, and no ,:
module Example::Greeting.
export greet name: Text -> Text [
ret "Hello, " + name.
].+ means arithmetic in Captain, and only arithmetic. A program that adds
two things is adding numbers.
A number or other value becomes text with Text from:, and only then can it
be appended:
module Example::Text.
export greet name: Text -> Text [
ret ("Hello, " append: name) append: "!".
].
export label n: i32 -> Text [
ret "items: " append: (Text from: n).
].
export size t: Text -> i32 [
ret (t size).
].
export before a: Text b: Text -> bool [
ret a < b.
].
export characters t: Text -> List[Text] [
ret (t characters).
].
export count t: Text -> Result[i32] [
ret Int parse: (t trimmed).
].Members
A text's members are read in parentheses: (t size), (t isEmpty),
(t trimmed), (t lines), (t characters). Its methods take keyword
arguments: t contains: "x", t splitOn: ",", t replaceOnce: a with: b.
(t size) counts characters (Unicode scalar values), not bytes, and
(t characters) gives one text per scalar value. Neither knows about
grapheme clusters: a letter written with a combining accent is two
characters.
Order
Two texts compare with <, <=, > and >= by Unicode code point, which
is also the order of their UTF-8 bytes. It is not alphabetical order in any
language: every capital letter comes before every lower-case one. sorted
on a list of texts uses the same order. The order never depends on a
locale, so the same program sorts the same way everywhere.
$ captain build text.capt -o text.capb
built text.capb ([..] bytes, sha256 [..])
$ captain run text.capb greet name=Ada
run log: greet.runlog
"Hello, Ada!"
$ captain run text.capb label n=3
run log: label.runlog
"items: 3"
$ captain run text.capb size t=café
run log: size.runlog
4
$ captain run text.capb characters t=né
run log: characters.runlog
#( "n", "é" )
$ captain run text.capb before a=Zebra b=apple
run log: before.runlog
true
$ captain run text.capb count t=12
run log: count.runlog
Ok { v: 12 }Text from outside
Text that arrives from outside, such as a file, a command's output or an
argument, is just text until it is checked. It becomes a number with
Int parse: and a tagged value with that type's parse:, each giving a
Result that the program must take apart
(Option and Result). It never becomes a number,
a path or a URL by being used as one.
Octets
Octets is a sequence of octets, each 0 to 255, with no meaning attached.
It is called Octets, not Bytes, because Bytes is the quantity
dimension of sizes such as 8GiB.
Use Octets when the bytes themselves matter: building a binary record,
computing a digest, or handling content that need not be valid UTF-8.
Making and reading octets
There is no octets literal. Octets are made from text, from hex, or from integers:
| Written | Gives |
|---|---|
Octets fromUtf8: t |
the UTF-8 bytes of a text |
Octets fromHex: t |
Result[Octets]: hex from outside may be malformed |
Octets fromU32: n and the other widths |
an unsigned big-endian integer |
a append: b |
a followed by b |
(o count) |
the number of octets |
(o hex) |
lowercase hex text |
(o sha256) |
the SHA-256 digest, as 32 octets |
module Example::Octets.
export utf8 t: Text -> Octets [
ret Octets fromUtf8: t.
].
export byteCount t: Text -> i32 [
ret ((Octets fromUtf8: t) count).
].
export digest t: Text -> Text [
ret ((Octets fromUtf8: t) sha256 hex).
].
export decode hex: Text -> Result[Octets] [
ret Octets fromHex: hex.
].captain run prints octets, and takes them as arguments, in hex:
$ captain build octets.capt -o octets.capb
built octets.capb ([..] bytes, sha256 [..])
$ captain run octets.capb utf8 t=hi
run log: utf8.runlog
octets 6869
$ captain run octets.capb byteCount t=café
run log: byteCount.runlog
5
$ captain run octets.capb digest t=abc
run log: digest.runlog
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
$ captain run octets.capb decode hex=6g
run log: decode.runlog
Fail { err: Err { kind: #HexParseError, message: "not hexadecimal at position 0" } }café is four characters as text and five octets as UTF-8. The two types
keep that difference visible: a size is a count of characters or a count of
octets, and the type says which.
Why two types
Most languages either have one string type for both jobs, which makes every
byte-level operation a question of encoding, or a byte type that slides
into text by default. Captain keeps them apart because the places where
they meet are where mistakes happen: a digest computed over the wrong
encoding, a length in characters used as a length in bytes. Each crossing
is a call you can see: Octets fromUtf8: one way, and (o utf8Lossy) or
(o latin1) the other, each saying what it does with bytes that are not
valid UTF-8.
Octets are compared octet for octet with = and !=. Unlike most
built-in operations, an octets operation costs VM instructions in
proportion to its length, so a program that digests a large value pays for
it in its instruction budget
(Octets and digests).