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

151 lines
3.3 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.
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) {
if len(b) == 0 {
return 0, nil
}
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() (b byte, err 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]
r.i++
return
}
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("strings.Reader.UnreadByte: at beginning of string")
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 {
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 0:
abs = offset
case 1:
abs = int64(r.i) + offset
case 2:
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
}
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} }