Files
herolib/aiprompts/v_core/io/string_reader.md
2025-09-02 07:28:13 +02:00

5.4 KiB

module string_reader

Contents

StringReader.new

fn StringReader.new(params StringReaderParams) StringReader

new creates a new StringReader and sets the string builder size to initial_size. If a source

[Return to contents]

StringReader

struct StringReader {
mut:
	reader ?io.Reader
	offset int // current offset in the buffer
pub mut:
	end_of_stream bool // whether we reached the end of the upstream reader
	builder       strings.Builder
}

StringReader is able to read data from a Reader interface and/or source string to a dynamically growing buffer using a string builder. Unlike the BufferedReader, StringReader will keep the entire contents of the buffer in memory, allowing the incoming data to be reused and read in an efficient matter. The StringReader will not set a maximum capacity to the string builders buffer and could grow very large.

[Return to contents]

needs_fill

fn (r StringReader) needs_fill() bool

needs_fill returns whether the buffer needs refilling

[Return to contents]

needs_fill_until

fn (r StringReader) needs_fill_until(n int) bool

needs_fill_until returns whether the buffer needs refilling in order to read n bytes

[Return to contents]

fill_buffer

fn (mut r StringReader) fill_buffer(read_till_end_of_stream bool) !int

fill_bufer tries to read data into the buffer until either a 0 length read or if read_to_end_of_stream is true then the end of the stream. It returns the number of bytes read

[Return to contents]

fill_buffer_until

fn (mut r StringReader) fill_buffer_until(n int) !int

fill_buffer_until tries read n amount of bytes from the reader into the buffer and returns the actual number of bytes read

[Return to contents]

read_all_bytes

fn (mut r StringReader) read_all_bytes(read_till_end_of_stream bool) ![]u8

read_all_bytes reads all bytes from a reader until either a 0 length read or if read_to_end_of_stream is true then the end of the stream. It returns a copy of the read data

[Return to contents]

read_all

fn (mut r StringReader) read_all(read_till_end_of_stream bool) !string

read_all reads all bytes from a reader until either a 0 length read or if read_to_end_of_stream is true then the end of the stream. It produces a string from the read data

[Return to contents]

read_bytes

fn (mut r StringReader) read_bytes(n int) ![]u8

read_bytes tries to read n amount of bytes from the reader

[Return to contents]

read_string

fn (mut r StringReader) read_string(n int) !string

read_bytes tries to read n amount of bytes from the reader and produces a string from the read data

[Return to contents]

read

fn (mut r StringReader) read(mut buf []u8) !int

read implements the Reader interface

[Return to contents]

read_line

fn (mut r StringReader) read_line(config io.BufferedReadLineConfig) !string

read_line attempts to read a line from the reader. It will read until it finds the specified line delimiter such as (\n, the default or \0) or the end of stream.

[Return to contents]

write

fn (mut r StringReader) write(buf []u8) !int

write implements the Writer interface

[Return to contents]

get_data

fn (r StringReader) get_data() []u8

get_data returns a copy of the buffer

[Return to contents]

get_part

fn (r StringReader) get_part(start int, n int) ![]u8

get get_part returns a copy of a part of the buffer from start till start + n

[Return to contents]

get_string

fn (r StringReader) get_string() string

get_string produces a string from all the bytes in the buffer

[Return to contents]

get_string_part

fn (r StringReader) get_string_part(start int, n int) !string

get_string_part produces a string from start till start + n of the buffer

[Return to contents]

flush

fn (mut r StringReader) flush() string

flush clears the stringbuilder and returns the resulting string and the stringreaders offset is reset to 0

[Return to contents]

free

fn (mut r StringReader) free()

free frees the memory block used for the string builders buffer, a new string builder with size 0 is initialized and the stringreaders offset is reset to 0

[Return to contents]

StringReaderParams

struct StringReaderParams {
pub:
	// the reader interface
	reader ?io.Reader
	// initialize the builder with this source string
	source ?string
	// if no source is given the string builder is initialized with this size
	initial_size int
}

[Return to contents]

Powered by vdoc. Generated on: 2 Sep 2025 07:19:15