std/bytes
Immutable byte buffers and their Byte element type. Bytes is the FFI
boundary for Go []byte; convert to List<Byte> only when Nomi code needs
to inspect or transform individual bytes.
Import with import std/bytes.
Exports
Section titled “Exports”type Byte
Section titled “type Byte”type Byte
A single byte, 0 through 255.
Byte.from_int
Section titled “Byte.from_int”fn from_int(n: Int): Maybe<Byte>
type function on Byte
Converts an Int to a Byte when it is in range 0..255.
Interactive Tests
b = try Byte.from_int(65)
assert Byte.from_int(65) == Some(b)
assert Byte.from_int(-1) == None
assert Byte.from_int(256) == None
Byte.to_int
Section titled “Byte.to_int”fn to_int(b: Byte): Int
type function on Byte
Converts a Byte to its Int value.
Interactive Tests
b = try Byte.from_int(255)
assert Byte.to_int(b) == 255
Byte.to_string
Section titled “Byte.to_string”fn to_string(b: Byte): String
impl Display.to_string
Byte.inspect
Section titled “Byte.inspect”fn inspect(b: Byte): String
impl Debug.inspect
Byte.equal?
Section titled “Byte.equal?”fn equal?(a: Byte, b: Byte): Bool
impl Equatable.equal?
Byte.hash
Section titled “Byte.hash”fn hash(b: Byte): Int
impl Hashable.hash
type Bytes
Section titled “type Bytes”type Bytes
Immutable byte buffer.
Bytes.length
Section titled “Bytes.length”fn length(data: Bytes): Int
type function on Bytes
Number of bytes in the buffer.
Interactive Tests
a = try Byte.from_int(97)
b = try Byte.from_int(98)
assert Bytes.length(Bytes.from_list([a, b])) == 2
Bytes.at
Section titled “Bytes.at”fn at(data: Bytes, index: Int): Maybe<Byte>
type function on Bytes
Returns the byte at index, or None when index is out of range.
Interactive Tests
a = try Byte.from_int(97)
b = try Byte.from_int(98)
data = Bytes.from_list([a, b])
assert data |> Bytes.at(0) |> Maybe.map(Byte.to_int) == Some(97)
assert Bytes.at(data, 2) == None
Bytes.slice
Section titled “Bytes.slice”fn slice(data: Bytes, start: Int, end: Int): Bytes
type function on Bytes
Returns bytes from start index to end index (start inclusive, end exclusive). Out-of-range bounds are clamped.
Interactive Tests
a = try Byte.from_int(97)
b = try Byte.from_int(98)
c = try Byte.from_int(99)
d = try Byte.from_int(100)
assert Bytes.slice(Bytes.from_list([a, b, c, d]), 1, 3) == Bytes.from_list(
[b, c]
)
Bytes.concat
Section titled “Bytes.concat”fn concat(a: Bytes, b: Bytes): Bytes
type function on Bytes
Concatenates two byte buffers.
Interactive Tests
a = try Byte.from_int(97)
b = try Byte.from_int(98)
c = try Byte.from_int(99)
d = try Byte.from_int(100)
assert Bytes.concat(
Bytes.from_list([a, b]),
Bytes.from_list([c, d]),
) == Bytes.from_list([a, b, c, d])
Bytes.to_list
Section titled “Bytes.to_list”fn to_list(data: Bytes): List<Byte>
type function on Bytes
Materializes a byte buffer as a list of Byte values.
Interactive Tests
a = try Byte.from_int(97)
b = try Byte.from_int(98)
assert Bytes.from_list([a, b])
|> Bytes.to_list()
|> Iter.map(Byte.to_int)
|> Iter.to_list() == [97, 98]
Bytes.from_list
Section titled “Bytes.from_list”fn from_list(items: List<Byte>): Bytes
type function on Bytes
Builds a byte buffer from a list of Byte values.
Interactive Tests
a = try Byte.from_int(97)
b = try Byte.from_int(98)
assert Bytes.from_list([a, b]) |> Bytes.to_list() == [a, b]
Bytes.to_string
Section titled “Bytes.to_string”fn to_string(data: Bytes): Result<String, String>
type function on Bytes
Decodes bytes as UTF-8 text.
Interactive Tests
h = try Byte.from_int(104)
e = try Byte.from_int(195)
accent = try Byte.from_int(169)
assert Bytes.from_list([h, e, accent]) |> Bytes.to_string() == Ok("hé")
Bytes.each_while
Section titled “Bytes.each_while”fn each_while(data: Bytes, yield: (Byte) -> Bool): Bool
impl Iter.each_while
Pushes each byte to yield, stopping when it returns False.
Interactive Tests
a = try Byte.from_int(97)
b = try Byte.from_int(98)
assert Bytes.from_list([a, b])
|> Iter.map(Byte.to_int)
|> Iter.to_list() == [97, 98]
Bytes.add
Section titled “Bytes.add”fn add(lhs: Bytes, rhs: Bytes): Bytes
impl Add.add
Concatenates two byte buffers.
Interactive Tests
a = try Byte.from_int(97)
b = try Byte.from_int(98)
c = try Byte.from_int(99)
d = try Byte.from_int(100)
assert Bytes.from_list([a, b]) + Bytes.from_list([c, d]) == Bytes.from_list(
[a, b, c, d]
)
Bytes.inspect
Section titled “Bytes.inspect”fn inspect(data: Bytes): String
impl Debug.inspect
Interactive Tests
a = try Byte.from_int(97)
b = try Byte.from_int(98)
assert Debug.inspect(Bytes.from_list([a, b])) == "<<97, 98>>"
assert Debug.inspect(Bytes.from_list([])) == "<<>>"
Bytes.equal?
Section titled “Bytes.equal?”fn equal?(a: Bytes, b: Bytes): Bool
impl Equatable.equal?
Bytes.hash
Section titled “Bytes.hash”fn hash(data: Bytes): Int
impl Hashable.hash