Bytes
import "std/data" (included in import "std") · namespace std.data
Bytes is a growable buffer of raw bytes. Unlike a string, it can contain zero bytes, so it suits binary file formats and network protocols. Elements are read and written as ints from 0 to 255, with bounds-checked indexing.
grav
import "std"
fn main() {
let b = new Bytes()
b.appendByte(0x47).appendString("RV")
b.appendUint(258, 2) // a 2-byte big-endian number
print(b.size())
print(b.toHex())
print(b[0])
print(b.readUint(3, 2))
print(b.slice(0, 3).toString())
let c = Bytes.fromHex("00ff10")
print(c[1])
c[1] = 7
print(c.toHex())
print(Bytes.fromString("hi") == Bytes.fromHex("6869"))
}5
4752560102
71
258
GRV
255
000710
trueappendUint(v, width) and readUint(at, width) write and read big-endian (network order) unsigned numbers of 1 to 8 bytes. toBinary() and Bytes.fromBinary(x) convert to and from the built-in binary type, which is what File.readBinary returns. Sockets read and write Bytes directly (TCP).
API
class Bytes
Constructors
new Bytes()new Bytes(capacity: int)
Static members
static fn fromString(s: string) -> Bytes— The string's bytes.static fn fromBinary(x: binary) -> Bytes— A copy of a binary value's bytes.static fn fromHex(hex: string) -> Bytes— Parse hex digits ("48656c6c6f"); throws nothing, skips non-hex characters.
Methods
fn reserve(n: int)— Makes room for at least n bytes in total.fn size() -> int— The number of bytes.fn length() -> int— The number of bytes (same as size()).fn isEmpty() -> bool— Whether there are no bytes.fn clear()— Removes every byte.fn get(i: int) -> int— The byte at index i, 0..255 (IndexError when out of range).fn set(i: int, v: int)— Sets the byte at index i to v (IndexError when out of range).fn operator[](i: int) -> int—b[i]: the byte at index i.fn operator[]=(i: int, v: int)—b[i] = v: sets the byte at index i.fn appendByte(v: int) -> Bytes— Appends one byte (the low 8 bits of v); returns this buffer.fn appendString(s: string) -> Bytes— Appends a string's bytes; returns this buffer.fn append(other: Bytes) -> Bytes— Appends another buffer's bytes; returns this buffer.fn appendUint(v: long, width: int) -> Bytes— Big-endian integers ofwidthbytes (1, 2, 4 or 8), as network protocols use.fn readUint(at: int, width: int) -> long— Reads awidth-byte big-endian unsigned number starting atat.fn appendUintLE(v: long, width: int) -> Bytes— Little-endian integers ofwidthbytes (1 to 8), as file formats and protobuf use.fn readUintLE(at: int, width: int) -> long— Reads awidth-byte little-endian number starting atat.fn slice(start: int, end: int) -> Bytes— Bytes [start, end) as a new buffer (bounds are clamped).fn dropFront(n: int)— Remove the firstnbytes (used for consuming parsed input).fn indexOf(sub: string) -> int— The index of the first occurrence ofsub's bytes, or -1.fn toString() -> string— The bytes as text (stops at a zero byte, like any string).fn toBinary() -> binary— The bytes as a binary value.fn toHex() -> string— The bytes in hexadecimal ("00ff10").fn copy() -> Bytes— A new buffer with the same bytes.fn operator==(other: Bytes) -> bool— Whether both buffers hold the same bytes.fn rawData() -> byte*— For C helpers in other modules: the raw buffer.fn appendRaw(p: byte*, n: int)— Appends n bytes from raw memory.