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
|
|
|
|
i int // current reading index
|
|
|
|
prevRune int // index of previous rune; or < 0
|
|
|
|
}
|
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 {
|
2012-02-08 23:28:41 -07:00
|
|
|
if r.i >= len(r.s) {
|
|
|
|
return 0
|
|
|
|
}
|
2011-05-31 09:47:03 -06:00
|
|
|
return len(r.s) - r.i
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2011-05-26 12:02:07 -06:00
|
|
|
if r.i >= 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:])
|
|
|
|
r.i += 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) {
|
|
|
|
if off < 0 {
|
|
|
|
return 0, errors.New("strings: invalid offset")
|
|
|
|
}
|
|
|
|
if off >= int64(len(r.s)) {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
n = copy(b, r.s[int(off):])
|
|
|
|
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
|
2011-05-26 12:02:07 -06:00
|
|
|
if r.i >= 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 {
|
2011-05-26 12:02:07 -06:00
|
|
|
if r.i <= 0 {
|
2011-11-01 20:05:34 -06:00
|
|
|
return errors.New("strings.Reader: at beginning of string")
|
2011-05-26 12:02:07 -06:00
|
|
|
}
|
|
|
|
r.i--
|
|
|
|
r.prevRune = -1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (r *Reader) ReadRune() (ch rune, size int, err error) {
|
2011-05-26 12:02:07 -06:00
|
|
|
if r.i >= 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
|
|
|
}
|
2011-05-26 12:02:07 -06:00
|
|
|
r.prevRune = r.i
|
|
|
|
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:])
|
2011-05-26 12:02:07 -06:00
|
|
|
r.i += 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 {
|
2011-11-01 20:05:34 -06:00
|
|
|
return errors.New("strings.Reader: previous operation was not ReadRune")
|
2011-05-26 12:02:07 -06:00
|
|
|
}
|
|
|
|
r.i = r.prevRune
|
|
|
|
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:
|
|
|
|
return 0, errors.New("strings: invalid whence")
|
|
|
|
}
|
|
|
|
if abs < 0 {
|
|
|
|
return 0, errors.New("strings: negative position")
|
|
|
|
}
|
|
|
|
if abs >= 1<<31 {
|
|
|
|
return 0, errors.New("strings: position out of range")
|
|
|
|
}
|
|
|
|
r.i = int(abs)
|
|
|
|
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
|
|
|
|
if r.i >= 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")
|
|
|
|
}
|
|
|
|
r.i += m
|
|
|
|
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} }
|