1
0
mirror of https://github.com/golang/go synced 2024-11-25 15:08:02 -07:00

bufio.UnreadRune: fix bug at EOF

Fixes #1132.

R=rsc
CC=golang-dev
https://golang.org/cl/2215045
This commit is contained in:
Rob Pike 2010-09-24 12:28:14 +10:00
parent 099d7b4d1d
commit e439345dfd
2 changed files with 15 additions and 0 deletions

View File

@ -207,6 +207,7 @@ func (b *Reader) ReadRune() (rune int, size int, err os.Error) {
for b.r+utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) && b.err == nil { for b.r+utf8.UTFMax > b.w && !utf8.FullRune(b.buf[b.r:b.w]) && b.err == nil {
b.fill() b.fill()
} }
b.lastRuneSize = -1
if b.r == b.w { if b.r == b.w {
return 0, 0, b.err return 0, 0, b.err
} }

View File

@ -319,6 +319,20 @@ func TestUnreadRuneError(t *testing.T) {
} }
} }
func TestUnreadRuneAtEOF(t *testing.T) {
// UnreadRune/ReadRune should error at EOF (was a bug; used to panic)
r := NewReader(strings.NewReader("x"))
r.ReadRune()
r.ReadRune()
r.UnreadRune()
_, _, err := r.ReadRune()
if err == nil {
t.Error("expected error at EOF")
} else if err != os.EOF {
t.Error("expected EOF; got", err)
}
}
func TestReadWriteRune(t *testing.T) { func TestReadWriteRune(t *testing.T) {
const NRune = 1000 const NRune = 1000
byteBuf := new(bytes.Buffer) byteBuf := new(bytes.Buffer)