// 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 stringsimport ()// 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.typeReaderstruct { 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)) {return0 }returnint(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 { returnint64(len(.s)) }// Read implements the [io.Reader] interface.func ( *Reader) ( []byte) ( int, error) {if .i >= int64(len(.s)) {return0, 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.ReaderAtif < 0 {return0, errors.New("strings.Reader.ReadAt: negative offset") }if >= int64(len(.s)) {return0, io.EOF } = copy(, .s[:])if < len() { = io.EOF }return}// ReadByte implements the [io.ByteReader] interface.func ( *Reader) () (byte, error) { .prevRune = -1if .i >= int64(len(.s)) {return0, io.EOF } := .s[.i] .i++return , nil}// UnreadByte implements the [io.ByteScanner] interface.func ( *Reader) () error {if .i <= 0 {returnerrors.New("strings.Reader.UnreadByte: at beginning of string") } .prevRune = -1 .i--returnnil}// ReadRune implements the [io.RuneReader] interface.func ( *Reader) () ( rune, int, error) {if .i >= int64(len(.s)) { .prevRune = -1return0, 0, io.EOF } .prevRune = int(.i)if := .s[.i]; < utf8.RuneSelf { .i++returnrune(), 1, nil } , = utf8.DecodeRuneInString(.s[.i:]) .i += int64()return}// UnreadRune implements the [io.RuneScanner] interface.func ( *Reader) () error {if .i <= 0 {returnerrors.New("strings.Reader.UnreadRune: at beginning of string") }if .prevRune < 0 {returnerrors.New("strings.Reader.UnreadRune: previous operation was not ReadRune") } .i = int64(.prevRune) .prevRune = -1returnnil}// Seek implements the [io.Seeker] interface.func ( *Reader) ( int64, int) (int64, error) { .prevRune = -1varint64switch {caseio.SeekStart: = caseio.SeekCurrent: = .i + caseio.SeekEnd: = int64(len(.s)) + default:return0, errors.New("strings.Reader.Seek: invalid whence") }if < 0 {return0, errors.New("strings.Reader.Seek: negative position") } .i = return , nil}// WriteTo implements the [io.WriterTo] interface.func ( *Reader) ( io.Writer) ( int64, error) { .prevRune = -1if .i >= int64(len(.s)) {return0, 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} }
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.