Std.Binary
View source →The bridge between Cure's two string representations.
A List(Char) is a cons spine of code points — the value-surface
representation you pattern-match, map, and fold like any other list.
String stores its text this way internally (characters/from_characters
in Std.String), but String itself is nominal, not a List(Char) alias.
A Binary is the BEAM binary. It is what OTP's :string, :binary,
and :erlang string functions consume and produce (Unicode case folding,
integer_to_binary, binary:split, …). Cure keeps it as a primitive type.
to_binary / from_binary convert between them so a List(Char) program
can reach an OTP binary primitive and come back. Conversion is UTF-8:
Char erases to its code point, so a List(Char) erases to a list of
code-point ints, which :unicode.characters_to_binary/1 encodes.
Examples
use Std.Binary
to_binary(['h', 'i']) # => "hi" (the Binary <<"hi">>)
from_binary("hi") # => ['h', 'i']
The BEAM binary as a primitive base type — its visible, inspectable home.
Functions
-
# fn byte_at(b: Binary, index: Int) -> Int extern
The byte (0–255) at zero-based
index; delegates to:binary.at/2. -
# fn byte_size(b: Binary) -> Int extern
Number of bytes in
b; delegates to:erlang.byte_size/1. -
# fn drop_bytes(b: Binary, n: Int) -> Binary
Drop the first
nbytes ofb, returning the remaining suffix. -
# fn from_binary(b: Binary) -> List(Char) extern
Decode a UTF-8
Binaryback into itsList(Char)of code points; delegates to:unicode.characters_to_list/1. -
# fn nat_of_int(i: Int) -> Nat extern
Keep Binary independent of Std.Nat in the stdlib dependency graph.
-
# fn of_bytes(bytes: List(Int)) -> Binary extern
Byte-granular primitives. These back the byte-syntax surface forms
<<b1, b2, …>>(construction) and<<b, rest::binary>>(pattern): the elaborator desugars byte segments toof_bytesandbyte_at/drop_bytesguarded bybyte_size. Sized/typed bit segments (x:16,x/float) are a separate future extension; these cover the default 8-bit-integer case. Pack aList(Int)of bytes (0–255) into aBinary; delegates to:erlang.list_to_binary/1. -
# fn part(b: Binary, start: Int, len: Int) -> Binary extern
The sub-binary
[start, start + len); delegates to:binary.part/3. -
# fn to_binary(cs: List(Char)) -> Binary extern
Encode a
List(Char)as its UTF-8Binary. The list erases to a list of code-point ints, which:unicode.characters_to_binary/1renders to a binary; delegates to that BIF. -
# fn to_bytes(b: Binary) -> List(Int)
The eager byte view used by
for <<b <- binary>>. It recurses over a Nat fuel value so Binary stays below the Std.List auto-prelude dependency edge. -
# fn to_bytes_from(b: Binary, index: Int, remaining: Nat) -> List(Int)