github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/bytes/reader.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package bytes 6 7 import ( 8 "errors" 9 "io" 10 "unicode/utf8" 11 ) 12 13 // A Reader implements the io.Reader, io.ReaderAt, io.WriterTo, io.Seeker, 14 // io.ByteScanner, and io.RuneScanner interfaces by reading from 15 // a byte slice. 16 // Unlike a Buffer, a Reader is read-only and supports seeking. 17 type Reader struct { 18 s []byte 19 i int64 // current reading index 20 prevRune int // index of previous rune; or < 0 21 } 22 23 // Len returns the number of bytes of the unread portion of the 24 // slice. 25 func (r *Reader) Len() int { 26 if r.i >= int64(len(r.s)) { 27 return 0 28 } 29 return int(int64(len(r.s)) - r.i) 30 } 31 32 // Size returns the original length of the underlying byte slice. 33 // Size is the number of bytes available for reading via ReadAt. 34 // The returned value is always the same and is not affected by calls 35 // to any other method. 36 func (r *Reader) Size() int64 { return int64(len(r.s)) } 37 38 func (r *Reader) Read(b []byte) (n int, err error) { 39 if r.i >= int64(len(r.s)) { 40 return 0, io.EOF 41 } 42 r.prevRune = -1 43 n = copy(b, r.s[r.i:]) 44 r.i += int64(n) 45 return 46 } 47 48 func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) { 49 // cannot modify state - see io.ReaderAt 50 if off < 0 { 51 return 0, errors.New("bytes.Reader.ReadAt: negative offset") 52 } 53 if off >= int64(len(r.s)) { 54 return 0, io.EOF 55 } 56 n = copy(b, r.s[off:]) 57 if n < len(b) { 58 err = io.EOF 59 } 60 return 61 } 62 63 func (r *Reader) ReadByte() (byte, error) { 64 r.prevRune = -1 65 if r.i >= int64(len(r.s)) { 66 return 0, io.EOF 67 } 68 b := r.s[r.i] 69 r.i++ 70 return b, nil 71 } 72 73 func (r *Reader) UnreadByte() error { 74 r.prevRune = -1 75 if r.i <= 0 { 76 return errors.New("bytes.Reader.UnreadByte: at beginning of slice") 77 } 78 r.i-- 79 return nil 80 } 81 82 func (r *Reader) ReadRune() (ch rune, size int, err error) { 83 if r.i >= int64(len(r.s)) { 84 r.prevRune = -1 85 return 0, 0, io.EOF 86 } 87 r.prevRune = int(r.i) 88 if c := r.s[r.i]; c < utf8.RuneSelf { 89 r.i++ 90 return rune(c), 1, nil 91 } 92 ch, size = utf8.DecodeRune(r.s[r.i:]) 93 r.i += int64(size) 94 return 95 } 96 97 func (r *Reader) UnreadRune() error { 98 if r.prevRune < 0 { 99 return errors.New("bytes.Reader.UnreadRune: previous operation was not ReadRune") 100 } 101 r.i = int64(r.prevRune) 102 r.prevRune = -1 103 return nil 104 } 105 106 // Seek implements the io.Seeker interface. 107 func (r *Reader) Seek(offset int64, whence int) (int64, error) { 108 r.prevRune = -1 109 var abs int64 110 switch whence { 111 case io.SeekStart: 112 abs = offset 113 case io.SeekCurrent: 114 abs = r.i + offset 115 case io.SeekEnd: 116 abs = int64(len(r.s)) + offset 117 default: 118 return 0, errors.New("bytes.Reader.Seek: invalid whence") 119 } 120 if abs < 0 { 121 return 0, errors.New("bytes.Reader.Seek: negative position") 122 } 123 r.i = abs 124 return abs, nil 125 } 126 127 // WriteTo implements the io.WriterTo interface. 128 func (r *Reader) WriteTo(w io.Writer) (n int64, err error) { 129 r.prevRune = -1 130 if r.i >= int64(len(r.s)) { 131 return 0, nil 132 } 133 b := r.s[r.i:] 134 m, err := w.Write(b) 135 if m > len(b) { 136 panic("bytes.Reader.WriteTo: invalid Write count") 137 } 138 r.i += int64(m) 139 n = int64(m) 140 if m != len(b) && err == nil { 141 err = io.ErrShortWrite 142 } 143 return 144 } 145 146 // Reset resets the Reader to be reading from b. 147 func (r *Reader) Reset(b []byte) { *r = Reader{b, 0, -1} } 148 149 // NewReader returns a new Reader reading from b. 150 func NewReader(b []byte) *Reader { return &Reader{b, 0, -1} }