Simple bytes reader in pure V, adapted from go version of bytes.Reader
$v install https://github.com/blackshirt/buffer
and then import it in your modules.
// import module
import blackshirt.buffer
// create new reader with given arrays of bytes as an input.
// data := []u8
mut reader := buffer.new_reader(data)
// example read one byte
byte := r.read_byte()!
// read two byte
twobyte := r.read_u16()!
// do with a byte or two byte
and then you can call desired methods.
fn new_reader(b []u8) &Reader
create new Reader with big endianess set to true, for more other option, see new_reader_with_endianess
function.
fn new_reader_with_endianess(b []u8, endian bool) &Reader
new_reader_with_endianess create new reader.
fn (mut r Reader) reset(b []u8)
resets the Reader to be reading from b
fn (r &Reader) remainder() int
remainder tells the length of unread portion of buffer.
fn (r &Reader) cap() i64
cap return capacity or original size of the buffer.
fn (mut r Reader) sub_reader(start i64, amount i64) !&Reader
sub_reader create sub Reader from defined current reader.
fn (mut r Reader) read_u8() !u8
read_u8 read one byte and updates current index
fn (mut r Reader) read_byte() !u8
read_byte is an alias for read_u8
fn (mut r Reader) peek_u8() !u8
peek_u8 peek one byte without udpates current index
fn (mut r Reader) read(mut b []u8) !int
implements io.Reader read b.len bytes from reader, and updates current index
fn (mut r Reader) peek(mut b []u8) !int
read b.len bytes from reader, without updates current index
fn (mut r Reader) read_sized(size int) !([]u8, int)
read with size
fn (mut r Reader) peek_sized(size int) !([]u8, int)
peek_sized peek with size
fn (mut r Reader) read_at_least(amount int) ![]u8
read in amount size from current offset
fn (mut r Reader) skip(amount int)
skip amount of bytes and updates index, its similar to peek but only update the index.
fn (mut r Reader) read_to_end() ![]u8
read from current index to the end of the buffer update the idx to the last
fn (mut r Reader) read_u16() !u16
read u16 size (two byte) from reader
fn (mut r Reader) peek_u16() !u16
peek u16 size (two bytes) from reader.
fn (mut r Reader) read_u24() !int
read_u24 read 3 bytes from reader, and return integer and updates index
fn (mut r Reader) peek_u24() !int
peek_u24 peek 3 bytes from reader without updates index.
fn (mut r Reader) read_u32() !u32
read_u32 read u32size bytes data from reader and updatea index
fn (mut r Reader) peek_u32() !u32
peek_u32 peek u32size bytes fron reader without updates index
fn (mut r Reader) read_u64() !u64
read_u64 read u64size (8) bytes from reader and updates index
fn (mut r Reader) peek_u64() !u64
peek_u64 peek u64size bytes from reader withhout updates index.
fn (r &Reader) remaining() ![]u8
remaining bytes without update the index