Std. Binary
(Cure Standard Library v0.37.2)
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']
Summary
Functions
The byte (0–255) at zero-based index; delegates to :binary.at/2.
Number of bytes in b; delegates to :erlang.byte_size/1.
Drop the first n bytes of b, returning the remaining suffix.
Decode a UTF-8 Binary back into its List(Char) of code points;
delegates to :unicode.characters_to_list/1.
Keep Binary independent of Std.Nat in the stdlib dependency graph.
Byte-granular primitives. These back the byte-syntax surface forms
<<b1, b2, …>> (construction) and <<b, rest::binary>> (pattern): the
elaborator desugars byte segments to of_bytes and byte_at/drop_bytes
guarded by byte_size. Sized/typed bit segments (x:16, x/float) are a
separate future extension; these cover the default 8-bit-integer case.
The sub-binary [start, start + len); delegates to :binary.part/3.
Encode a List(Char) as its UTF-8 Binary. The list erases to a list of
code-point ints, which :unicode.characters_to_binary/1 renders to a
binary; delegates to that BIF.
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.
Byte view starting at index, taking remaining more bytes.
Functions
fn byte_at(b: Binary, index: Int) -> Int
The byte (0–255) at zero-based index; delegates to :binary.at/2.
fn byte_size(b: Binary) -> Int
Number of bytes in b; delegates to :erlang.byte_size/1.
fn drop_bytes(b: Binary, n: Int) -> Binary
Drop the first n bytes of b, returning the remaining suffix.
fn from_binary(b: Binary) -> List(Char)
Decode a UTF-8 Binary back into its List(Char) of code points;
delegates to :unicode.characters_to_list/1.
fn nat_of_int(i: Int) -> Nat
Keep Binary independent of Std.Nat in the stdlib dependency graph.
fn of_bytes(bytes: List(Int)) -> Binary
Byte-granular primitives. These back the byte-syntax surface forms
<<b1, b2, …>> (construction) and <<b, rest::binary>> (pattern): the
elaborator desugars byte segments to of_bytes and byte_at/drop_bytes
guarded by byte_size. Sized/typed bit segments (x:16, x/float) are a
separate future extension; these cover the default 8-bit-integer case.
Pack a List(Int) of bytes (0–255) into a Binary; delegates to
:erlang.list_to_binary/1.
fn part(b: Binary, start: Int, len: Int) -> Binary
The sub-binary [start, start + len); delegates to :binary.part/3.
fn to_binary(cs: List(Char)) -> Binary
Encode a List(Char) as its UTF-8 Binary. The list erases to a list of
code-point ints, which :unicode.characters_to_binary/1 renders 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)
Byte view starting at index, taking remaining more bytes.
The fuel-carrying worker behind to_bytes/1: each step conses one byte and
decrements the Nat fuel, so the recursion is structurally decreasing and
needs no Std.List dependency.