// Copyright 2009 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 strings

import (
	
	
	
)

// A Reader implements the [io.Reader], [io.ReaderAt], [io.ByteReader], [io.ByteScanner],
// [io.RuneReader], [io.RuneScanner], [io.Seeker], and [io.WriterTo] interfaces by reading
// from a string.
// The zero value for Reader operates like a Reader of an empty string.
type Reader struct {
	s        string
	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
// string.
func ( *Reader) () int {
	if .i >= int64(len(.s)) {
		return 0
	}
	return int(int64(len(.s)) - .i)
}

// Size returns the original length of the underlying string.
// Size is the number of bytes available for reading via [Reader.ReadAt].
// The returned value is always the same and is not affected by calls
// to any other method.
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("strings.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 implements the [io.ByteScanner] interface.
func ( *Reader) () error {
	if .i <= 0 {
		return errors.New("strings.Reader.UnreadByte: at beginning of string")
	}
	.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.DecodeRuneInString(.s[.i:])
	.i += int64()
	return
}

// UnreadRune implements the [io.RuneScanner] interface.
func ( *Reader) () error {
	if .i <= 0 {
		return errors.New("strings.Reader.UnreadRune: at beginning of string")
	}
	if .prevRune < 0 {
		return errors.New("strings.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("strings.Reader.Seek: invalid whence")
	}
	if  < 0 {
		return 0, errors.New("strings.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:]
	,  := io.WriteString(, )
	if  > len() {
		panic("strings.Reader.WriteTo: invalid WriteString count")
	}
	.i += int64()
	 = int64()
	if  != len() &&  == nil {
		 = io.ErrShortWrite
	}
	return
}

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

// NewReader returns a new [Reader] reading from s.
// It is similar to [bytes.NewBufferString] but more efficient and non-writable.
func ( string) *Reader { return &Reader{, 0, -1} }