// Copyright 2019+ Klaus Post. All rights reserved.
// License information can be found in the LICENSE file.
// Based on work by Yann Collet, released under BSD License.

package zstd

// byteReader provides a byte reader that reads
// little endian values from a byte stream.
// The input stream is manually advanced.
// The reader performs no bounds checks.
type byteReader struct {
	b   []byte
	off int
}

// advance the stream b n bytes.
func ( *byteReader) ( uint) {
	.off += int()
}

// overread returns whether we have advanced too far.
func ( *byteReader) () bool {
	return .off > len(.b)
}

// Int32 returns a little endian int32 starting at current offset.
func ( byteReader) () int32 {
	 := .b[.off:]
	 = [:4]
	 := int32([3])
	 := int32([2])
	 := int32([1])
	 := int32([0])
	return  | ( << 8) | ( << 16) | ( << 24)
}

// Uint8 returns the next byte
func ( *byteReader) () uint8 {
	 := .b[.off]
	return 
}

// Uint32 returns a little endian uint32 starting at current offset.
func ( byteReader) () uint32 {
	if  := .remain();  < 4 {
		// Very rare
		 := uint32(0)
		for  := 1;  <= ; ++ {
			 = ( << 8) | uint32(.b[len(.b)-])
		}
		return 
	}
	 := .b[.off:]
	 = [:4]
	 := uint32([3])
	 := uint32([2])
	 := uint32([1])
	 := uint32([0])
	return  | ( << 8) | ( << 16) | ( << 24)
}

// Uint32NC returns a little endian uint32 starting at current offset.
// The caller must be sure if there are at least 4 bytes left.
func ( byteReader) () uint32 {
	 := .b[.off:]
	 = [:4]
	 := uint32([3])
	 := uint32([2])
	 := uint32([1])
	 := uint32([0])
	return  | ( << 8) | ( << 16) | ( << 24)
}

// unread returns the unread portion of the input.
func ( byteReader) () []byte {
	return .b[.off:]
}

// remain will return the number of bytes remaining.
func ( byteReader) () int {
	return len(.b) - .off
}