Retro68/gcc/libgo/go/strings/reader.go

155 lines
3.5 KiB
Go
Raw Normal View History

2012-03-27 23:13:14 +00:00
// 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 strings
import (
"errors"
"io"
"unicode/utf8"
)
2014-09-21 17:33:12 +00:00
// A Reader implements the io.Reader, io.ReaderAt, io.Seeker, io.WriterTo,
2012-03-27 23:13:14 +00:00
// io.ByteScanner, and io.RuneScanner interfaces by reading
// from a string.
2019-06-02 15:48:37 +00:00
// The zero value for Reader operates like a Reader of an empty string.
2012-03-27 23:13:14 +00:00
type Reader struct {
s string
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
// string.
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 string.
// 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)) }
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
}
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("strings.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
}
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
}
func (r *Reader) UnreadByte() error {
if r.i <= 0 {
2015-08-28 15:33:40 +00:00
return errors.New("strings.Reader.UnreadByte: at beginning of string")
2012-03-27 23:13:14 +00:00
}
2019-06-02 15:48:37 +00:00
r.prevRune = -1
2012-03-27 23:13:14 +00:00
r.i--
return nil
}
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.DecodeRuneInString(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
}
func (r *Reader) UnreadRune() error {
2019-06-02 15:48:37 +00:00
if r.i <= 0 {
return errors.New("strings.Reader.UnreadRune: at beginning of string")
}
2012-03-27 23:13:14 +00:00
if r.prevRune < 0 {
2015-08-28 15:33:40 +00:00
return errors.New("strings.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("strings.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("strings.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
}
s := r.s[r.i:]
m, err := io.WriteString(w, s)
if m > len(s) {
panic("strings.Reader.WriteTo: invalid WriteString 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(s) && err == nil {
err = io.ErrShortWrite
}
return
}
// Reset resets the Reader to be reading from s.
func (r *Reader) Reset(s string) { *r = Reader{s, 0, -1} }
2012-03-27 23:13:14 +00:00
// NewReader returns a new Reader reading from s.
// It is similar to bytes.NewBufferString but more efficient and read-only.
func NewReader(s string) *Reader { return &Reader{s, 0, -1} }