6.29 Byte Strings
A byte string is a sequence of bytes (i.e., integers between 0 and 255 inclusive). 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.
bstr.length()
is
Bytes.length(bstr)
bstr.get(n)
is
Bytes.get(bstr, n)
bstr.set(n, byte)
is
Bytes.set(bstr, n, byte)
is
Bytes.append(bstr, bstr2, ...)
is
Bytes.subbytes(bstr, arg, ...)
bstr.copy()
is
Bytes.copy(bstr)
is
Bytes.copy_from(bstr, arg, ...)
is
Bytes.utf8_string(bstr, arg, ...)
is
Bytes.latin1_string(bstr, arg, ...)
is
Bytes.locale_string(bstr, arg, ...)
Byte strings are comparable, which means that generic operations like < and > work on byte strings.
annotation |
|
annotation |
|
annotation |
function | ||
> Bytes.make(5, 33)
Bytes.copy(#"!!!!!")
> Bytes.length(#"hello")
5
> #"hello".length()
5
> #"abc"[0]
97
> #"abc".get(0)
97
> b[0] := 104
> b
Bytes.copy(#"hbc")
> b.set(1, 104)
> b
Bytes.copy(#"hhc")
function |
fun Bytes.append(bstr :: Bytes, ...) :: MutableBytes |
> #"abc".append(#"def", #"ghi")
Bytes.copy(#"abcdefghi")
function | ||||
|
> Bytes.subbytes(#"hello", 2, 4)
Bytes.copy(#"ll")
> Bytes.subbytes(#"hello", 2)
Bytes.copy(#"llo")
function |
fun Bytes.copy(bstr :: Bytes) :: MutableBytes |
function | ||||||
|
function | |||||
| |||||
| |||||
function | |||||
| |||||
| |||||
function | |||||
|
> #"hello".utf8_string()
"hello"