Bytes & BytesBuilder
Pyle strings are byte strings, but they are also interned and immutable. Bytes and BytesBuilder are the types to use for binary protocols, files, and hashing.
Bytes(array) creates an immutable byte buffer from an array of ints.
let b = Bytes([72, 101, 108, 108, 111])print(b.size()) # 5print(b[0]) # 72, an intprint(b.to_string()) # HelloByte values must be 0-255. Bytes([300]) and Bytes([-1]) raise RuntimeError.
| Method | Returns | Description |
|---|---|---|
.size() |
int |
Byte count |
.to_string() |
string |
Raw byte-preserving copy into a string |
.to_int_le() |
int |
Read an int64 little-endian; requires exactly 8 bytes |
.to_int_be() |
int |
Read an int64 big-endian; requires exactly 8 bytes |
.to_float_le() |
float |
Read a float64 little-endian; requires exactly 8 bytes |
.to_float_be() |
float |
Read a float64 big-endian; requires exactly 8 bytes |
let header = Bytes([1, 0, 0, 0, 0, 0, 0, 0])print(header.to_int_le()) # 1print(header.to_int_be()) # 72057594037927936to_string does not validate UTF-8. It copies the raw bytes; interpreting them is up to you.
Indexing returns an int, and out-of-bounds access is a runtime error. Iterating yields ints:
for byte in Bytes([65, 66]) { print(byte) # 65, 66}BytesBuilder
Section titled “BytesBuilder”BytesBuilder(capacity?) creates a mutable byte writer. It has no indexed access; reading is done through to_bytes() or to_string().
| Method | Returns | Description |
|---|---|---|
.push(b: int) |
int |
Append one byte (0-255); returns the new size |
.extend_array(arr: array[int]) |
int |
Append packed bytes from an int array; returns the new size |
.reserve(n: int) |
int |
Ensure capacity for n bytes; returns the capacity |
.size() |
int |
Current byte count |
.to_string() |
string |
Raw copy into a string; the builder is unchanged |
.to_bytes() |
Bytes |
Move the bytes into Bytes; the builder is emptied and reusable |
let b = BytesBuilder()b.push(72)b.extend_array([101, 108, 108, 111])
let hello = b.to_bytes()print(hello.to_string()) # Helloprint(b.size()) # 0, the builder was emptiedlet b = BytesBuilder(1024)b.reserve(4096)b.push(255)
let text = b.to_string() # copy, builder still holds the byteprint(b.size()) # 1Choosing between them
Section titled “Choosing between them”- Use
Byteswhen the data is final and should not change. - Use
BytesBuilderwhen appending incrementally. - Use
.extend_array(chunk)rather than calling.push()per byte; it is one native call per chunk. - Use
encoding.hex_encode(bytes)to inspect binary data as text.
Related
Section titled “Related”- Strings, Arrays & Maps
- Files for
read_bytesandwrite_bytes - encoding for hex and Base64 coding
- crypto for digests and random bytes