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 | |
| |
annotation | |
| |
annotation | |
Static information associated by Bytes, etc., makes an expression acceptable as a sequence to for in static mode.
function | ||
> Bytes.make(5, Byte#"!")
Bytes.copy(#"!!!!!")
5
> Bytes.length(#"hello")
5
> #"abc"[0]
97
97
> b
Bytes.copy(#"hbc")
> b
Bytes.copy(#"hhc")
method | |
| |
| |
function | |
|
Bytes.copy(#"abcdefghi")
method | |||
| |||
method | |||
When given one argument, rge is used to derive start and end as in String.substring.
Bytes.copy(#"ll")
Bytes.copy(#"llo")
Bytes.copy(#"llo")
Bytes.copy(#"hell")
Bytes.copy(#"hello")
Bytes.copy(#"apple")
#true
method | ||||||
|
method | |
|
> b
Bytes.copy(#"xxxxx")
method | |||||
| |||||
| |||||
method | |||||
| |||||
| |||||
method | |||||
|
> #"hello".utf8_string()
"hello"
> #"hi \316\273".utf8_string()
"hi λ"
> #"hi \316 xxx \273".utf8_string()
Bytes.utf8_string: byte string is not a well-formed UTF-8 encoding
byte string: Bytes.copy(#"hi \316 xxx \273")
> #"hi \316 xxx \273".utf8_string(Char"?")
"hi ? xxx ?"
method | ||||||
| ||||||
| ||||||
method | ||||||
| ||||||
| ||||||
method | ||||||
|
The Bytes.utf8_length method returns the length in characters of the decoded substring from start to end.
The Bytes.utf8_ref method returns the character decoded in substring from start to end, skipping skip decoded characters.
The Bytes.utf8_index method returns the byte index starting the character that would be decoded in substring from start to end, skipping skip decoded characters. Note that a non-#false result does not mean that bytes at the returned index are well formed as UTF-8, only that the bytes up to the returned index are available and well-formed. The returned index is relative to the start of bstr, not to start, and it is always less than end.
> #"hi \316\273".utf8_length()
4
> #"hi \316 xxx \273".utf8_length()
#false
> #"hi \316 xxx \273".utf8_length(Char"?")
10
Char"h"
Char"λ"
> #"hi \316\273".utf8_index(0)
0
> #"hi \316\273!".utf8_index(4)
5