Bytes.extend
func Bytes.extend(b: Bytes, src: Bytes): voidBytes.extend appends the bytes src holds at the call to b.
Details
- To keep
bas it is and get newBytes, use Bytes.concat. - Appends the bytes
srcholds at the call, sob.extend(b)doublesb, and a later write tosrcdoes not reachb.
Basic examples
Append the bytes of one collection to another:
let b = Bytes.fromArray([0xca, 0xfe])
Bytes.extend(b, Bytes.fromArray([0xba, 0xbe]))
b // Bytes(ca fe ba be)Append the UTF-8 bytes of a string:
let b = "ant".toBytes()
Bytes.extend(b, "hill".toBytes())
String.fromBytes(b) // "anthill"Scope
Empty src leaves b as it was, and empty b ends up with the bytes of src:
let b = Bytes.fromArray([1, 2])
b.extend(Bytes.new())
b // Bytes(01 02)
let empty = Bytes.new()
empty.extend(Bytes.fromArray([3, 4]))
empty // Bytes(03 04)Bytes extended with itself doubles, because the call reads src as it was at the start:
let b = Bytes.fromArray([1, 2])
b.extend(b)
b // Bytes(01 02 01 02)The bytes are copied in. Writing to src afterwards leaves b as it is:
let b = Bytes.fromArray([1])
let src = Bytes.fromArray([2])
b.extend(src)
src[0] = byte(9)
b // Bytes(01 02)
src // Bytes(09)Applications
Build a message in one buffer: a type byte, a two-byte length, then the payload:
func message(kind: byte, payload: Bytes): Bytes {
let out = Bytes.new()
out.push(kind)
out.append(type(uint16), uint16(len(payload)), Bytes.BigEndian{})
out.extend(payload)
out
}
println(message(byte(1), "hi".toBytes()))Bytes(01 00 02 68 69)Append only part of another Bytes. Bytes.slice copies the range first, so src can be any range of any Bytes, b included:
let input = "id=42;".toBytes()
let out = "value: ".toBytes()
out.extend(input.slice(3, 5))
println(String.fromBytes(out) ?? "")
out.extend(out.slice(0, 5))
println(String.fromBytes(out) ?? "")value: 42
value: 42valueRead a file in chunks and collect them in one buffer:
func readFile(path: string): Result[Bytes, Error] {
let f = try File.open(path)
defer { f.close() }
let out = Bytes.new()
while true {
let chunk = try f.readChunk(4) ?? break
out.extend(chunk)
}
Result.ok(out)
}
let data = try readFile("data.bin")
println(len(data))
print(String.fromBytes(data) ?? "")14
hello, chunksReuse one buffer for each record: Bytes.truncate empties it, and Bytes.extend fills it again:
let line = Bytes.new()
for word in ["ant", "hill"] {
line.truncate(0)
line.extend(word.toBytes())
line.extend("\n".toBytes())
print(String.fromBytes(line) ?? "")
}ant
hillProperties and relations
The length grows by the length of src:
let b = "日本".toBytes()
let src = Bytes.fromArray([0, 255])
let before = len(b)
b.extend(src)
assert(len(b) == before + len(src))It gives in place what Bytes.concat returns as new Bytes:
let b = Bytes.fromArray([1, 2])
let src = Bytes.fromArray([3])
let joined = Bytes.concat([b, src])
b.extend(src)
assert(b == joined)It is Bytes.push of each byte of src, in order:
let a = Bytes.fromArray([1])
let c = Bytes.fromArray([1])
let src = Bytes.fromArray([2, 3])
a.extend(src)
for x in src {
c.push(x)
}
assert(a == c)b stays the same collection, so every other name for it sees the new bytes:
let b = Bytes.fromArray([1])
let alias = b
b.extend(Bytes.fromArray([2]))
assert(Bytes.same(alias, b))
alias // Bytes(01 02)Possible issues
Bytes.extend returns void, not the extended bytes. To keep b and get new Bytes, use Bytes.concat:
let b = Bytes.fromArray([1])
let src = Bytes.fromArray([2])
Bytes.concat([b, src]) // Bytes(01 02)
b // Bytes(01)src must be Bytes. A string or an array of integers is a compile error; convert it with String.toBytes or Bytes.fromArray:
let b = Bytes.new()
b.extend("hi") // error: cannot use string where Bytes is expected
b.extend([1, 2]) // error: cannot use Array[int] where Bytes is expectedTo append one byte, use Bytes.push. An integer literal is an int, so convert it:
let b = Bytes.new()
b.push(byte(10))
b // Bytes(0a)A for loop over b follows its current length, so it also visits the bytes that Bytes.extend appends during the loop. Loop over a copy from Bytes.clone to visit only the bytes that were there at the start:
let b = Bytes.fromArray([1, 2])
for x in b.clone() {
b.extend(Bytes.fromArray([x * byte(10)]))
}
b // Bytes(01 02 0a 14)See also
Bytes.push, Bytes.concat, Bytes.append, Bytes.truncate, Bytes.clone, Bytes.same, Array.extend
