Skip to main content

buffer

include "buffer";

Growable byte storage for building, parsing, reading and writing binary data.

Types​

buffer​

Object

Growable byte storage with a read cursor.

size infallible​

infallible fn size() -> int

Number of bytes stored.

capacity infallible​

infallible fn capacity() -> int

Bytes that fit before the storage next grows.

position infallible​

infallible fn position() -> int

The read cursor used by read and read_byte.

remaining infallible​

infallible fn remaining() -> int

Bytes between the read cursor and the end.

close infallible​

infallible fn close()

Does nothing; buffers are reclaimed automatically.

matches_string​

fn matches_string(string text) -> bool

Whether the whole buffer equals the bytes of text.

reserve​

fn reserve(int capacity)

Ensures room for at least capacity bytes.

resize​

fn resize(int size)

Changes the size; new bytes are zero.

append_string​

fn append_string(string text)

Appends the bytes of text.

append_strings​

fn append_strings(string first, string second)

Appends the bytes of first, then the bytes of second.

append_int​

fn append_int(int value)

Appends value as decimal digits.

push​

fn push(int value)

Appends one byte (0 to 255).

clear​

fn clear()

Removes every byte and rewinds the cursor; keeps the capacity.

get​

fn get(int index) -> int

The byte at index.

set​

fn set(int index, int value)

Replaces the byte at index.

seek​

fn seek(int position)

Moves the read cursor; the end of the buffer is a valid position.

read_byte​

fn read_byte() -> int

Reads the byte at the cursor and advances it; -1 at the end.

slice​

fn slice(int offset, int count) -> buffer

A new buffer holding a copy of count bytes from offset.

read​

fn read(int count) -> buffer

Copies up to count bytes from the cursor into a new buffer and advances the cursor past them.

find​

fn find(string needle, int start) -> int

The first index at or after start where needle occurs, or -1.

find_range​

fn find_range(string needle, int offset, int count) -> int

The first index inside [offset, offset + count) where needle occurs, or -1.

count_byte​

fn count_byte(int byte, int offset, int count) -> int

How many times byte occurs inside [offset, offset + count).

find_byte​

fn find_byte(int byte, int offset, int count) -> int

The first index of byte inside [offset, offset + count), or -1.

range_has_only_text_bytes​

fn range_has_only_text_bytes(int offset, int count, bool allow_tab) -> bool

Whether [offset, offset + count) holds only printable ASCII text (and tabs when allow_tab is set).

range_matches​

fn range_matches(int offset, int count, string text) -> bool

Whether [offset, offset + count) equals the bytes of text.

range_ascii_case_matches​

fn range_ascii_case_matches(int offset, int count, string text) -> bool

Whether [offset, offset + count) equals text, ignoring ASCII case.

copy_range​

fn copy_range(
int destination_offset,
buffer source,
int source_offset,
int count)

Copies count bytes from source into this buffer at destination_offset. source may be this buffer, and the ranges may overlap.

append_range​

fn append_range(buffer source, int source_offset, int count)

Appends count bytes of source starting at source_offset.

find_first_not_of​

fn find_first_not_of(string allowed, int offset, int count) -> int

The first index inside [offset, offset + count) whose byte is not in allowed, or -1.

find_last_not_of​

fn find_last_not_of(string allowed, int offset, int count) -> int

The last index inside [offset, offset + count) whose byte is not in allowed, or -1.

to_string​

fn to_string() -> string

Requires include "string";.

The whole buffer as a string.

range_to_string​

fn range_to_string(int offset, int count) -> string

Requires include "string";.

The count bytes starting at offset, as a string.

range_to_ascii_lower_string​

fn range_to_ascii_lower_string(int offset, int count) -> string

Requires include "string";.

Like range_to_string, with ASCII letters made lowercase.

range_to_cached_string​

fn range_to_cached_string(int offset, int count, string cached) -> string

Requires include "string";.

Like range_to_string, but returns cached itself when it already holds those bytes. Pass the string from the last read to reuse it while the text is unchanged.

Functions​

buffer_with_capacity​

fn buffer_with_capacity(int capacity) -> buffer

An empty buffer with room for capacity bytes.

buffer_from_string​

fn buffer_from_string(string text) -> buffer

A buffer holding the bytes of text.