msgpack/buf
an owned, length-prefixed byte buffer.
Since: Phase MP1
Buf
(Buf)
an owned length-prefixed byte buffer (ABI: ptr<void>).
buf-alloc
(buf-alloc [n : int] :)
allocate a zeroed buffer of `n` bytes.
| n | number of data bytes (not counting the 8-byte length prefix) |
A fresh owned Buf; release it with buf-free.
(let [b (buf-alloc 4)] (buf-len b)) ; => 4
Since: Phase MP1
buf-len
(buf-len [b : Buf] :)
the number of data bytes the buffer holds.
| b | the buffer to measure |
int -- the byte length (0 for a null handle).
(buf-len (hex->buf "c3")) ; => 1
Since: Phase MP1
buf-data
(buf-data [b : Buf] :)
a borrowed pointer to the buffer's first data byte.
| b | the buffer to borrow from |
ptr<void> -- the data region (past the length prefix).
(let [b (buf-alloc 8)] (buf-data b))
Since: Phase MP1
buf-free
(buf-free [b : Buf] :)
release the buffer.
| b | the buffer whose owning handle is being dropped |
(buf-free (encode-mp 42))
Since: Phase MP1
buf-concat
(buf-concat [a : Buf b : Buf] :)
concatenate two buffers, CONSUMING both.
| a | the left fragment (consumed) | |
| b | the right fragment (consumed) |
A fresh owned Buf holding a's bytes followed by b's.
(buf->hex (buf-concat (encode-mp 1) (encode-mp 2))) ; => "0102"
Since: Phase MP1
buf-byte
(buf-byte [b : Buf i : int] :)
the i-th data byte, as an int in 0..255.
| b | the buffer to read | |
| i | zero-based byte index |
int -- the byte, or -1 when i is out of range.
(buf-byte (encode-mp true) 0) ; => 195 (0xc3)
Since: Phase MP1
buf-empty?
(buf-empty? [b : Buf] :)
true when the buffer holds no data bytes.
| b | the buffer to test |
bool -- true for a zero-length (or null) buffer.
(buf-empty? (buf-alloc 0)) ; => true
Since: Phase MP1
buf->hex
(buf->hex [b : Buf] :)
render the buffer as lowercase hex.
| b | the buffer to render |
cstr -- a freshly malloc'd hex string; the caller frees it.
(buf->hex (encode-mp 128)) ; => "cc80"
Since: Phase MP1
hex->buf
(hex->buf [s : cstr] :)
parse a lowercase-or-uppercase hex string into a Buf.
| s | an even-length string of hex digits |
A fresh owned Buf; an EMPTY buf when `s` is malformed (odd length or a non-hex digit).
(buf-byte (hex->buf "c0") 0) ; => 192
Since: Phase MP1
Internal definitions
__buf-alloc-raw-- internal: allocate a zeroed buffer, as a bare pointer.__buf-concat-raw-- internal: concatenate, as a bare pointer.buf-- byte-for-byte equality.__buf-null-ptr?-- internal: is this a null pointer?__hex->buf-raw-- internal: parse hex, as a bare pointer (NULL on error).