1
0
mirror of https://github.com/golang/go synced 2024-11-25 13:17:56 -07:00

strings: implement UnreadByte, UnreadRune

Added corresponding tests.

R=rsc
CC=golang-dev
https://golang.org/cl/4560045
This commit is contained in:
Robert Griesemer 2011-05-26 11:02:07 -07:00
parent abb970ef57
commit 9cd3372f9b
2 changed files with 111 additions and 20 deletions

View File

@ -9,50 +9,78 @@ import (
"utf8" "utf8"
) )
// A Reader satisfies calls to Read, ReadByte, and ReadRune by // A Reader implements the io.Reader, io.ByteScanner, and
// reading from a string. // io.RuneScanner interfaces by reading from a string.
type Reader string type Reader struct {
s string
i int // current reading index
prevRune int // index of previous rune; or < 0
}
func (r *Reader) Read(b []byte) (n int, err os.Error) { func (r *Reader) Read(b []byte) (n int, err os.Error) {
s := *r if r.i >= len(r.s) {
if len(s) == 0 {
return 0, os.EOF return 0, os.EOF
} }
n = copy(b, s) n = copy(b, r.s[r.i:])
*r = s[n:] r.i += n
r.prevRune = -1
return return
} }
func (r *Reader) ReadByte() (b byte, err os.Error) { func (r *Reader) ReadByte() (b byte, err os.Error) {
s := *r if r.i >= len(r.s) {
if len(s) == 0 {
return 0, os.EOF return 0, os.EOF
} }
b = s[0] b = r.s[r.i]
*r = s[1:] r.i++
r.prevRune = -1
return return
} }
// 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.ErrorString("strings.Reader: at beginning of string")
}
r.i--
r.prevRune = -1
return nil
}
// ReadRune reads and returns the next UTF-8-encoded // ReadRune reads and returns the next UTF-8-encoded
// Unicode code point from the buffer. // Unicode code point from the buffer.
// If no bytes are available, the error returned is os.EOF. // If no bytes are available, the error returned is os.EOF.
// If the bytes are an erroneous UTF-8 encoding, it // If the bytes are an erroneous UTF-8 encoding, it
// consumes one byte and returns U+FFFD, 1. // consumes one byte and returns U+FFFD, 1.
func (r *Reader) ReadRune() (rune int, size int, err os.Error) { func (r *Reader) ReadRune() (rune int, size int, err os.Error) {
s := *r if r.i >= len(r.s) {
if len(s) == 0 {
return 0, 0, os.EOF return 0, 0, os.EOF
} }
c := s[0] r.prevRune = r.i
if c < utf8.RuneSelf { if c := r.s[r.i]; c < utf8.RuneSelf {
*r = s[1:] r.i++
return int(c), 1, nil return int(c), 1, nil
} }
rune, size = utf8.DecodeRuneInString(string(s)) rune, size = utf8.DecodeRuneInString(r.s[r.i:])
*r = s[size:] r.i += size
return return
} }
// 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.ErrorString("strings.Reader: previous operation was not ReadRune")
}
r.i = r.prevRune
r.prevRune = -1
return nil
}
// NewReader returns a new Reader reading from s. // NewReader returns a new Reader reading from s.
// It is similar to bytes.NewBufferString but more efficient and read-only. // It is similar to bytes.NewBufferString but more efficient and read-only.
func NewReader(s string) *Reader { return (*Reader)(&s) } func NewReader(s string) *Reader { return &Reader{s, 0, -1} }

View File

@ -5,6 +5,7 @@
package strings_test package strings_test
import ( import (
"bytes"
"os" "os"
"reflect" "reflect"
"strconv" "strconv"
@ -751,13 +752,56 @@ func TestRunes(t *testing.T) {
} }
} }
func TestReadByte(t *testing.T) {
testStrings := []string{"", abcd, faces, commas}
for _, s := range testStrings {
reader := NewReader(s)
if e := reader.UnreadByte(); e == nil {
t.Errorf("Unreading %q at beginning: expected error", s)
}
var res bytes.Buffer
for {
b, e := reader.ReadByte()
if e == os.EOF {
break
}
if e != nil {
t.Errorf("Reading %q: %s", s, e)
break
}
res.WriteByte(b)
// unread and read again
e = reader.UnreadByte()
if e != nil {
t.Errorf("Unreading %q: %s", s, e)
break
}
b1, e := reader.ReadByte()
if e != nil {
t.Errorf("Reading %q after unreading: %s", s, e)
break
}
if b1 != b {
t.Errorf("Reading %q after unreading: want byte %q, got %q", s, b, b1)
break
}
}
if res.String() != s {
t.Errorf("Reader(%q).ReadByte() produced %q", s, res.String())
}
}
}
func TestReadRune(t *testing.T) { func TestReadRune(t *testing.T) {
testStrings := []string{"", abcd, faces, commas} testStrings := []string{"", abcd, faces, commas}
for _, s := range testStrings { for _, s := range testStrings {
reader := NewReader(s) reader := NewReader(s)
if e := reader.UnreadRune(); e == nil {
t.Errorf("Unreading %q at beginning: expected error", s)
}
res := "" res := ""
for { for {
r, _, e := reader.ReadRune() r, z, e := reader.ReadRune()
if e == os.EOF { if e == os.EOF {
break break
} }
@ -766,6 +810,25 @@ func TestReadRune(t *testing.T) {
break break
} }
res += string(r) res += string(r)
// unread and read again
e = reader.UnreadRune()
if e != nil {
t.Errorf("Unreading %q: %s", s, e)
break
}
r1, z1, e := reader.ReadRune()
if e != nil {
t.Errorf("Reading %q after unreading: %s", s, e)
break
}
if r1 != r {
t.Errorf("Reading %q after unreading: want rune %q, got %q", s, r, r1)
break
}
if z1 != z {
t.Errorf("Reading %q after unreading: want size %d, got %d", s, z, z1)
break
}
} }
if res != s { if res != s {
t.Errorf("Reader(%q).ReadRune() produced %q", s, res) t.Errorf("Reader(%q).ReadRune() produced %q", s, res)