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 (
|
|
|
|
"os"
|
|
|
|
"utf8"
|
|
|
|
)
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// A Reader implements the io.Reader, io.ByteScanner, and
|
|
|
|
// io.RuneScanner interfaces by reading from a string.
|
|
|
|
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 {
|
|
|
|
return len(r.s) - r.i
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
func (r *Reader) Read(b []byte) (n int, err os.Error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if r.i >= len(r.s) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return 0, os.EOF
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) ReadByte() (b byte, err os.Error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if r.i >= len(r.s) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return 0, os.EOF
|
|
|
|
}
|
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-09-16 17:47:21 +02:00
|
|
|
// UnreadByte moves the reading position back by one byte.
|
|
|
|
// It is an error to call UnreadByte if nothing has been
|
|
|
|
// read yet.
|
|
|
|
func (r *Reader) UnreadByte() os.Error {
|
|
|
|
if r.i <= 0 {
|
|
|
|
return os.NewError("strings.Reader: at beginning of string")
|
|
|
|
}
|
|
|
|
r.i--
|
|
|
|
r.prevRune = -1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// ReadRune reads and returns the next UTF-8-encoded
|
|
|
|
// Unicode code point from the buffer.
|
|
|
|
// If no bytes are available, the error returned is os.EOF.
|
|
|
|
// If the bytes are an erroneous UTF-8 encoding, it
|
|
|
|
// consumes one byte and returns U+FFFD, 1.
|
|
|
|
func (r *Reader) ReadRune() (rune int, size int, err os.Error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if r.i >= len(r.s) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return 0, 0, os.EOF
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
r.prevRune = r.i
|
|
|
|
if c := r.s[r.i]; c < utf8.RuneSelf {
|
|
|
|
r.i++
|
2010-12-03 05:34:57 +01:00
|
|
|
return int(c), 1, nil
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
rune, size = utf8.DecodeRuneInString(r.s[r.i:])
|
|
|
|
r.i += size
|
2010-12-03 05:34:57 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// UnreadRune causes the next call to ReadRune to return the same rune
|
|
|
|
// as the previous call to ReadRune.
|
|
|
|
// The last method called on r must have been ReadRune.
|
|
|
|
func (r *Reader) UnreadRune() os.Error {
|
|
|
|
if r.prevRune < 0 {
|
|
|
|
return os.NewError("strings.Reader: previous operation was not ReadRune")
|
|
|
|
}
|
|
|
|
r.i = r.prevRune
|
|
|
|
r.prevRune = -1
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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} }
|