Bytes.push
func Bytes.push(b: Bytes, x: byte): voidBytes.push appends x to b.
Details
Bytes.pushchangesbin place. Every name for the sameBytessees the new byte.- An integer literal is an
int, so writebyte(65). - A
forloop overbvisits bytes pushed during the loop.
Basic examples
Append bytes to empty Bytes:
let b = Bytes.new()
Bytes.push(b, byte(0x41))
Bytes.push(b, byte(0xff))
b // Bytes(41 ff)
len(b) // 2Append a byte after the UTF-8 bytes of a string:
let line = "hi".toBytes()
Bytes.push(line, byte(0x0a))
line // Bytes(68 69 0a)Scope
Push onto Bytes that already holds bytes, from any source:
let magic = Bytes.fromArray([0x89, 0x50])
magic.push(byte(0x4e))
magic // Bytes(89 50 4e)A byte read from other Bytes, or from a byte variable, goes in as it is:
let src = "Ant".toBytes()
let dst = Bytes.new()
dst.push(src[0])
let zero = byte(0)
dst.push(zero)
dst // Bytes(41 00)Bytes is shared by reference, so a push through one name shows through every other:
let b = Bytes.new()
let alias = b
alias.push(byte(7))
b // Bytes(07)
Bytes.same(alias, b) // trueA function that pushes onto its parameter changes the caller's Bytes:
func terminate(b: Bytes): void {
b.push(byte(0))
}
let name = "ant".toBytes()
terminate(name)
println(name)Bytes(61 6e 74 00)Applications
Encode an unsigned integer as a variable-length integer, seven bits per byte with the high bit set on every byte but the last:
func varint(n: int): Bytes {
let out = Bytes.new()
var v = n
while v >= 128 {
out.push(byte(v % 128 + 128))
v /= 128
}
out.push(byte(v))
out
}
println(varint(1))
println(varint(300))
println(varint(16384))Bytes(01)
Bytes(ac 02)
Bytes(80 80 01)Append a one-byte checksum, the XOR of every byte before it:
func withChecksum(payload: Bytes): Bytes {
let out = payload.clone()
var sum = byte(0)
for x in payload {
sum = Bits.xor(sum, x)
}
out.push(sum)
out
}
let frame = withChecksum(Bytes.fromArray([0x01, 0x02, 0x04]))
println(Encoding.toHex(frame))01020407Keep only the printable ASCII bytes of some input:
let input = Bytes.fromArray([0x41, 0x00, 0x6e, 0x07, 0x74])
let clean = Bytes.new()
for x in input {
if x >= byte(0x20) && x < byte(0x7f) {
clean.push(x)
}
}
println(String.fromBytes(clean))AntProperties and relations
Bytes.push adds one byte at the end and leaves the others as they were:
let b = Bytes.fromArray([1, 2, 3])
let before = b.clone()
b.push(byte(9))
assert(len(b) == len(before) + 1)
assert(b[len(b) - 1] == byte(9))
assert(b.slice(0, len(before)) == before)Bytes.pop undoes it:
let b = "ant".toBytes()
let before = b.clone()
b.push(byte(0x21))
assert(b.pop() == byte(0x21))
assert(b == before)It is Bytes.extend with one byte:
let a = Bytes.fromArray([1, 2])
let b = a.clone()
a.push(byte(3))
b.extend(Bytes.fromArray([3]))
assert(a == b)When the size is known, Bytes.new and assignment by index build the same bytes:
let pushed = Bytes.new()
let sized = Bytes.new(3)
for i in Iter.range(0, 3) {
pushed.push(byte(i * 10))
sized[i] = byte(i * 10)
}
assert(pushed == sized)Possible issues
An integer literal is an int, never a byte, so pushing one is a compile error. Convert it with byte(...):
let b = Bytes.new()
b.push(65) // error: cannot use int where byte is expectedAn int variable needs the same conversion, which panics when the value does not fit in a byte:
let b = Bytes.new()
let n = 300
b.push(byte(n)) // panic: 300 out of range for byteBytes.push returns void, not the Bytes, so it does not chain. Use the Bytes itself after the call:
let b = Bytes.new()
let grown: Bytes = b.push(byte(1)) // error: cannot use void where Bytes is expected
let n = b.push(byte(2)).length() // error: void has no field or function lengthBytes.push takes one byte. Append many with Bytes.extend, and a fixed-size number with Bytes.append:
let b = Bytes.new()
b.push("hi".toBytes()) // error: cannot use Bytes where byte is expectedA for loop over b reaches the bytes pushed during the loop, so for x in b { b.push(x) } never ends. Double the bytes with Bytes.extend, which appends b as it was at the call, or loop over the indexes that exist before the first push:
let b = Bytes.fromArray([1, 2])
b.extend(b)
b // Bytes(01 02 01 02)let b = Bytes.fromArray([1, 2])
for i in Iter.range(0, len(b)) {
b.push(b[i])
}
println(b)Bytes(01 02 01 02)See also
Bytes.extend · Bytes.append · Bytes.pop · Bytes.truncate · Bytes.new · Array.push
