Bytes.pop
func Bytes.pop(b: Bytes): byte | nullBytes.pop removes and returns the last byte of b, or returns null when b is empty.
Details
- Changes
bin place. Every name that shares it sees the shorterBytes.
Basic examples
Pop the last byte, and b is one byte shorter:
let b = Bytes.fromArray([1, 2, 3])
Bytes.pop(b) // 3
b // Bytes(01 02)Empty Bytes gives null:
let empty = Bytes.new()
Bytes.pop(empty) // null
empty // Bytes()Each call removes one more byte, until none is left:
let b = String.toBytes("hi")
b.pop() // 105
b.pop() // 104
b.pop() // nullScope
The result is a byte, not a one-byte Bytes. Slice before popping to keep the last byte as Bytes:
let b = String.toBytes("ant")
let n = b.length()
b.slice(n - 1, n) // Bytes(74)
b.pop() // 116Bytes from text are UTF-8, so a character outside ASCII comes off one byte at a time:
let b = String.toBytes("é")
b.pop() // 169
b // Bytes(c3)Bytes bound by let can still lose bytes, and every name that shares it sees the change, including the caller of a function that pops:
func dropLast(b: Bytes) {
b.pop()
}
let a = Bytes.fromArray([1, 2, 3])
let alias = a
dropLast(alias)
println(a)Bytes(01 02)A copy made with Bytes.clone or Bytes.slice is separate, so popping it leaves the original alone:
let original = Bytes.fromArray([1, 2, 3])
let copy = original.clone()
copy.pop() // 3
original.slice(0, 2).pop() // 2
original // Bytes(01 02 03)A for loop follows the current length, so popping inside it stops the loop earlier:
let b = Bytes.fromArray([1, 2, 3])
for x in b {
if x == byte(2) {
b.pop()
}
println(x)
}1
2In a pipe, the bytes are the first argument:
let b = Bytes.fromArray([0xca, 0xfe])
b | Bytes.pop() // 254
b // Bytes(ca)Applications
Bind the result and check it for null when empty Bytes needs its own handling. After the guard, the result is a byte:
func lastByte(b: Bytes): string {
let x = b.pop()
if x == null {
return "empty"
}
"last ${x}"
}
println(lastByte(String.toBytes("ant")))
println(lastByte(Bytes.new()))last 116
emptyDrop one trailing newline from a line read as bytes:
func chomp(line: Bytes) {
if line.endsWith(Bytes.fromArray([0x0a])) {
line.pop()
}
}
let line = String.toBytes("ant\n")
chomp(line)
println(String.fromBytes(line))antStrip the zero bytes that pad a fixed-size field:
func trimZeros(field: Bytes) {
while field.length() > 0 && field[field.length() - 1] == byte(0) {
field.pop()
}
}
let field = Bytes.fromArray([0x41, 0x42, 0, 0, 0])
trimZeros(field)
println(field)Bytes(41 42)Use Bytes as a stack of bytes: apply backspaces (byte 8) to typed input, where popping empty output does nothing:
func applyBackspaces(input: Bytes): Bytes {
let out = Bytes.new()
for x in input {
if x == byte(8) {
out.pop()
} else {
out.push(x)
}
}
out
}
let typed = Bytes.fromArray([8, 0x61, 0x6e, 0x78, 8, 0x74])
println(String.fromBytes(applyBackspaces(typed)))antDrain Bytes from the end with a while loop, and stop at null with ?? break:
let src = String.toBytes("abc")
let reversed = Bytes.new()
while true {
let x = src.pop() ?? break
reversed.push(x)
}
println(String.fromBytes(reversed))
println(src)cba
Bytes()Properties and relations
Bytes.pop returns the byte at the last index, and the length drops by one:
let b = Bytes.fromArray([4, 5, 6])
let n = b.length()
let last = b[n - 1]
assert(b.pop() == last)
assert(b.length() == n - 1)Bytes.pop undoes Bytes.push:
let b = Bytes.fromArray([1, 2])
b.push(byte(9))
assert(b.pop() == byte(9))
assert(b == Bytes.fromArray([1, 2]))On non-empty Bytes, what remains is what Bytes.truncate keeps with one byte fewer, and what Bytes.slice copies without the last byte:
let a = String.toBytes("anthill")
let b = a.clone()
let rest = a.slice(0, a.length() - 1)
a.pop()
b.truncate(b.length() - 1)
assert(a == b)
assert(a == rest)Possible issues
The result is byte | null, not byte, so using it as a byte is a compile error:
let b = Bytes.fromArray([7])
let x: byte = b.pop() // error: cannot use byte | null where byte is expectedGive a default with ?? when a byte is needed:
let b = Bytes.fromArray([7])
b.pop() ?? byte(0) // 7Bytes is not an array, so Array.pop does not take it:
let b = Bytes.fromArray([1])
println(Array.pop(b)) // error: cannot use Bytes where Array[T] is expectedPopping UTF-8 text removes a byte, not a character. Text whose last character takes more than one byte is no longer valid UTF-8, and String.fromBytes returns null:
let b = String.toBytes("café")
b.pop() // 169
String.fromBytes(b) // nullSee also
Bytes.push · Bytes.truncate · Bytes.length · Bytes.endsWith · Bytes.slice · Bytes.clone · Array.pop · Bytes
