// 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 bytesimport ()// 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.typeReaderstruct {s []byteiint64// current reading indexprevRuneint// 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)) {return0 }returnint(int64(len(.s)) - .i)}// Size returns the original length of the underlying byte slice.// Size is the number of bytes available for reading via ReadAt.// The result is unaffected by any method calls except Reset.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("bytes.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 complements ReadByte in implementing the io.ByteScanner interface.func ( *Reader) () error {if .i <= 0 {returnerrors.New("bytes.Reader.UnreadByte: at beginning of slice") } .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.DecodeRune(.s[.i:]) .i += int64()return}// UnreadRune complements ReadRune in implementing the io.RuneScanner interface.func ( *Reader) () error {if .i <= 0 {returnerrors.New("bytes.Reader.UnreadRune: at beginning of slice") }if .prevRune < 0 {returnerrors.New("bytes.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("bytes.Reader.Seek: invalid whence") }if < 0 {return0, errors.New("bytes.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:] , := .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} }
The pages are generated with Goldsv0.6.7. (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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds.