peek buffer - retrieves data from a buffer at a specific position and size
string = pbuf(buffer, offset, size) number = pbuf(buffer [, offset)
See also clbuf, cbuf, buffer type, record
pbuf can read data from a buffer for any numeric or string data type. If pbuf assigns data to a string then the offset and size must be specified. The size of the data actually retrieved is a “best fit”, determined by the (remaining) size of the buffer or by the string in case of a fixed-length string. If pbuf assigns data to a numeric type, the offset is optional and the size of the data actually retrieved is determined by the size of the numeric type or the size of the buffer.
This example stores a string and its size in a buffer, mimicking a simple string type with a descriptor. The size is then retrieved from the buffer and used to assign the string to a variable length string type.
' SharpBASIC buffer write-read programming example
' ------------------------------------------------
option strict;
incl "lib/sys.sbi";
const title = "A new BASIC language";
dim buffer: byte * 24;
dim text: str;
dim size: uint;
main do
' size of constant string
size = len(title);
' size to buffer
buffer = cbuf(size);
' string to buffer skipping size
buffer = cbuf(title, 4);
' get size from buffer (read 4 bytes - uint size on 32-bits arch)
size = pbuf(buffer);
' get string from buffer
text = pbuf(buffer, len(size), size);
print(text);
print(size);
end
Output:
A new BASIC language
20