On this page:
Bytes
Mutable  Bytes
Immutable  Bytes
Bytes.make
Bytes.length
Bytes.get
Bytes.set
Bytes.append
Bytes.subbytes
Bytes.copy
Bytes.copy_  from
Bytes.fill
Bytes.snapshot
Bytes.utf8_  string
Bytes.latin1_  string
Bytes.locale_  string
Bytes.to_  sequence
8.15.0.12

7.7 Byte Strings🔗ℹ

A byte string is a sequence of bytes. A byte string works with map-referencing [] to access a byte via #%index. A byte string also works with the ++ operator to append bytes strings. A byte string can be used as sequence, in which case it supplies its bytes in order.

A byte string is normally mutable, but byte-string literals are immutable. The Bytes annotation is satisfied by both mutable and immutable byte strings, while MutableBytes and ImmutableBytes require one or the other.

Two byte strings are equal by is_now as long as they have equal contents, even if one is mutable and the other is immutable.

Byte strings are comparable, which means that generic operations like < and > work on byte strings.

annotation

Bytes

 

annotation

MutableBytes

 

annotation

ImmutableBytes

Matches byte strings, where MutableBytes matches only mutable byte strings, and and ImmutableBytes matches only immutable byte strings.

Static information associated by Bytes, etc., makes an expression acceptable as a sequence to for in static mode.

function

fun Bytes.make(length :: NonnegInt, byte :: Byte = 0)

  :: MutableBytes

Creates a fresh byte string with length bytes, where each byte is initialized to byte.

> Bytes.make(5, Byte#"!")

Bytes.copy(#"!!!!!")

method

method (bstr :: Bytes).length() :: NonnegInt

Returns the number of bytes in bstr.

> #"hello".length()

5

> Bytes.length(#"hello")

5

method

method (bstr :: Bytes).get(n :: NonnegInt) :: Byte

Equivalent to bstr[n] (with the default implicit #%index form). Returns the nth byte of bstr (starting from 0).

> #"abc"[0]

97

> #"abc".get(0)

97

method

method (bstr :: Bytes).set(n :: NonnegInt, byte :: Byte)

  :: Void

Equivalent to bstr[n] := byte (with the default implicit #%index form). Updates the nth position of bstr to byte.

> def b = #"abc".copy()

> b[0] := Byte#"h"

> b

Bytes.copy(#"hbc")

> b.set(1, Byte#"h")

> b

Bytes.copy(#"hhc")

method

method (bstr :: Bytes).append(bstr :: Bytes, ...) :: MutableBytes

 

function

fun Bytes.append(bstr :: Bytes, ...) :: MutableBytes

Appends bstrs by creating a new mutable byte string with all bytes.

> #"abc".append(#"def", #"ghi")

Bytes.copy(#"abcdefghi")

method

method (bstr :: Bytes).subbytes(start :: NonnegInt,

                                end :: NonnegInt = Bytes.length(bstr))

  :: MutableBytes

Returns the substring of bstr from start (inclusive) to end (exclusive).

> #"hello".subbytes(2, 4)

Bytes.copy(#"ll")

> #"hello".subbytes(2)

Bytes.copy(#"llo")

method

method (bstr :: Bytes).copy(bstr :: Bytes) :: MutableBytes

Returns a fresh mutable byte string with the same initial content as bstr.

> def b = #"apple"

> b.copy()

Bytes.copy(#"apple")

> b.copy() is_now b

#true

function

fun Bytes.copy_from(dest_bstr :: MutableBytes,

                    dest_start :: NonnegInt,

                    src_bstr :: Bytes,

                    src_start :: NonnegInt = 0,

                    src_end :: NonnegInt = Bytes.length(src_bstr))

  :: Void

Copies bytes from src_bstr at src_start (inclusive) to src_end (exclusive) into dest_bstr starting at dest_start. The length of dest_bstr must be at least dest_start + (src_end - src_start).

method

method Bytes.fill(bstr :: MutableBytes, byte :: Byte) :: Void

Sets every byte in bstr to byte.

> def b = #"apple".copy()

> b.fill(Byte#"x")

> b

Bytes.copy(#"xxxxx")

method

method (bstr :: Bytes).snapshot(str :: Bytes) :: ImmutableBytes

Returns an immutable byte string as-is or copies a mutable byte string’s content to an immutable byte string.

> def b = #"apple"

> b.snapshot()

#"apple"

> b.snapshot() === b

#true

> def b = #"apple".copy()

> b.snapshot()

#"apple"

> b.snapshot() is_now b

#true

method

method (bstr :: Bytes).utf8_string(

  err_char :: maybe(Char) = #false,

  start :: NonnegInt = 0,

  end :: NonnegInt = Bytes.length(bstr)

) :: String

 

method

method (bstr :: Bytes).latin1_string(

  err_char :: maybe(Char) = #false,

  start :: NonnegInt = 0,

  end :: NonnegInt = Bytes.length(bstr)

) :: String

 

method

method (bstr :: Bytes).locale_string(

  err_char :: maybe(Char) = #false,

  start :: NonnegInt = 0,

  end :: NonnegInt = Bytes.length(bstr)

) :: String

Converts a byte string to a string, decoding as UTF-8, Latin-1, or the current locale’s encoding. The err_char argument provides a character to use in place of an encoding error, where #false means that an exception is thrown.

> #"hello".utf8_string()

"hello"

method

method (bstr :: Bytes).to_sequence(bstr :: Bytes) :: Sequence

Implements Sequenceable by returning a sequence of bstr’s bytes in order.