mirror of
https://github.com/golang/go
synced 2024-11-27 00:51:26 -07:00
bytes, strings: fix Reader.UnreadRune returning without error on a zero Reader
Fixes #28269 Change-Id: I878dff43c0b6bdb98702d8e73f2ecd984fb2350f Reviewed-on: https://go-review.googlesource.com/c/145098 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
70dd90c4a9
commit
5fc4604aa8
@ -14,6 +14,7 @@ import (
|
||||
// io.ByteScanner, and io.RuneScanner interfaces by reading from
|
||||
// a byte slice.
|
||||
// Unlike a Buffer, a Reader is read-only and supports seeking.
|
||||
// The zero value for Reader operates like a Reader of an empty slice.
|
||||
type Reader struct {
|
||||
s []byte
|
||||
i int64 // current reading index
|
||||
@ -75,10 +76,10 @@ func (r *Reader) ReadByte() (byte, error) {
|
||||
|
||||
// UnreadByte complements ReadByte in implementing the io.ByteScanner interface.
|
||||
func (r *Reader) UnreadByte() error {
|
||||
r.prevRune = -1
|
||||
if r.i <= 0 {
|
||||
return errors.New("bytes.Reader.UnreadByte: at beginning of slice")
|
||||
}
|
||||
r.prevRune = -1
|
||||
r.i--
|
||||
return nil
|
||||
}
|
||||
@ -101,6 +102,9 @@ func (r *Reader) ReadRune() (ch rune, size int, err error) {
|
||||
|
||||
// UnreadRune complements ReadRune in implementing the io.RuneScanner interface.
|
||||
func (r *Reader) UnreadRune() error {
|
||||
if r.i <= 0 {
|
||||
return errors.New("bytes.Reader.UnreadRune: at beginning of slice")
|
||||
}
|
||||
if r.prevRune < 0 {
|
||||
return errors.New("bytes.Reader.UnreadRune: previous operation was not ReadRune")
|
||||
}
|
||||
|
@ -276,3 +276,45 @@ func TestReaderReset(t *testing.T) {
|
||||
t.Errorf("ReadAll: got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReaderZero(t *testing.T) {
|
||||
if l := (&Reader{}).Len(); l != 0 {
|
||||
t.Errorf("Len: got %d, want 0", l)
|
||||
}
|
||||
|
||||
if n, err := (&Reader{}).Read(nil); n != 0 || err != io.EOF {
|
||||
t.Errorf("Read: got %d, %v; want 0, io.EOF", n, err)
|
||||
}
|
||||
|
||||
if n, err := (&Reader{}).ReadAt(nil, 11); n != 0 || err != io.EOF {
|
||||
t.Errorf("ReadAt: got %d, %v; want 0, io.EOF", n, err)
|
||||
}
|
||||
|
||||
if b, err := (&Reader{}).ReadByte(); b != 0 || err != io.EOF {
|
||||
t.Errorf("ReadByte: got %d, %v; want 0, io.EOF", b, err)
|
||||
}
|
||||
|
||||
if ch, size, err := (&Reader{}).ReadRune(); ch != 0 || size != 0 || err != io.EOF {
|
||||
t.Errorf("ReadRune: got %d, %d, %v; want 0, 0, io.EOF", ch, size, err)
|
||||
}
|
||||
|
||||
if offset, err := (&Reader{}).Seek(11, io.SeekStart); offset != 11 || err != nil {
|
||||
t.Errorf("Seek: got %d, %v; want 11, nil", offset, err)
|
||||
}
|
||||
|
||||
if s := (&Reader{}).Size(); s != 0 {
|
||||
t.Errorf("Size: got %d, want 0", s)
|
||||
}
|
||||
|
||||
if (&Reader{}).UnreadByte() == nil {
|
||||
t.Errorf("UnreadByte: got nil, want error")
|
||||
}
|
||||
|
||||
if (&Reader{}).UnreadRune() == nil {
|
||||
t.Errorf("UnreadRune: got nil, want error")
|
||||
}
|
||||
|
||||
if n, err := (&Reader{}).WriteTo(ioutil.Discard); n != 0 || err != nil {
|
||||
t.Errorf("WriteTo: got %d, %v; want 0, nil", n, err)
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import (
|
||||
// A Reader implements the io.Reader, io.ReaderAt, io.Seeker, io.WriterTo,
|
||||
// io.ByteScanner, and io.RuneScanner interfaces by reading
|
||||
// from a string.
|
||||
// The zero value for Reader operates like a Reader of an empty string.
|
||||
type Reader struct {
|
||||
s string
|
||||
i int64 // current reading index
|
||||
@ -70,10 +71,10 @@ func (r *Reader) ReadByte() (byte, error) {
|
||||
}
|
||||
|
||||
func (r *Reader) UnreadByte() error {
|
||||
r.prevRune = -1
|
||||
if r.i <= 0 {
|
||||
return errors.New("strings.Reader.UnreadByte: at beginning of string")
|
||||
}
|
||||
r.prevRune = -1
|
||||
r.i--
|
||||
return nil
|
||||
}
|
||||
@ -94,6 +95,9 @@ func (r *Reader) ReadRune() (ch rune, size int, err error) {
|
||||
}
|
||||
|
||||
func (r *Reader) UnreadRune() error {
|
||||
if r.i <= 0 {
|
||||
return errors.New("strings.Reader.UnreadRune: at beginning of string")
|
||||
}
|
||||
if r.prevRune < 0 {
|
||||
return errors.New("strings.Reader.UnreadRune: previous operation was not ReadRune")
|
||||
}
|
||||
|
@ -190,3 +190,45 @@ func TestReaderReset(t *testing.T) {
|
||||
t.Errorf("ReadAll: got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReaderZero(t *testing.T) {
|
||||
if l := (&strings.Reader{}).Len(); l != 0 {
|
||||
t.Errorf("Len: got %d, want 0", l)
|
||||
}
|
||||
|
||||
if n, err := (&strings.Reader{}).Read(nil); n != 0 || err != io.EOF {
|
||||
t.Errorf("Read: got %d, %v; want 0, io.EOF", n, err)
|
||||
}
|
||||
|
||||
if n, err := (&strings.Reader{}).ReadAt(nil, 11); n != 0 || err != io.EOF {
|
||||
t.Errorf("ReadAt: got %d, %v; want 0, io.EOF", n, err)
|
||||
}
|
||||
|
||||
if b, err := (&strings.Reader{}).ReadByte(); b != 0 || err != io.EOF {
|
||||
t.Errorf("ReadByte: got %d, %v; want 0, io.EOF", b, err)
|
||||
}
|
||||
|
||||
if ch, size, err := (&strings.Reader{}).ReadRune(); ch != 0 || size != 0 || err != io.EOF {
|
||||
t.Errorf("ReadRune: got %d, %d, %v; want 0, 0, io.EOF", ch, size, err)
|
||||
}
|
||||
|
||||
if offset, err := (&strings.Reader{}).Seek(11, io.SeekStart); offset != 11 || err != nil {
|
||||
t.Errorf("Seek: got %d, %v; want 11, nil", offset, err)
|
||||
}
|
||||
|
||||
if s := (&strings.Reader{}).Size(); s != 0 {
|
||||
t.Errorf("Size: got %d, want 0", s)
|
||||
}
|
||||
|
||||
if (&strings.Reader{}).UnreadByte() == nil {
|
||||
t.Errorf("UnreadByte: got nil, want error")
|
||||
}
|
||||
|
||||
if (&strings.Reader{}).UnreadRune() == nil {
|
||||
t.Errorf("UnreadRune: got nil, want error")
|
||||
}
|
||||
|
||||
if n, err := (&strings.Reader{}).WriteTo(ioutil.Discard); n != 0 || err != nil {
|
||||
t.Errorf("WriteTo: got %d, %v; want 0, nil", n, err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user