Skip to content

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.

Types

type Byte

A single byte, 0 through 255.

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
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
fn to_string(b: Byte): String

impl Display.to_string

fn inspect(b: Byte): String

impl Debug.inspect

fn equal?(a: Byte, b: Byte): Bool

impl Equatable.equal?

fn hash(b: Byte): Int

impl Hashable.hash

type Bytes

Immutable byte buffer.

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
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
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]
)
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])
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]
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]
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é")
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]
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]
)
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([])) == "<<>>"
fn equal?(a: Bytes, b: Bytes): Bool

impl Equatable.equal?

fn hash(data: Bytes): Int

impl Hashable.hash