2009-10-12 11:09:35 -06: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
|
|
|
|
|
2010-04-20 23:18:26 -06:00
|
|
|
import (
|
2011-11-01 20:05:34 -06:00
|
|
|
"errors"
|
|
|
|
"io"
|
2011-11-08 16:41:54 -07:00
|
|
|
"unicode/utf8"
|
2010-04-20 23:18:26 -06:00
|
|
|
)
|
2009-10-12 11:09:35 -06:00
|
|
|
|
2012-10-11 21:43:50 -06:00
|
|
|
// A Reader implements the io.Reader, io.ReaderAt, io.Seeker, io.WriterTo,
|
2012-02-14 18:58:00 -07:00
|
|
|
// io.ByteScanner, and io.RuneScanner interfaces by reading
|
|
|
|
// from a string.
|
2011-05-26 12:02:07 -06:00
|
|
|
type Reader struct {
|
|
|
|
s string
|
2014-03-28 13:23:51 -06:00
|
|
|
i int64 // current reading index
|
|
|
|
prevRune int // index of previous rune; or < 0
|
2011-05-26 12:02:07 -06:00
|
|
|
}
|
2009-10-12 11:09:35 -06:00
|
|
|
|
2011-05-31 09:47:03 -06:00
|
|
|
// Len returns the number of bytes of the unread portion of the
|
|
|
|
// string.
|
|
|
|
func (r *Reader) Len() int {
|
2014-03-28 13:23:51 -06:00
|
|
|
if r.i >= int64(len(r.s)) {
|
2012-02-08 23:28:41 -07:00
|
|
|
return 0
|
|
|
|
}
|
2014-03-28 13:23:51 -06:00
|
|
|
return int(int64(len(r.s)) - r.i)
|
2011-05-31 09:47:03 -06:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (r *Reader) Read(b []byte) (n int, err error) {
|
2014-03-19 10:00:58 -06:00
|
|
|
r.prevRune = -1
|
2012-02-08 23:28:41 -07:00
|
|
|
if len(b) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2014-03-28 13:23:51 -06:00
|
|
|
if r.i >= int64(len(r.s)) {
|
2011-11-01 20:05:34 -06:00
|
|
|
return 0, io.EOF
|
2009-10-12 11:09:35 -06:00
|
|
|
}
|
2011-05-26 12:02:07 -06:00
|
|
|
n = copy(b, r.s[r.i:])
|
2014-03-28 13:23:51 -06:00
|
|
|
r.i += int64(n)
|
2009-12-15 16:40:16 -07:00
|
|
|
return
|
2009-10-12 11:09:35 -06:00
|
|
|
}
|
|
|
|
|
2012-02-14 18:58:00 -07:00
|
|
|
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {
|
2014-04-10 16:46:07 -06:00
|
|
|
// cannot modify state - see io.ReaderAt
|
2012-02-14 18:58:00 -07:00
|
|
|
if off < 0 {
|
2014-04-10 22:45:41 -06:00
|
|
|
return 0, errors.New("strings.Reader.ReadAt: negative offset")
|
2012-02-14 18:58:00 -07:00
|
|
|
}
|
|
|
|
if off >= int64(len(r.s)) {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
2014-03-28 13:23:51 -06:00
|
|
|
n = copy(b, r.s[off:])
|
2012-02-14 18:58:00 -07:00
|
|
|
if n < len(b) {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (r *Reader) ReadByte() (b byte, err error) {
|
2014-03-19 10:00:58 -06:00
|
|
|
r.prevRune = -1
|
2014-03-28 13:23:51 -06:00
|
|
|
if r.i >= int64(len(r.s)) {
|
2011-11-01 20:05:34 -06:00
|
|
|
return 0, io.EOF
|
2009-10-12 11:09:35 -06:00
|
|
|
}
|
2011-05-26 12:02:07 -06:00
|
|
|
b = r.s[r.i]
|
|
|
|
r.i++
|
2009-12-15 16:40:16 -07:00
|
|
|
return
|
2009-10-12 11:09:35 -06:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (r *Reader) UnreadByte() error {
|
2014-04-10 22:45:41 -06:00
|
|
|
r.prevRune = -1
|
2011-05-26 12:02:07 -06:00
|
|
|
if r.i <= 0 {
|
2014-04-10 22:45:41 -06:00
|
|
|
return errors.New("strings.Reader.UnreadByte: at beginning of string")
|
2011-05-26 12:02:07 -06:00
|
|
|
}
|
|
|
|
r.i--
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (r *Reader) ReadRune() (ch rune, size int, err error) {
|
2014-03-28 13:23:51 -06:00
|
|
|
if r.i >= int64(len(r.s)) {
|
2014-03-19 10:00:58 -06:00
|
|
|
r.prevRune = -1
|
2011-11-01 20:05:34 -06:00
|
|
|
return 0, 0, io.EOF
|
2010-04-20 23:18:26 -06:00
|
|
|
}
|
2014-03-28 13:23:51 -06:00
|
|
|
r.prevRune = int(r.i)
|
2011-05-26 12:02:07 -06:00
|
|
|
if c := r.s[r.i]; c < utf8.RuneSelf {
|
|
|
|
r.i++
|
2011-10-25 23:22:09 -06:00
|
|
|
return rune(c), 1, nil
|
2010-04-20 23:18:26 -06:00
|
|
|
}
|
2011-10-25 23:22:09 -06:00
|
|
|
ch, size = utf8.DecodeRuneInString(r.s[r.i:])
|
2014-03-28 13:23:51 -06:00
|
|
|
r.i += int64(size)
|
2010-04-20 23:18:26 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (r *Reader) UnreadRune() error {
|
2011-05-26 12:02:07 -06:00
|
|
|
if r.prevRune < 0 {
|
2014-04-10 22:45:41 -06:00
|
|
|
return errors.New("strings.Reader.UnreadRune: previous operation was not ReadRune")
|
2011-05-26 12:02:07 -06:00
|
|
|
}
|
2014-03-28 13:23:51 -06:00
|
|
|
r.i = int64(r.prevRune)
|
2011-05-26 12:02:07 -06:00
|
|
|
r.prevRune = -1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-02-08 23:28:41 -07:00
|
|
|
// Seek implements the io.Seeker interface.
|
|
|
|
func (r *Reader) Seek(offset int64, whence int) (int64, error) {
|
2014-03-19 10:00:58 -06:00
|
|
|
r.prevRune = -1
|
2012-02-08 23:28:41 -07: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:
|
2014-04-10 22:45:41 -06:00
|
|
|
return 0, errors.New("strings.Reader.Seek: invalid whence")
|
2012-02-08 23:28:41 -07:00
|
|
|
}
|
|
|
|
if abs < 0 {
|
2014-04-10 22:45:41 -06:00
|
|
|
return 0, errors.New("strings.Reader.Seek: negative position")
|
2012-02-08 23:28:41 -07:00
|
|
|
}
|
2014-03-28 13:23:51 -06:00
|
|
|
r.i = abs
|
2012-02-08 23:28:41 -07:00
|
|
|
return abs, nil
|
|
|
|
}
|
|
|
|
|
2012-10-11 21:43:50 -06:00
|
|
|
// WriteTo implements the io.WriterTo interface.
|
|
|
|
func (r *Reader) WriteTo(w io.Writer) (n int64, err error) {
|
|
|
|
r.prevRune = -1
|
2014-03-28 13:23:51 -06:00
|
|
|
if r.i >= int64(len(r.s)) {
|
2012-11-25 10:04:13 -07:00
|
|
|
return 0, nil
|
2012-10-11 21:43:50 -06:00
|
|
|
}
|
|
|
|
s := r.s[r.i:]
|
|
|
|
m, err := io.WriteString(w, s)
|
|
|
|
if m > len(s) {
|
|
|
|
panic("strings.Reader.WriteTo: invalid WriteString count")
|
|
|
|
}
|
2014-03-28 13:23:51 -06:00
|
|
|
r.i += int64(m)
|
2012-10-11 21:43:50 -06:00
|
|
|
n = int64(m)
|
|
|
|
if m != len(s) && err == nil {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2009-10-12 11:09:35 -06:00
|
|
|
// NewReader returns a new Reader reading from s.
|
|
|
|
// It is similar to bytes.NewBufferString but more efficient and read-only.
|
2011-05-26 12:02:07 -06:00
|
|
|
func NewReader(s string) *Reader { return &Reader{s, 0, -1} }
|