core/bufio
bufio
Types
8Lookahead_Reader
Lookahead_Reader :: struct {
r: io.Reader,
buf: []u8,
n: int,
}SourceLookahead_Reader provides io lookahead. This is useful for tokenizers/parsers. Lookahead_Reader is similar to bufio.Reader, but unlike bufio.Reader, Lookahead_Reader's buffer size will EXACTLY match the specified size, whereas bufio.Reader's buffer size may differ from the specified size. This makes sure that the buffer will not be accidentally read beyond the expected size.
Read_Writer
Read_Writer :: struct {
r: ^Reader,
w: ^Writer,
}SourceRead_Writer stores pointers to a Reader and a Writer
Reader
Reader :: struct {
buf: []u8,
buf_allocator: runtime.Allocator,
rd: io.Reader,
r: int,
w: int,
err: io.Error,
last_byte: int,
last_rune_size: int,
max_consecutive_empty_reads: int,
}SourceReader is a buffered wrapper for an io.Reader
Scanner
Scanner :: struct {
r: io.Reader,
split: Split_Proc,
buf: [dynamic]u8,
max_token_size: int,
start: int,
end: int,
token: []u8,
_err: Scanner_Error,
max_consecutive_empty_reads: int,
successive_empty_token_count: int,
scan_called: bool,
done: bool,
}SourceScanner_Error
Scanner_Error :: union {
io.Error,
Scanner_Extra_Error,
}SourceScanner_Extra_Error
Scanner_Extra_Error :: enum i32 {
None = 0,
Negative_Advance = 1,
Advanced_Too_Far = 2,
Bad_Read_Count = 3,
Too_Long = 4,
Too_Short = 5,
}SourceExtra errors returns by scanning procedures
Split_Proc
Split_Proc :: proc(data: []byte, at_eof: bool) -> (advance: int, token: []byte, err: Scanner_Error, final_token: bool)SourceSplit_Proc is the signature of the split procedure used to tokenize the input.
Writer
Writer :: struct {
buf: []u8,
buf_allocator: runtime.Allocator,
wr: io.Writer,
n: int,
err: io.Error,
max_consecutive_empty_writes: int,
}Sourceimport "core:bytes" Writer is a buffered wrapper for an io.Writer
Constants
2Procedures
54_writer_proc
_writer_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []u8, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error)Sourcelookahead_reader_buffer
lookahead_reader_buffer :: proc(lr: ^Lookahead_Reader) -> ([]u8)Sourcelookahead_reader_consume
lookahead_reader_consume :: proc(lr: ^Lookahead_Reader, n: int) -> (io.Error)Sourcelookahead_reader_consume drops the first n populated bytes from the Lookahead_Reader.
lookahead_reader_consume_all
lookahead_reader_consume_all :: proc(lr: ^Lookahead_Reader) -> (io.Error)Sourcelookahead_reader_init
lookahead_reader_init :: proc(lr: ^Lookahead_Reader, r: io.Reader, buf: []u8) -> (^Lookahead_Reader)Sourcelookahead_reader_peek
lookahead_reader_peek :: proc(lr: ^Lookahead_Reader, n: int) -> ([]u8, io.Error)Sourcelookahead_reader_peek returns a slice of the Lookahead_Reader which holds n bytes If the Lookahead_Reader cannot hold enough bytes, it will read from the underlying reader to populate the rest. NOTE: The returned buffer is not a copy of the underlying buffer
lookahead_reader_peek_all
lookahead_reader_peek_all :: proc(lr: ^Lookahead_Reader) -> ([]u8, io.Error)Sourcelookahead_reader_peek_all returns a slice of the Lookahead_Reader populating the full buffer If the Lookahead_Reader cannot hold enough bytes, it will read from the underlying reader to populate the rest. NOTE: The returned buffer is not a copy of the underlying buffer
read_writer_init
read_writer_init :: proc(rw: ^Read_Writer, r: ^Reader, w: ^Writer)Sourceread_writer_to_stream
read_writer_to_stream :: proc(rw: ^Read_Writer) -> (s: io.Stream)Sourcereader_buffered
reader_buffered :: proc(b: ^Reader) -> (int)Sourcereader_buffered returns the number of bytes that can be read from the current buffer
reader_destroy
reader_destroy :: proc(b: ^Reader)Sourcereader_destroy destroys the underlying buffer with its associated allocator if and only if (⟺) that allocator has been set
reader_discard
reader_discard :: proc(b: ^Reader, n: int) -> (discarded: int, err: io.Error)Sourcereader_discard skips the next n bytes, and returns the number of bytes that were discarded
reader_init
reader_init :: proc(b: ^Reader, rd: io.Reader, size: int, allocator: mem.Allocator = context.allocator, loc = #caller_location)Sourcereader_init initializes using an allocator
reader_init_with_buf
reader_init_with_buf :: proc(b: ^Reader, rd: io.Reader, buf: []u8)Sourcereader_init initializes using a user provided bytes buffer buf
reader_peek
reader_peek :: proc(b: ^Reader, n: int) -> (data: []u8, err: io.Error)Sourcereader_peek returns the next n bytes without advancing the reader The bytes stop being valid on the next read call If reader_peek returns fewer than n bytes, it also return an error explaining why the read is short The error will be .Buffer_Full if n is larger than the internal buffer size
reader_read
reader_read :: proc(b: ^Reader, p: []u8) -> (n: int, err: io.Error)Sourcereader_read reads data into p The bytes are taken from at most one read on the underlying Reader, which means n may be less than len(p)
reader_read_byte
reader_read_byte :: proc(b: ^Reader) -> (c: u8, err: io.Error)Sourcereader_read_byte reads and returns a single byte If no byte is available, it return an error
reader_read_bytes
reader_read_bytes :: proc(b: ^Reader, delim: u8, allocator: mem.Allocator = context.allocator) -> (buf: []u8, err: io.Error)Sourcereader_read_bytes reads until the first occurrence of delim from the Reader It returns an allocated slice containing the data up to and including the delimiter
reader_read_rune
reader_read_rune :: proc(b: ^Reader) -> (r: rune, size: int, err: io.Error)Sourcereader_read_rune reads a single UTF-8 encoded unicode character and returns the rune and its size in bytes If the encoded rune is invalid, it consumes one byte and returns utf8.RUNE_ERROR (U+FFFD) with a size of 1
reader_read_slice
reader_read_slice :: proc(b: ^Reader, delim: u8) -> (line: []u8, err: io.Error)Sourcereader_read_slice reads until the first occurrence of delim in the input, returning a slice pointing at the bytes in the internal buffer. The returned slice is only valid until the next read call. If the buffer fills without finding delim, it returns .Buffer_Full. If the underlying reader returns an error before finding delim, that error is returned. Because the returned data will be overwritten by the next I/O operation, reader_read_bytes or reader_read_string is usually preferred.
reader_read_slice returns err != nil if and only if line does not end in delim.
reader_read_string
reader_read_string :: proc(b: ^Reader, delim: u8, allocator: mem.Allocator = context.allocator) -> (io.Error, string)Sourcereader_read_string reads until the first occurrence of delim from the Reader It returns an allocated string containing the data up to and including the delimiter
reader_reset
reader_reset :: proc(b: ^Reader, r: io.Reader)Sourcereader_reset resets the read and write positions, and the error values
reader_size
reader_size :: proc(b: ^Reader) -> (int)Sourcereader_size returns the number of bytes in the backing buffer
reader_to_stream
reader_to_stream :: proc(b: ^Reader) -> (s: io.Stream)Sourcereader_to_stream converts a Reader into an io.Stream
reader_unread_byte
reader_unread_byte :: proc(b: ^Reader) -> (io.Error)Sourcereader_unread_byte unreads the last byte. Only the most recently read byte can be unread
reader_unread_rune
reader_unread_rune :: proc(b: ^Reader) -> (io.Error)Sourcereader_unread_rune unreads the last rune. Only the most recently read rune can be unread
reader_write_to
reader_write_to :: proc(b: ^Reader, w: io.Writer) -> (n: i64, err: io.Error)Sourcescan
scan :: proc(s: ^Scanner) -> (bool)Sourcescan advances the Scanner
scan_bytes
scan_bytes :: proc(data: []u8, at_eof: bool) -> (advance: int, token: []u8, err: Scanner_Error, final_token: bool)Sourcescan_bytes is a splitting procedure that returns each byte as a token
scan_lines
scan_lines :: proc(data: []u8, at_eof: bool) -> (advance: int, token: []u8, err: Scanner_Error, final_token: bool)Sourcescan_lines is a splitting procedure that returns each line of text stripping of any trailing newline and an optional preceding carriage return (\r?\n). A new line is allowed to be empty.
scan_runes
scan_runes :: proc(data: []u8, at_eof: bool) -> (advance: int, token: []u8, err: Scanner_Error, final_token: bool)Sourcescan_runes is a splitting procedure that returns each UTF-8 encoded rune as a token. The lsit of runes return is equivalent to that of iterating over a string in a 'for in' loop, meaning any erroneous UTF-8 encodings will be returned as U+FFFD. Unfortunately this means it is impossible for the "client" to know whether a U+FFFD is an expected replacement rune or an encoding of an error.
scan_words
scan_words :: proc(data: []u8, at_eof: bool) -> (advance: int, token: []u8, err: Scanner_Error, final_token: bool)Sourcescan_words is a splitting procedure that returns each Unicode-space-separated word of text, excluding the surrounded spaces. It will never return return an empty string.
scanner_bytes
scanner_bytes :: proc(s: ^Scanner) -> ([]u8)SourceReturns the most recent token created by 'scan'. The underlying array may point to data that may be overwritten by another call to 'scan'. Treat the returned value as if it is immutable.
scanner_destroy
scanner_destroy :: proc(s: ^Scanner)Sourcescanner_error
scanner_error :: proc(s: ^Scanner) -> (Scanner_Error)SourceReturns the first non-EOF error that was encountered by the scanner
scanner_init
scanner_init :: proc(s: ^Scanner, r: io.Reader, buf_allocator: mem.Allocator = context.allocator) -> (^Scanner)SourceInitializes a Scanner buffer an allocator buf_allocator
scanner_init_with_buffer
scanner_init_with_buffer :: proc(s: ^Scanner, r: io.Reader, buf: []u8) -> (^Scanner)SourceInitializes a Scanner buffer a user provided bytes buffer buf
scanner_scan
scanner_scan :: proc(s: ^Scanner) -> (bool)Sourcescanner_scan is an alias of scan
scanner_text
scanner_text :: proc(s: ^Scanner) -> (string)SourceReturns the most recent token created by 'scan'. The underlying array may point to data that may be overwritten by another call to 'scan'. Treat the returned value as if it is immutable.
writer_available
writer_available :: proc(b: ^Writer) -> (int)Sourcewriter_available returns how many bytes are unused in the buffer
writer_buffered
writer_buffered :: proc(b: ^Writer) -> (int)Sourcewriter_buffered returns the number of bytes that have been writted into the current buffer
writer_destroy
writer_destroy :: proc(b: ^Writer)Sourcewriter_destroy destroys the underlying buffer with its associated allocator if and only if (⟺) that allocator has been set
writer_flush
writer_flush :: proc(b: ^Writer) -> (io.Error)Sourcewriter_flush writes any buffered data into the underlying io.Writer
writer_init
writer_init :: proc(b: ^Writer, wr: io.Writer, size: int, allocator: mem.Allocator = context.allocator)SourceInitialized a Writer with an allocator
writer_init_with_buf
writer_init_with_buf :: proc(b: ^Writer, wr: io.Writer, buf: []u8)SourceInitialized a Writer with a user provided buffer buf
writer_read_from
writer_read_from :: proc(b: ^Writer, r: io.Reader) -> (n: i64, err: io.Error)Sourcewriter_read_from is to support io.Reader_From types If the underlying writer supports the io,read_from, and b has no buffered data yet, this procedure calls the underlying read_from implementation without buffering
writer_reset
writer_reset :: proc(b: ^Writer, w: io.Writer)Sourcewriter_size
writer_size :: proc(b: ^Writer) -> (int)Sourcewriter_size returns the size of underlying buffer in bytes
writer_to_stream
writer_to_stream :: proc(b: ^Writer) -> (s: io.Stream)Sourcewriter_to_stream converts a Writer into an io.Stream
writer_to_writer
writer_to_writer :: proc(b: ^Writer) -> (s: io.Writer)Sourcewriter_to_stream converts a Writer into an io.Stream
writer_write
writer_write :: proc(b: ^Writer, p: []u8) -> (n: int, err: io.Error)Sourcewriter_write writes the contents of p into the buffer It returns the number of bytes written If n < len(p), it will return an error explaining why the write is short
writer_write_byte
writer_write_byte :: proc(b: ^Writer, c: u8) -> (io.Error)Sourcewriter_write_byte writes a single byte
writer_write_rune
writer_write_rune :: proc(b: ^Writer, r: rune) -> (size: int, err: io.Error)Sourcewriter_write_rune writes a single unicode code point, and returns the number of bytes written with any error
writer_write_string
writer_write_string :: proc(b: ^Writer, s: string) -> (io.Error, int)Sourcewriter_write_string writes a string into the buffer It returns the number of bytes written If n < len(p), it will return an error explaining why the write is short