SliceReader

Namespace: SnowBank.Buffers · struct

Helper class that holds the internal state used to parse tuples from slices

Remarks

This struct MUST be passed by reference!

Constructors

SliceReader

SliceReader(Slice buffer)

Constructs a new reader that will consume the contents of a Slice

  • buffer — Slice that will be used as the underlying buffer

SliceReader(byte[] buffer)

Constructs a new reader that will consume the contents of a byte array

  • buffer — Array that will be used as the underlying buffer

SliceReader(byte[] buffer, int offset, int count)

Constructs a new reader that will consume the part of a byte array

  • buffer — Array that will be used as the underlying buffer
  • offset — Start offset in the buffer
  • count — Number of bytes exposed

Properties

HasMore

bool HasMore { get; }

Returns true if there are more bytes to parse

Slice Head { get; }

Returns a slice with all the bytes read so far in the buffer

Remaining

int Remaining { get; }

Returns the number of bytes remaining

Tail

Slice Tail { get; }

Returns a slice with all the remaining bytes in the buffer

Methods

EnsureBytes

void EnsureBytes(int count)

Ensures that there are at least count bytes remaining in the buffer

PeekByte

int PeekByte()

Returns the value of the next byte in the buffer, or -1 if we reached the end

PeekByteAt

int PeekByteAt(int offset)

Returns the value of the byte at a specified offset from the current position, or -1 if this is after the end, or before the start

PeekBytes

Slice PeekBytes(int count)

Returns the next count bytes from the current position, without advancing the cursor

ReadByte

byte ReadByte()

Read the next byte from the buffer

ReadByteString

Slice ReadByteString()

Read an encoded nul-terminated byte array from the buffer

ReadBytes

Slice ReadBytes(int count)

Read the next count bytes from the buffer

Slice ReadBytes(uint count)

Read the next count bytes from the buffer

ReadDouble

double ReadDouble()

Reads the next 8 bytes as an IEEE 64-bit floating point number, encoded in little-endian

ReadDoubleBE

double ReadDoubleBE()

Reads the next 8 bytes as an IEEE 64-bit floating point number, encoded in big-endian

ReadFixed128

UInt128 ReadFixed128()

Read the next 16 bytes as an unsigned 128-bit integer, encoded in little-endian

ReadFixed128BE

UInt128 ReadFixed128BE()

Reads the next 16 bytes as an unsigned 128-bit integer, encoded in big-endian

ReadFixed16

ushort ReadFixed16()

Reads the next 2 bytes as an unsigned 16-bit integer, encoded in little-endian

ReadFixed16BE

ushort ReadFixed16BE()

Reads the next 2 bytes as an unsigned 16-bit integer, encoded in big-endian

ReadFixed24

uint ReadFixed24()

Reads the next 3 bytes as an unsigned 24-bit integer, encoded in little-endian

Bits 24 to 31 will always be zero

ReadFixed24BE

uint ReadFixed24BE()

Reads the next 3 bytes as an unsigned 24-bit integer, encoded in big-endian

ReadFixed32

uint ReadFixed32()

Reads the next 4 bytes as an unsigned 32-bit integer, encoded in little-endian

ReadFixed32BE

uint ReadFixed32BE()

Reads the next 4 bytes as an unsigned 32-bit integer, encoded in big-endian

ReadFixed64

ulong ReadFixed64()

Reads the next 8 bytes as an unsigned 64-bit integer, encoded in little-endian

ReadFixed64BE

ulong ReadFixed64BE()

Reads the next 8 bytes as an unsigned 64-bit integer, encoded in big-endian

ReadHalf

Half ReadHalf()

Reads the next 2 bytes as an IEEE 16-bit floating point number, encoded in little-endian

ReadHalfBE

Half ReadHalfBE()

Reads the next 2 bytes as an IEEE 16-bit floating point number, encoded in big-endian

ReadInt128

Int128 ReadInt128()

Read the next 16 bytes as a signed 128-bit integer, encoded in little-endian

ReadInt128BE

Int128 ReadInt128BE()

Reads the next 16 bytes as a signed 128-bit integer, encoded in big-endian

ReadInt16

short ReadInt16()

Reads the next 2 bytes as a signed 16-bit integer, encoded in little-endian

ReadInt16BE

short ReadInt16BE()

Reads the next 2 bytes as a signed 16-bit integer, encoded in big-endian

ReadInt24

int ReadInt24()

Reads the next 3 bytes as a signed 24-bit integer, encoded in little-endian

Bits 24 to 31 will always be zero

ReadInt24BE

int ReadInt24BE()

Reads the next 3 bytes as an unsigned 24-bit integer, encoded in big-endian

The upper 8-bits are always set to 0

ReadInt32

int ReadInt32()

Reads the next 4 bytes as a signed 32-bit integer, encoded in little-endian

ReadInt32BE

int ReadInt32BE()

Reads the next 4 bytes as a signed 32-bit integer, encoded in big-endian

ReadInt64

long ReadInt64()

Reads the next 8 bytes as a signed 64-bit integer, encoded in little-endian

ReadInt64BE

long ReadInt64BE()

Reads the next 8 bytes as a signed 64-bit integer, encoded in big-endian

ReadSingle

float ReadSingle()

Reads the next 4 bytes as an IEEE 32-bit floating point number, encoded in little-endian

ReadSingleBE

float ReadSingleBE()

Reads the next 4 bytes as an IEEE 32-bit floating point number, encoded in big-endian

ReadToEnd

Slice ReadToEnd()

Reads and consumes the remaining data

ReadUInt128

UInt128 ReadUInt128()

Read the next 16 bytes as an unsigned 128-bit integer, encoded in little-endian

ReadUInt128BE

UInt128 ReadUInt128BE()

Reads the next 16 bytes as an unsigned 128-bit integer, encoded in big-endian

ReadUInt16

ushort ReadUInt16()

Reads the next 2 bytes as an unsigned 16-bit integer, encoded in little-endian

ReadUInt16BE

ushort ReadUInt16BE()

Reads the next 2 bytes as an unsigned 16-bit integer, encoded in big-endian

ReadUInt24

uint ReadUInt24()

Reads the next 3 bytes as an unsigned 24-bit integer, encoded in little-endian

Bits 24 to 31 will always be zero

ReadUInt24BE

uint ReadUInt24BE()

Reads the next 3 bytes as an unsigned 24-bit integer, encoded in big-endian

ReadUInt32

uint ReadUInt32()

Reads the next 4 bytes as an unsigned 32-bit integer, encoded in little-endian

ReadUInt32BE

uint ReadUInt32BE()

Reads the next 4 bytes as an unsigned 32-bit integer, encoded in big-endian

ReadUInt64

ulong ReadUInt64()

Reads the next 8 bytes as an unsigned 64-bit integer, encoded in little-endian

ReadUInt64BE

ulong ReadUInt64BE()

Reads the next 8 bytes as an unsigned 64-bit integer, encoded in big-endian

ReadUuid128

Uuid128 ReadUuid128()

Reads a 128-bit UUID

ReadUuid64

Uuid64 ReadUuid64()

Reads a 64-bit UUID

ReadVarBytes

Slice ReadVarBytes()

Reads a variable sized slice, by first reading its size (stored as a Varint32) and then the data

ReadVarInt16

ushort ReadVarInt16()

Reads a 7-bit encoded unsigned int (aka 'Varint16') from the buffer, and advances the cursor

Can Read up to 3 bytes from the input

ReadVarInt32

uint ReadVarInt32()

Reads a 7-bit encoded unsigned int (aka 'Varint32') from the buffer, and advances the cursor

Can Read up to 5 bytes from the input

ReadVarInt64

ulong ReadVarInt64()

Reads a 7-bit encoded unsigned long (aka 'Varint64') from the buffer, and advances the cursor

Can Read up to 10 bytes from the input

ReadVarString

string ReadVarString()

Reads an utf-8 encoded string prefixed by a variable-sized length

string ReadVarString(Encoding encoding)

Reads a string prefixed by a variable-sized length, using the specified encoding

Encoding used for this string (or UTF-8 if null)

ReadWhile

Slice ReadWhile(Func<byte, int, bool> handler)

Read until handler returns true, or we reach the end of the buffer

Skip

void Skip(int count)

Skip the next count bytes of the buffer

TryCopyBytes

bool TryCopyBytes(int count, Span<byte> buffer)

Read the next count bytes from the buffer, unless we already reached the end.

TryPeekBytes

bool TryPeekBytes(int count, out Slice bytes)

Tries reading the next count bytes from the reader, without advancing the cursor

  • count — Number of bytes to peek
  • bytes — Receives the corresponding slice if there are enough bytes remaining.

Returns: If true, the next count are available in bytes. If false, there are not enough bytes remaining in the buffer.

TryPeekInt16

bool TryPeekInt16(out short value)

Tries reading the next 2 bytes as a signed 16-bit integer, using little-endian encoding, without advancing the cursor

TryPeekInt16BE

bool TryPeekInt16BE(out short value)

Tries reading the next 2 bytes as a signed 16-bit integer, using big-endian encoding, without advancing the cursor

TryPeekInt32

bool TryPeekInt32(out int value)

Tries reading the next 4 bytes as a signed 32-bit integer, using little-endian encoding, without advancing the cursor

TryPeekInt32BE

bool TryPeekInt32BE(out int value)

Tries reading the next 4 bytes as a signed 32-bit integer, using big-endian encoding, without advancing the cursor

TryPeekInt64

bool TryPeekInt64(out long value)

Tries reading the next 8 bytes as a signed 64-bit integer, using little-endian encoding, without advancing the cursor

TryPeekInt64BE

bool TryPeekInt64BE(out long value)

Tries reading the next 8 bytes as a signed 64-bit integer, using big-endian encoding, without advancing the cursor

TryPeekUInt16

bool TryPeekUInt16(out ushort value)

Tries reading the next 2 bytes as an unsigned 16-bit integer, using little-endian encoding, without advancing the cursor

TryPeekUInt16BE

bool TryPeekUInt16BE(out ushort value)

Tries reading the next 2 bytes as an unsigned 16-bit integer, using big-endian encoding, without advancing the cursor

TryPeekUInt32

bool TryPeekUInt32(out uint value)

Tries reading the next 4 bytes as an unsigned 32-bit integer, using little-endian encoding, without advancing the cursor

TryPeekUInt32BE

bool TryPeekUInt32BE(out uint value)

Tries reading the next 4 bytes as an unsigned 32-bit integer, using big-endian encoding, without advancing the cursor

TryPeekUInt64

bool TryPeekUInt64(out ulong value)

Tries reading the next 8 bytes as an unsigned 64-bit integer, using little-endian encoding, without advancing the cursor

TryPeekUInt64BE

bool TryPeekUInt64BE(out ulong value)

Tries reading the next 4 bytes as an unsigned 64-bit integer, using big-endian encoding, without advancing the cursor

TryReadByte

bool TryReadByte(out byte value)

Read the next byte from the buffer, unless we already reached the end.

TryReadByteString

bool TryReadByteString(out Slice bytes)

Read an encoded nul-terminated byte array from the buffer

TryReadBytes

bool TryReadBytes(int count, out Slice value)

Read the next count bytes from the buffer, unless we already reached the end.

bool TryReadBytes(int count, out ReadOnlySpan<byte> value)

Read the next count bytes from the buffer, unless we already reached the end.

TryReadDouble

bool TryReadDouble(out double value)

Reads the next 8 bytes as an IEEE 64-bit floating point number, encoded in little-endian, unless we already reached the end.

TryReadDoubleBE

bool TryReadDoubleBE(out double value)

Reads the next 8 bytes as an IEEE 64-bit floating point number, encoded in big-endian, unless we already reached the end.

TryReadFixed128

bool TryReadFixed128(out UInt128 value)

Reads the next 16 bytes as an unsigned 128-bit integer, encoded in little-endian, unless we already reached the end.

TryReadFixed128BE

bool TryReadFixed128BE(out UInt128 value)

Reads the next 16 bytes as an unsigned 128-bit integer, encoded in big-endian, unless we already reached the end.

TryReadFixed16

bool TryReadFixed16(out ushort value)

Reads the next 2 bytes as an unsigned 16-bit integer, encoded in little-endian, unless we already reached the end.

TryReadFixed16BE

bool TryReadFixed16BE(out ushort value)

Reads the next 2 bytes as an unsigned 16-bit integer, encoded in big-endian, unless we already reached the end.

TryReadFixed32

bool TryReadFixed32(out uint value)

Reads the next 4 bytes as an unsigned 32-bit integer, encoded in little-endian, unless we already reached the end.

TryReadFixed32BE

bool TryReadFixed32BE(out uint value)

Reads the next 4 bytes as an unsigned 32-bit integer, encoded in big-endian, unless we already reached the end.

TryReadFixed64

bool TryReadFixed64(out ulong value)

Reads the next 8 bytes as an unsigned 64-bit integer, encoded in little-endian, unless we already reached the end.

TryReadFixed64BE

bool TryReadFixed64BE(out ulong value)

Reads the next 8 bytes as an unsigned 64-bit integer, encoded in big-endian, unless we already reached the end.

TryReadHalf

bool TryReadHalf(out Half value)

Reads the next 4 bytes as an IEEE 32-bit floating point number, encoded in little-endian, unless we already reached the end.

TryReadHalfBE

bool TryReadHalfBE(out Half value)

Reads the next 4 bytes as an IEEE 32-bit floating point number, encoded in big-endian, unless we already reached the end.

TryReadInt128

bool TryReadInt128(out Int128 value)

Reads the next 16 bytes as an unsigned 128-bit integer, encoded in little-endian, unless we already reached the end.

TryReadInt128BE

bool TryReadInt128BE(out Int128 value)

Reads the next 16 bytes as a signed 128-bit integer, encoded in big-endian, unless we already reached the end.

TryReadInt16

bool TryReadInt16(out short value)

Reads the next 2 bytes as a signed 16-bit integer, encoded in little-endian, unless we already reached the end.

TryReadInt16BE

bool TryReadInt16BE(out short value)

Reads the next 2 bytes as a signed 16-bit integer, encoded in big-endian, unless we already reached the end.

TryReadInt24

bool TryReadInt24(out int value)

Reads the next 3 bytes as a signed 24-bit integer, encoded in little-endian, unless we already reached the end.

TryReadInt24BE

bool TryReadInt24BE(out int value)

Reads the next 3 bytes as a signed 24-bit integer, encoded in big-endian, unless we already reached the end.

TryReadInt32

bool TryReadInt32(out int value)

Reads the next 4 bytes as a signed 32-bit integer, encoded in little-endian, unless we already reached the end.

TryReadInt32BE

bool TryReadInt32BE(out int value)

Reads the next 4 bytes as an unsigned 32-bit integer, encoded in big-endian, unless we already reached the end.

TryReadInt64

bool TryReadInt64(out long value)

Reads the next 8 bytes as a signed 64-bit integer, encoded in little-endian, unless we already reached the end.

TryReadInt64BE

bool TryReadInt64BE(out long value)

Reads the next 8 bytes as a signed 64-bit integer, encoded in big-endian, unless we already reached the end.

TryReadSingle

bool TryReadSingle(out float value)

Reads the next 4 bytes as an IEEE 32-bit floating point number, encoded in little-endian, unless we already reached the end.

TryReadSingleBE

bool TryReadSingleBE(out float value)

Reads the next 4 bytes as an IEEE 32-bit floating point number, encoded in big-endian, unless we already reached the end.

TryReadUInt128

bool TryReadUInt128(out UInt128 value)

Reads the next 16 bytes as an unsigned 128-bit integer, encoded in little-endian, unless we already reached the end.

TryReadUInt128BE

bool TryReadUInt128BE(out UInt128 value)

Reads the next 16 bytes as an unsigned 128-bit integer, encoded in big-endian, unless we already reached the end.

TryReadUInt16

bool TryReadUInt16(out ushort value)

Reads the next 2 bytes as an unsigned 16-bit integer, encoded in little-endian, unless we already reached the end.

TryReadUInt16BE

bool TryReadUInt16BE(out ushort value)

Reads the next 2 bytes as an unsigned 16-bit integer, encoded in big-endian, unless we already reached the end.

TryReadUInt24

bool TryReadUInt24(out uint value)

Reads the next 3 bytes as an unsigned 24-bit integer, encoded in little-endian, unless we already reached the end.

TryReadUInt24BE

bool TryReadUInt24BE(out uint value)

Reads the next 3 bytes as an unsigned 24-bit integer, encoded in big-endian, unless we already reached the end.

TryReadUInt32

bool TryReadUInt32(out uint value)

Reads the next 4 bytes as an unsigned 32-bit integer, encoded in little-endian, unless we already reached the end.

TryReadUInt32BE

bool TryReadUInt32BE(out uint value)

Reads the next 4 bytes as an unsigned 32-bit integer, encoded in big-endian, unless we already reached the end.

TryReadUInt64

bool TryReadUInt64(out ulong value)

Reads the next 8 bytes as an unsigned 64-bit integer, encoded in little-endian, unless we already reached the end.

TryReadUInt64BE

bool TryReadUInt64BE(out ulong value)

Reads the next 8 bytes as an unsigned 64-bit integer, encoded in big-endian, unless we already reached the end.

TryReadVarBytes

bool TryReadVarBytes(out Slice bytes)

Attempts to read a variable sized slice, by first reading its size (stored as a Varint32) and then the data.

  • bytes — Decoded bytes, if successful.

Returns: true if the read was successful, or false if the length is incorrect, or the buffer is too small.

TryReadVarInt32

bool TryReadVarInt32(out uint value)

Reads a 7-bit encoded unsigned int (aka 'Varint32') from the buffer, and advances the cursor if successful.

Can Read up to 5 bytes from the input

TryReadVarInt64

bool TryReadVarInt64(out ulong value)

Reads a 7-bit encoded unsigned long (aka 'Varint64') from the buffer, and advances the cursor if successful.

Can Read up to 5 bytes from the input

Fields

Buffer

readonly Slice Buffer

Buffer containing the tuple being parsed

Position

int Position

Current position inside the buffer