diff options
Diffstat (limited to 'src/pkg/bytes/reader.go')
-rw-r--r-- | src/pkg/bytes/reader.go | 52 |
1 files changed, 26 insertions, 26 deletions
diff --git a/src/pkg/bytes/reader.go b/src/pkg/bytes/reader.go index 77511b945..d2d40fa7c 100644 --- a/src/pkg/bytes/reader.go +++ b/src/pkg/bytes/reader.go @@ -16,40 +16,41 @@ import ( // Unlike a Buffer, a Reader is read-only and supports seeking. type Reader struct { s []byte - i int // current reading index - prevRune int // index of previous rune; or < 0 + 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 (r *Reader) Len() int { - if r.i >= len(r.s) { + if r.i >= int64(len(r.s)) { return 0 } - return len(r.s) - r.i + return int(int64(len(r.s)) - r.i) } func (r *Reader) Read(b []byte) (n int, err error) { if len(b) == 0 { return 0, nil } - if r.i >= len(r.s) { + if r.i >= int64(len(r.s)) { return 0, io.EOF } - n = copy(b, r.s[r.i:]) - r.i += n r.prevRune = -1 + n = copy(b, r.s[r.i:]) + r.i += int64(n) return } func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) { + // cannot modify state - see io.ReaderAt if off < 0 { - return 0, errors.New("bytes: invalid offset") + return 0, errors.New("bytes.Reader.ReadAt: negative offset") } if off >= int64(len(r.s)) { return 0, io.EOF } - n = copy(b, r.s[int(off):]) + n = copy(b, r.s[off:]) if n < len(b) { err = io.EOF } @@ -57,49 +58,51 @@ func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) { } func (r *Reader) ReadByte() (b byte, err error) { - if r.i >= len(r.s) { + r.prevRune = -1 + if r.i >= int64(len(r.s)) { return 0, io.EOF } b = r.s[r.i] r.i++ - r.prevRune = -1 return } func (r *Reader) UnreadByte() error { + r.prevRune = -1 if r.i <= 0 { - return errors.New("bytes.Reader: at beginning of slice") + return errors.New("bytes.Reader.UnreadByte: at beginning of slice") } r.i-- - r.prevRune = -1 return nil } func (r *Reader) ReadRune() (ch rune, size int, err error) { - if r.i >= len(r.s) { + if r.i >= int64(len(r.s)) { + r.prevRune = -1 return 0, 0, io.EOF } - r.prevRune = r.i + r.prevRune = int(r.i) if c := r.s[r.i]; c < utf8.RuneSelf { r.i++ return rune(c), 1, nil } ch, size = utf8.DecodeRune(r.s[r.i:]) - r.i += size + r.i += int64(size) return } func (r *Reader) UnreadRune() error { if r.prevRune < 0 { - return errors.New("bytes.Reader: previous operation was not ReadRune") + return errors.New("bytes.Reader.UnreadRune: previous operation was not ReadRune") } - r.i = r.prevRune + r.i = int64(r.prevRune) r.prevRune = -1 return nil } // Seek implements the io.Seeker interface. func (r *Reader) Seek(offset int64, whence int) (int64, error) { + r.prevRune = -1 var abs int64 switch whence { case 0: @@ -109,22 +112,19 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) { case 2: abs = int64(len(r.s)) + offset default: - return 0, errors.New("bytes: invalid whence") + return 0, errors.New("bytes.Reader.Seek: invalid whence") } if abs < 0 { - return 0, errors.New("bytes: negative position") - } - if abs >= 1<<31 { - return 0, errors.New("bytes: position out of range") + return 0, errors.New("bytes.Reader.Seek: negative position") } - r.i = int(abs) + r.i = abs return abs, nil } // WriteTo implements the io.WriterTo interface. func (r *Reader) WriteTo(w io.Writer) (n int64, err error) { r.prevRune = -1 - if r.i >= len(r.s) { + if r.i >= int64(len(r.s)) { return 0, nil } b := r.s[r.i:] @@ -132,7 +132,7 @@ func (r *Reader) WriteTo(w io.Writer) (n int64, err error) { if m > len(b) { panic("bytes.Reader.WriteTo: invalid Write count") } - r.i += m + r.i += int64(m) n = int64(m) if m != len(b) && err == nil { err = io.ErrShortWrite |