Retro68/gcc/libgo/go/bytes/reader.go

157 lines
3.7 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// 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 bytes
import (
"errors"
"io"
"unicode/utf8"
)
2014-09-21 17:33:12 +00:00
// A Reader implements the io.Reader, io.ReaderAt, io.WriterTo, io.Seeker,
2012-03-27 23:13:14 +00:00
// io.ByteScanner, and io.RuneScanner interfaces by reading from
// a byte slice.
// Unlike a Buffer, a Reader is read-only and supports seeking.
type Reader struct {
s []byte
2015-08-28 15:33:40 +00:00
i int64 // current reading index
prevRune int // index of previous rune; or < 0
2012-03-27 23:13:14 +00:00
}
// Len returns the number of bytes of the unread portion of the
// slice.
func (r *Reader) Len() int {
2015-08-28 15:33:40 +00:00
if r.i >= int64(len(r.s)) {
2012-03-27 23:13:14 +00:00
return 0
}
2015-08-28 15:33:40 +00:00
return int(int64(len(r.s)) - r.i)
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
// Size returns the original length of the underlying byte slice.
// Size is the number of bytes available for reading via ReadAt.
// The returned value is always the same and is not affected by calls
// to any other method.
func (r *Reader) Size() int64 { return int64(len(r.s)) }
2018-12-28 15:30:48 +00:00
// Read implements the io.Reader interface.
2012-03-27 23:13:14 +00:00
func (r *Reader) Read(b []byte) (n int, err error) {
2015-08-28 15:33:40 +00:00
if r.i >= int64(len(r.s)) {
2012-03-27 23:13:14 +00:00
return 0, io.EOF
}
r.prevRune = -1
2015-08-28 15:33:40 +00:00
n = copy(b, r.s[r.i:])
r.i += int64(n)
2012-03-27 23:13:14 +00:00
return
}
2018-12-28 15:30:48 +00:00
// ReadAt implements the io.ReaderAt interface.
2012-03-27 23:13:14 +00:00
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
2015-08-28 15:33:40 +00:00
// cannot modify state - see io.ReaderAt
2012-03-27 23:13:14 +00:00
if off < 0 {
2015-08-28 15:33:40 +00:00
return 0, errors.New("bytes.Reader.ReadAt: negative offset")
2012-03-27 23:13:14 +00:00
}
if off >= int64(len(r.s)) {
return 0, io.EOF
}
2015-08-28 15:33:40 +00:00
n = copy(b, r.s[off:])
2012-03-27 23:13:14 +00:00
if n < len(b) {
err = io.EOF
}
return
}
2018-12-28 15:30:48 +00:00
// ReadByte implements the io.ByteReader interface.
func (r *Reader) ReadByte() (byte, error) {
2015-08-28 15:33:40 +00:00
r.prevRune = -1
if r.i >= int64(len(r.s)) {
2012-03-27 23:13:14 +00:00
return 0, io.EOF
}
b := r.s[r.i]
2012-03-27 23:13:14 +00:00
r.i++
return b, nil
2012-03-27 23:13:14 +00:00
}
2018-12-28 15:30:48 +00:00
// UnreadByte complements ReadByte in implementing the io.ByteScanner interface.
2012-03-27 23:13:14 +00:00
func (r *Reader) UnreadByte() error {
2015-08-28 15:33:40 +00:00
r.prevRune = -1
2012-03-27 23:13:14 +00:00
if r.i <= 0 {
2015-08-28 15:33:40 +00:00
return errors.New("bytes.Reader.UnreadByte: at beginning of slice")
2012-03-27 23:13:14 +00:00
}
r.i--
return nil
}
2018-12-28 15:30:48 +00:00
// ReadRune implements the io.RuneReader interface.
2012-03-27 23:13:14 +00:00
func (r *Reader) ReadRune() (ch rune, size int, err error) {
2015-08-28 15:33:40 +00:00
if r.i >= int64(len(r.s)) {
r.prevRune = -1
2012-03-27 23:13:14 +00:00
return 0, 0, io.EOF
}
2015-08-28 15:33:40 +00:00
r.prevRune = int(r.i)
2012-03-27 23:13:14 +00:00
if c := r.s[r.i]; c < utf8.RuneSelf {
r.i++
return rune(c), 1, nil
}
ch, size = utf8.DecodeRune(r.s[r.i:])
2015-08-28 15:33:40 +00:00
r.i += int64(size)
2012-03-27 23:13:14 +00:00
return
}
2018-12-28 15:30:48 +00:00
// UnreadRune complements ReadRune in implementing the io.RuneScanner interface.
2012-03-27 23:13:14 +00:00
func (r *Reader) UnreadRune() error {
if r.prevRune < 0 {
2015-08-28 15:33:40 +00:00
return errors.New("bytes.Reader.UnreadRune: previous operation was not ReadRune")
2012-03-27 23:13:14 +00:00
}
2015-08-28 15:33:40 +00:00
r.i = int64(r.prevRune)
2012-03-27 23:13:14 +00:00
r.prevRune = -1
return nil
}
// Seek implements the io.Seeker interface.
func (r *Reader) Seek(offset int64, whence int) (int64, error) {
2015-08-28 15:33:40 +00:00
r.prevRune = -1
2012-03-27 23:13:14 +00:00
var abs int64
switch whence {
case io.SeekStart:
2012-03-27 23:13:14 +00:00
abs = offset
case io.SeekCurrent:
abs = r.i + offset
case io.SeekEnd:
2012-03-27 23:13:14 +00:00
abs = int64(len(r.s)) + offset
default:
2015-08-28 15:33:40 +00:00
return 0, errors.New("bytes.Reader.Seek: invalid whence")
2012-03-27 23:13:14 +00:00
}
if abs < 0 {
2015-08-28 15:33:40 +00:00
return 0, errors.New("bytes.Reader.Seek: negative position")
2012-03-27 23:13:14 +00:00
}
2015-08-28 15:33:40 +00:00
r.i = abs
2012-03-27 23:13:14 +00:00
return abs, nil
}
2014-09-21 17:33:12 +00:00
// WriteTo implements the io.WriterTo interface.
func (r *Reader) WriteTo(w io.Writer) (n int64, err error) {
r.prevRune = -1
2015-08-28 15:33:40 +00:00
if r.i >= int64(len(r.s)) {
2014-09-21 17:33:12 +00:00
return 0, nil
}
b := r.s[r.i:]
m, err := w.Write(b)
if m > len(b) {
panic("bytes.Reader.WriteTo: invalid Write count")
}
2015-08-28 15:33:40 +00:00
r.i += int64(m)
2014-09-21 17:33:12 +00:00
n = int64(m)
if m != len(b) && err == nil {
err = io.ErrShortWrite
}
return
}
// Reset resets the Reader to be reading from b.
func (r *Reader) Reset(b []byte) { *r = Reader{b, 0, -1} }
2012-03-27 23:13:14 +00:00
// NewReader returns a new Reader reading from b.
func NewReader(b []byte) *Reader { return &Reader{b, 0, -1} }