veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

buf

lib/buf.vk

lib/buf.vk Pure-Veko cursor-based ByteReader, ByteWriter, and StringBuilder built on hardware LE/BE wire intrinsics. Zero capabilities required.

ByteReader struct
signature
ByteReader = struct { data bytes, pos i64 = ... }

Sequential cursor over an immutable bytes buffer.

ByteWriter struct
signature
ByteWriter = struct { buf bytes }

Growable binary buffer builder for packet and file serialization.

signature
StringBuilder = struct { buf bytes }

Growable UTF-8 string builder backed by a pre-allocated bytes slab.

signature
reader-new = fn [data bytes] ByteReader

Creates a new ByteReader positioned at offset 0 of data.

Executable Example
veko
use [buf]
mut w = buf/writer-new 16
_ = buf/write-u16-be mut w 0x0303
_ = buf/write-u32-le mut w 0x11223344
mut r = buf/reader-new (buf/writer-bytes w)
v1 = buf/read-u16-be mut r
v2 = buf/read-u32-le mut r
if v1 != 0x0303 or v2 != 0x11223344 or (not (buf/reader-eof? r)) [ err 'buf rw' ] else [ .ok ]
signature
reader-remaining = fn [r ByteReader] i64

Returns the number of unread bytes remaining in r.

signature
reader-eof? = fn [r ByteReader] bool

Returns true when r has reached the end of its buffer.

signature
reader-seek = fn [mut r ByteReader, off i64] i64

Moves the cursor of r to absolute byte offset off.

signature
reader-skip = fn [mut r ByteReader, n i64] i64

Advances the cursor of r by n bytes.

signature
read-u8 = fn [mut r ByteReader] i64

Reads a single unsigned byte from r.

signature
read-u16-le = fn [mut r ByteReader] i64

Reads an unsigned 16-bit little-endian integer from r.

signature
read-u16-be = fn [mut r ByteReader] i64

Reads an unsigned 16-bit big-endian integer from r.

signature
read-u32-le = fn [mut r ByteReader] i64

Reads an unsigned 32-bit little-endian integer from r.

signature
read-u32-be = fn [mut r ByteReader] i64

Reads an unsigned 32-bit big-endian integer from r.

signature
read-i64-le = fn [mut r ByteReader] i64

Reads a 64-bit little-endian integer from r.

signature
read-i64-be = fn [mut r ByteReader] i64

Reads a 64-bit big-endian integer from r.

signature
read-bytes = fn [mut r ByteReader, n i64] bytes

Reads exactly n bytes from r.

signature
read-str = fn [mut r ByteReader, n i64] str

Reads n bytes from r and decodes them as a UTF-8 str.

signature
read-rest = fn [mut r ByteReader] bytes

Reads all remaining unread bytes from r.

signature
read-until = fn [mut r ByteReader, delim i64] bytes

Reads bytes up to (but not including) delim, advancing past delim; returns err if not found.

signature
writer-new = fn [cap i64] ByteWriter

Creates a new ByteWriter pre-allocated with cap bytes.

signature
writer-len = fn [w ByteWriter] i64

Returns the current byte length written into w.

signature
writer-bytes = fn [w ByteWriter] bytes

Returns the accumulated bytes buffer from w.

signature
write-u8 = fn [mut w ByteWriter, v i64] i64

Appends one byte to w.

signature
write-u16-le = fn [mut w ByteWriter, v i64] i64

Appends a 16-bit little-endian integer to w.

signature
write-u16-be = fn [mut w ByteWriter, v i64] i64

Appends a 16-bit big-endian integer to w.

signature
write-u32-le = fn [mut w ByteWriter, v i64] i64

Appends a 32-bit little-endian integer to w.

signature
write-u32-be = fn [mut w ByteWriter, v i64] i64

Appends a 32-bit big-endian integer to w.

signature
write-i64-le = fn [mut w ByteWriter, v i64] i64

Appends a 64-bit little-endian integer to w.

signature
write-i64-be = fn [mut w ByteWriter, v i64] i64

Appends a 64-bit big-endian integer to w.

signature
write-bytes = fn [mut w ByteWriter, data bytes] i64

Appends a bytes slice to w.

signature
write-str = fn [mut w ByteWriter, s str] i64

Appends a UTF-8 str to w.

signature
sb-new = fn [cap i64] StringBuilder

Creates a new StringBuilder with initial capacity cap.

Executable Example
veko
use [buf]
mut sb = buf/sb-new 32
_ = buf/sb-push mut sb 'hello'
_ = buf/sb-push mut sb ' veko'
if (buf/sb-build sb) != 'hello veko' [ err 'sb mismatch' ] else [ .ok ]
signature
sb-push = fn [mut sb StringBuilder, s str] i64

Appends s to sb.

signature
sb-push-line = fn [mut sb StringBuilder, s str] i64

Appends s followed by \n to sb.

signature
sb-push-byte = fn [mut sb StringBuilder, b i64] i64

Appends a single byte b to sb.

signature
sb-len = fn [sb StringBuilder] i64

Returns the current byte length of sb.

signature
sb-build = fn [sb StringBuilder] str

Returns the built UTF-8 string.