2010-12-03 05:34:57 +01: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 (
|
2011-12-03 03:17:34 +01:00
|
|
|
"errors"
|
|
|
|
"io"
|
2011-12-07 02:11:29 +01:00
|
|
|
"unicode/utf8"
|
2010-12-03 05:34:57 +01:00
|
|
|
)
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
// A Reader implements the io.Reader, io.ReaderAt, io.Seeker, io.WriterTo,
|
2012-03-02 17:38:43 +01:00
|
|
|
// io.ByteScanner, and io.RuneScanner interfaces by reading
|
|
|
|
// from a string.
|
2011-09-16 17:47:21 +02:00
|
|
|
type Reader struct {
|
|
|
|
s string
|
|
|
|
i int // current reading index
|
|
|
|
prevRune int // index of previous rune; or < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the number of bytes of the unread portion of the
|
|
|
|
// string.
|
|
|
|
func (r *Reader) Len() int {
|
2012-03-02 17:38:43 +01:00
|
|
|
if r.i >= len(r.s) {
|
|
|
|
return 0
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
return len(r.s) - r.i
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (r *Reader) Read(b []byte) (n int, err error) {
|
2012-03-02 17:38:43 +01:00
|
|
|
if len(b) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if r.i >= len(r.s) {
|
2011-12-03 03:17:34 +01:00
|
|
|
return 0, io.EOF
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
n = copy(b, r.s[r.i:])
|
|
|
|
r.i += n
|
|
|
|
r.prevRune = -1
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01: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-12-03 03:17:34 +01:00
|
|
|
func (r *Reader) ReadByte() (b byte, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if r.i >= len(r.s) {
|
2011-12-03 03:17:34 +01:00
|
|
|
return 0, io.EOF
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
b = r.s[r.i]
|
|
|
|
r.i++
|
|
|
|
r.prevRune = -1
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (r *Reader) UnreadByte() error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if r.i <= 0 {
|
2011-12-03 03:17:34 +01:00
|
|
|
return errors.New("strings.Reader: at beginning of string")
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
r.i--
|
|
|
|
r.prevRune = -1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (r *Reader) ReadRune() (ch rune, size int, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if r.i >= len(r.s) {
|
2011-12-03 03:17:34 +01:00
|
|
|
return 0, 0, io.EOF
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
r.prevRune = r.i
|
|
|
|
if c := r.s[r.i]; c < utf8.RuneSelf {
|
|
|
|
r.i++
|
2011-12-02 20:34:41 +01:00
|
|
|
return rune(c), 1, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-12-02 20:34:41 +01:00
|
|
|
ch, size = utf8.DecodeRuneInString(r.s[r.i:])
|
2011-09-16 17:47:21 +02:00
|
|
|
r.i += size
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (r *Reader) UnreadRune() error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if r.prevRune < 0 {
|
2011-12-03 03:17:34 +01:00
|
|
|
return errors.New("strings.Reader: previous operation was not ReadRune")
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
r.i = r.prevRune
|
|
|
|
r.prevRune = -1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-03-02 17:38:43 +01:00
|
|
|
// Seek implements the io.Seeker interface.
|
|
|
|
func (r *Reader) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
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-11-21 08:03:38 +01: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-12-13 00:13:29 +01:00
|
|
|
return 0, nil
|
2012-11-21 08:03:38 +01: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
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// NewReader returns a new Reader reading from s.
|
|
|
|
// It is similar to bytes.NewBufferString but more efficient and read-only.
|
2011-09-16 17:47:21 +02:00
|
|
|
func NewReader(s string) *Reader { return &Reader{s, 0, -1} }
|