// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package bytes

import (
	
	
	
)

// A Reader implements the [io.Reader], [io.ReaderAt], [io.WriterTo], [io.Seeker],
// [io.ByteScanner], and [io.RuneScanner] interfaces by reading from
// a byte slice.
// Unlike a [Buffer], a Reader is read-only and supports seeking.
// The zero value for Reader operates like a Reader of an empty slice.
type Reader struct {
	s        []byte
	i        int64 // current reading index
	prevRune int   // index of previous rune; or < 0
}

// Len returns the number of bytes of the unread portion of the
// slice.
func ( *Reader) () int {
	if .i >= int64(len(.s)) {
		return 0
	}
	return int(int64(len(.s)) - .i)
}

// Size returns the original length of the underlying byte slice.
// Size is the number of bytes available for reading via [Reader.ReadAt].
// The result is unaffected by any method calls except [Reader.Reset].
func ( *Reader) () int64 { return int64(len(.s)) }

// Read implements the [io.Reader] interface.
func ( *Reader) ( []byte) ( int,  error) {
	if .i >= int64(len(.s)) {
		return 0, io.EOF
	}
	.prevRune = -1
	 = copy(, .s[.i:])
	.i += int64()
	return
}

// ReadAt implements the [io.ReaderAt] interface.
func ( *Reader) ( []byte,  int64) ( int,  error) {
	// cannot modify state - see io.ReaderAt
	if  < 0 {
		return 0, errors.New("bytes.Reader.ReadAt: negative offset")
	}
	if  >= int64(len(.s)) {
		return 0, io.EOF
	}
	 = copy(, .s[:])
	if  < len() {
		 = io.EOF
	}
	return
}

// ReadByte implements the [io.ByteReader] interface.
func ( *Reader) () (byte, error) {
	.prevRune = -1
	if .i >= int64(len(.s)) {
		return 0, io.EOF
	}
	 := .s[.i]
	.i++
	return , nil
}

// UnreadByte complements [Reader.ReadByte] in implementing the [io.ByteScanner] interface.
func ( *Reader) () error {
	if .i <= 0 {
		return errors.New("bytes.Reader.UnreadByte: at beginning of slice")
	}
	.prevRune = -1
	.i--
	return nil
}

// ReadRune implements the [io.RuneReader] interface.
func ( *Reader) () ( rune,  int,  error) {
	if .i >= int64(len(.s)) {
		.prevRune = -1
		return 0, 0, io.EOF
	}
	.prevRune = int(.i)
	if  := .s[.i];  < utf8.RuneSelf {
		.i++
		return rune(), 1, nil
	}
	,  = utf8.DecodeRune(.s[.i:])
	.i += int64()
	return
}

// UnreadRune complements [Reader.ReadRune] in implementing the [io.RuneScanner] interface.
func ( *Reader) () error {
	if .i <= 0 {
		return errors.New("bytes.Reader.UnreadRune: at beginning of slice")
	}
	if .prevRune < 0 {
		return errors.New("bytes.Reader.UnreadRune: previous operation was not ReadRune")
	}
	.i = int64(.prevRune)
	.prevRune = -1
	return nil
}

// Seek implements the [io.Seeker] interface.
func ( *Reader) ( int64,  int) (int64, error) {
	.prevRune = -1
	var  int64
	switch  {
	case io.SeekStart:
		 = 
	case io.SeekCurrent:
		 = .i + 
	case io.SeekEnd:
		 = int64(len(.s)) + 
	default:
		return 0, errors.New("bytes.Reader.Seek: invalid whence")
	}
	if  < 0 {
		return 0, errors.New("bytes.Reader.Seek: negative position")
	}
	.i = 
	return , nil
}

// WriteTo implements the [io.WriterTo] interface.
func ( *Reader) ( io.Writer) ( int64,  error) {
	.prevRune = -1
	if .i >= int64(len(.s)) {
		return 0, nil
	}
	 := .s[.i:]
	,  := .Write()
	if  > len() {
		panic("bytes.Reader.WriteTo: invalid Write count")
	}
	.i += int64()
	 = int64()
	if  != len() &&  == nil {
		 = io.ErrShortWrite
	}
	return
}

// Reset resets the [Reader] to be reading from b.
func ( *Reader) ( []byte) { * = Reader{, 0, -1} }

// NewReader returns a new [Reader] reading from b.
func ( []byte) *Reader { return &Reader{, 0, -1} }