Bytes.slice
func Bytes.slice(b: Bytes, from: int, to: int): BytesBytes.slice returns a copy of the bytes of b from index from up to but excluding index to. It panics if from > to, or if either is outside 0 ..= Bytes.length(b).
Details
- Indices count bytes.
fromis inclusive andtois exclusive, sofrom == togives emptyBytes.
Basic examples
Take the bytes from index 1 up to but excluding index 4:
let png = Bytes.fromArray([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a])
Bytes.slice(png, 1, 4) // Bytes(50 4e 47)Take a prefix from index 0, or a suffix up to the length:
let b = Bytes.fromArray([1, 2, 3, 4, 5])
Bytes.slice(b, 0, 2) // Bytes(01 02)
Bytes.slice(b, 3, len(b)) // Bytes(04 05)Scope
Equal indices give empty Bytes, at any position up to the length, empty Bytes included:
let b = Bytes.fromArray([1, 2, 3])
b.slice(1, 1) // Bytes()
b.slice(3, 3) // Bytes()
String.toBytes("").slice(0, 0) // Bytes()Indices count bytes, not code points. In UTF-8, é takes the two bytes at indices 1 and 2:
let utf8 = String.toBytes("héllo")
utf8.slice(1, 3) // Bytes(c3 a9)
String.fromBytes(utf8.slice(1, 3)) // "é"The slice is Bytes like any other: it compares by contents, indexes to a byte, and works as a map key:
let b = Bytes.fromArray([0xca, 0xfe, 0xba, 0xbe])
b.slice(0, 2) == Bytes.fromArray([0xca, 0xfe]) // true
b.slice(2, 4)[0] // 186
let seen: Map[Bytes, string] = Map{}
seen[b.slice(0, 2)] = "cafe"
seen[Bytes.fromArray([0xca, 0xfe])] // "cafe"Call it through ., in a pipe, or with named arguments:
let b = String.toBytes("hello")
b.slice(1, 4) // Bytes(65 6c 6c)
b | Bytes.slice(1, 4) // Bytes(65 6c 6c)
Bytes.slice(b, from: 1, to: 3) // Bytes(65 6c)Applications
Read a big-endian unsigned integer from a range of a binary header:
func bigEndian(b: Bytes, from: int, to: int): int {
var n = 0
for x in b.slice(from, to) {
n = n * 256 + int(x)
}
n
}
let header = Bytes.fromArray([0x01, 0xbb, 0x00, 0x00, 0x00, 0x50])
println(bigEndian(header, 0, 2))
println(bigEndian(header, 2, 6))443
80Split at the first newline with Bytes.indexOf, into a record of the two parts:
type Frame = { head: Bytes, body: Bytes }
func frame(b: Bytes): Frame | null {
let i = b.indexOf(Bytes.fromArray([0x0a])) ?? return null
Frame{ head: b.slice(0, i), body: b.slice(i + 1, len(b)) }
}
let f = frame(String.toBytes("GET /\nHost: ant"))
if f != null {
println(String.fromBytes(f.head))
println(String.fromBytes(f.body))
}
println(frame(String.toBytes("no newline")))GET /
Host: ant
nullCut data into blocks of a fixed size, the last one shorter:
func blocks(b: Bytes, size: int): Array[Bytes] {
let out: Array[Bytes] = []
var i = 0
while i < len(b) {
out.push(b.slice(i, Math.min(i + size, len(b))))
i += size
}
out
}
for block in blocks(String.toBytes("abcdefg"), 3) {
println(Encoding.toHex(block))
}616263
646566
67Properties and relations
The result holds to - from bytes, and byte i of it is byte from + i of b:
let b = Bytes.fromArray([5, 6, 7, 8, 9])
let part = b.slice(1, 4)
assert(len(part) == 3)
assert(Array.range(0, len(part)).every({ part[x] == b[1 + x] }))Two slices that meet at the same index join back to b with Bytes.concat:
let b = String.toBytes("añt🐜")
let n = len(b)
for i in Iter.range(0, n + 1) {
assert(Bytes.concat([b.slice(0, i), b.slice(i, n)]) == b)
}It takes the same bytes that Array.slice takes from Bytes.toArray:
let b = Bytes.fromArray([10, 20, 30, 40, 50])
assert(b.slice(1, 4) == Bytes.fromArray(b.toArray().slice(1, 4)))Slicing a slice is one slice with the offsets added:
let b = Bytes.fromArray([1, 2, 3, 4, 5, 6])
assert(b.slice(1, 5).slice(1, 3) == b.slice(2, 4))Possible issues
An index past the end panics; Bytes.slice does not clamp it:
Bytes.fromArray([1, 2, 3]).slice(0, 5) // panic: Bytes.slice: the range 0..5 is not within 0 ..= 3A negative index panics rather than counting from the end:
Bytes.fromArray([1, 2, 3]).slice(-1, 3) // panic: Bytes.slice: the range -1..3 is not within 0 ..= 3from greater than to panics rather than giving empty Bytes:
Bytes.fromArray([1, 2, 3]).slice(2, 1) // panic: Bytes.slice: the range 2..1 is not within 0 ..= 3All three arguments are required; there is no form that runs to the end:
println(Bytes.fromArray([1, 2, 3]).slice(1)) // error: Bytes.slice needs an argument for toA byte range can cut a UTF-8 character in two. String.fromBytes then returns null, and String.slice counts code points instead:
let utf8 = String.toBytes("héllo")
String.fromBytes(utf8.slice(0, 2)) // null
"héllo".slice(0, 2) // "hé"See also
Bytes.length · Bytes.indexOf · Bytes.concat · Bytes.startsWith · Bytes.toArray · Array.slice · String.slice · String.fromBytes
