Bytes.length
func Bytes.length(b: Bytes): intBytes.length returns the number of bytes in b.
Details
- Counts bytes, not characters. Empty
Byteshas length0. len(b)gives the same count.
Basic examples
Count the bytes of a string in UTF-8:
Bytes.length(String.toBytes("ant")) // 3Count the bytes of a constant built from integers:
let png = Bytes.fromArray([0x89, 0x50, 0x4e, 0x47])
Bytes.length(png) // 4Empty Bytes has length 0:
Bytes.length(String.toBytes("")) // 0
Bytes.length(Bytes.concat([])) // 0Scope
A character outside ASCII takes two to four bytes in UTF-8:
String.toBytes("é").length() // 2
String.toBytes("日本語").length() // 9
String.toBytes("🐜").length() // 4A string escape writes a code point, not a byte, so "\u{89}" is two bytes:
String.toBytes("\u{89}").length() // 2
Bytes.fromArray([0x89]).length() // 1Decoded bytes have one byte per two hexadecimal digits, and three per four Base64 characters:
Encoding.fromHex("cafe").unwrap().length() // 2
Encoding.fromBase64("YW50").unwrap().length() // 3A slice has as many bytes as its range is wide:
let b = String.toBytes("anthill")
b.slice(3, 7).length() // 4
b.slice(3, 3).length() // 0Call it through ., in a pipe, or pass it as a function value:
let b = String.toBytes("anthill")
b.length() // 7
b | Bytes.length() // 7
[String.toBytes("a"), String.toBytes("bc")].map(Bytes.length) // [1, 2]Applications
Check that a payload fits a limit on bytes, such as a field stored in 16 bytes:
func fits(name: string): bool {
String.toBytes(name).length() <= 16
}
fits("anthill") // true
fits("муравейник") // false
"муравейник".length() // 10Take the last four bytes, such as a checksum stored at the end of a block:
func trailer(b: Bytes): Bytes {
let n = b.length()
b.slice(n - 4, n)
}
trailer(Bytes.fromArray([1, 2, 3, 4, 5, 6])) // Bytes(03 04 05 06)Check the length before reading a byte at a fixed offset:
func version(b: Bytes): int | null {
if b.length() < 5 {
return null
}
int(b[4])
}
version(Bytes.fromArray([0x89, 0x50, 0x4e, 0x47, 2, 0])) // 2
version(Bytes.fromArray([0x89, 0x50])) // nullAdd up the sizes of several pieces before joining them:
let parts = ["ant", "hill", "🐜"].map(String.toBytes)
parts.map(Bytes.length).sum() // 11
Bytes.concat(parts).length() // 11Properties and relations
len and Bytes.length give the same count:
for s in ["", "ant", "日本語"] {
let b = String.toBytes(s)
assert(len(b) == Bytes.length(b))
}The length of a concatenation is the sum of the lengths:
let a = String.toBytes("ant")
let b = Bytes.fromArray([0, 255])
assert(Bytes.concat([a, b]).length() == a.length() + b.length())A slice from from to to has to - from bytes:
let b = String.toBytes("anthill")
for r in [[0, 0], [1, 4], [0, 7]] {
assert(b.slice(r[0], r[1]).length() == r[1] - r[0])
}Bytes.toArray and Bytes.fromArray keep the length:
let b = String.toBytes("héllo")
assert(len(b.toArray()) == b.length())
assert(Bytes.fromArray([1, 2, 3]).length() == 3)A string has at least as many bytes as code points:
for s in ["ant", "héllo", "🐜"] {
assert(String.toBytes(s).length() >= s.length())
}Possible issues
The byte count of a string is not its length in characters. len on a string counts code points too, not bytes. Convert with String.toBytes first:
let s = "日本語"
String.length(s) // 3
len(s) // 3
Bytes.length(String.toBytes(s)) // 9
len(String.toBytes(s)) // 9The length is one past the last index, so indexing at it panics:
let b = String.toBytes("ant")
b[b.length() - 1] // 116
b[b.length()] // panic: index 3 out of range for Bytes of length 3A string is not Bytes, so passing one is a compile error. Convert it with String.toBytes:
println(Bytes.length("ant")) // error: cannot use string where Bytes is expectedArithmetic on the length can go below zero. Taking the last four bytes of shorter data panics, so check b.length() >= 4 first:
func trailer(b: Bytes): Bytes {
let n = b.length()
b.slice(n - 4, n)
}
trailer(Bytes.fromArray([1, 2])) // panic: Bytes.slice: the range -2..2 is not within 0 ..= 2See also
Bytes.slice · Bytes.concat · Bytes.toArray · Bytes.fromArray · String.toBytes · String.length · Array.length · Bytes
