Bytes.truncate
func Bytes.truncate(b: Bytes, n: int): voidBytes.truncate keeps the first n bytes of b. It panics if n is outside 0 ..= Bytes.length(b).
Details
- It changes
bin place: every name that refers to the sameBytessees the shorter length. - It only shortens; to add bytes, use
Bytes.pushorBytes.extend. b.truncate(0)emptiesbso it can be filled again. The dropped bytes never come back: bytes added later are the new ones.
Basic examples
Keep the first three bytes:
let b = Bytes.fromArray([1, 2, 3, 4, 5])
Bytes.truncate(b, 3)
b // Bytes(01 02 03)Truncate to 0 to empty Bytes:
let b = String.toBytes("hello")
Bytes.truncate(b, 0)
b // Bytes()
len(b) // 0Scope
n equal to the length changes nothing, and empty Bytes truncates to 0:
let b = Bytes.fromArray([7, 8])
b.truncate(len(b))
b // Bytes(07 08)
let empty = Bytes.new()
empty.truncate(0)
empty // Bytes()b is shared by reference, so every name for it, a record field included, sees the change:
type Packet = { data: Bytes }
let b = String.toBytes("ping!")
let p = Packet{ data: b }
b.truncate(4)
p.data // Bytes(70 69 6e 67)
String.fromBytes(p.data) // "ping"
Bytes.same(b, p.data) // trueCall it through ., in a pipe, or with a named argument:
let b = String.toBytes("anthill")
b.truncate(6)
b | Bytes.truncate(5)
Bytes.truncate(b, n: 4)
b // Bytes(61 6e 74 68)A for loop follows the current length, so truncating inside the loop ends it early:
let b = Bytes.fromArray([1, 2, 3, 4])
for x in b {
println(x)
if x == byte(2) {
b.truncate(2)
}
}1
2Applications
Remove a trailing line break, \n or \r\n, in place:
func chomp(b: Bytes) {
if !b.endsWith(String.toBytes("\n")) {
return
}
b.truncate(len(b) - 1)
if b.endsWith(String.toBytes("\r")) {
b.truncate(len(b) - 1)
}
}
let line = String.toBytes("GET / HTTP/1.1\r\n")
chomp(line)
println(String.fromBytes(line))GET / HTTP/1.1Strip the zero bytes that pad a fixed-size field:
let field = Bytes.fromArray([0x61, 0x6e, 0x74, 0, 0, 0, 0, 0])
var n = len(field)
while n > 0 && field[n - 1] == byte(0) {
n -= 1
}
field.truncate(n)
println(String.fromBytes(field))antReuse one buffer for each record instead of making a new one every time:
let buf = Bytes.new()
for name in ["ant", "bee", "wasp"] {
buf.truncate(0)
buf.extend(String.toBytes(name))
buf.push(byte(0x0a))
println(Encoding.toHex(buf))
}616e740a
6265650a
776173700aUndo a partial write: remember the length, append, and truncate back when the result is too long:
func addField(b: Bytes, value: string, limit: int): bool {
let mark = len(b)
b.extend(String.toBytes(value))
b.push(byte(0x3b))
if len(b) > limit {
b.truncate(mark)
return false
}
true
}
let out = Bytes.new()
for v in ["id=7", "name=ant", "colony=formicidae"] {
if !addField(out, v, 16) {
println("skipped ${v}")
}
}
println(String.fromBytes(out))skipped colony=formicidae
id=7;name=ant;Properties and relations
Afterwards the length is n, and the bytes are those Bytes.slice takes from 0 to n:
let b = String.toBytes("añt🐜")
for n in Iter.range(0, len(b) + 1) {
let c = b.clone()
c.truncate(n)
assert(len(c) == n)
assert(c == b.slice(0, n))
}It undoes Bytes.extend: truncating back to the old length restores the old contents:
let b = Bytes.fromArray([1, 2, 3])
let before = b.clone()
b.extend(Bytes.fromArray([4, 5]))
b.truncate(len(before))
assert(b == before)Bytes added after a truncation are the new ones, not the bytes it dropped:
let b = Bytes.fromArray([1, 2, 3])
b.truncate(1)
b.push(byte(9))
assert(b == Bytes.fromArray([1, 9]))Possible issues
Bytes.truncate does not grow b. A length above the current one panics; use Bytes.extend or Bytes.push to add bytes:
let b = Bytes.fromArray([1, 2, 3])
b.truncate(4) // panic: Bytes.truncate needs a length within 0 ..= 3, got 4A negative length panics rather than counting from the end. To drop the last k bytes, pass len(b) - k:
let b = Bytes.fromArray([1, 2, 3])
b.truncate(-1) // panic: Bytes.truncate needs a length within 0 ..= 3, got -1It changes the Bytes every alias refers to. For a shorter copy that leaves b as it is, use Bytes.slice:
let original = String.toBytes("anthill")
let alias = original
alias.truncate(3)
original // Bytes(61 6e 74)
let copy = String.toBytes("anthill")
copy.slice(0, 3) // Bytes(61 6e 74)
copy // Bytes(61 6e 74 68 69 6c 6c)n counts bytes, not characters. Text from String.toBytes cut inside a multi-byte character is invalid UTF-8: String.fromBytes returns null, and String.fromBytesLossy puts U+FFFD in place of the cut character:
let b = String.toBytes("añt")
b.truncate(2)
b // Bytes(61 c3)
String.fromBytes(b) // null
String.fromBytesLossy(b) // "a�"An index that was valid before the truncation panics after it:
let b = Bytes.fromArray([1, 2, 3])
b.truncate(1)
b[0] // 1
b[2] // panic: index 2 out of range for Bytes of length 1See also
Bytes.pop · Bytes.slice · Bytes.extend · Bytes.push · Bytes.clone · Bytes.new · Bytes.length · Bytes
