Commit 9473c044 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

bufio: document relationship between UnreadByte/UnreadRune and Peek

Fixes #29387

Change-Id: I2d9981f63ac16630ed39d6da6692c81396f4e9ea
Reviewed-on: https://go-review.googlesource.com/c/155930Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 1d2e548b
...@@ -33,8 +33,8 @@ type Reader struct { ...@@ -33,8 +33,8 @@ type Reader struct {
rd io.Reader // reader provided by the client rd io.Reader // reader provided by the client
r, w int // buf read and write positions r, w int // buf read and write positions
err error err error
lastByte int lastByte int // last byte read for UnreadByte; -1 means invalid
lastRuneSize int lastRuneSize int // size of last rune read for UnreadRune; -1 means invalid
} }
const minReadBufferSize = 16 const minReadBufferSize = 16
...@@ -123,6 +123,9 @@ func (b *Reader) readErr() error { ...@@ -123,6 +123,9 @@ func (b *Reader) readErr() error {
// being valid at the next read call. If Peek returns fewer than n bytes, it // being valid at the next read call. If Peek returns fewer than n bytes, it
// also returns an error explaining why the read is short. The error is // also returns an error explaining why the read is short. The error is
// ErrBufferFull if n is larger than b's buffer size. // ErrBufferFull if n is larger than b's buffer size.
//
// Calling Peek prevents a UnreadByte or UnreadRune call from succeeding
// until the next read operation.
func (b *Reader) Peek(n int) ([]byte, error) { func (b *Reader) Peek(n int) ([]byte, error) {
if n < 0 { if n < 0 {
return nil, ErrNegativeCount return nil, ErrNegativeCount
...@@ -252,6 +255,10 @@ func (b *Reader) ReadByte() (byte, error) { ...@@ -252,6 +255,10 @@ func (b *Reader) ReadByte() (byte, error) {
} }
// UnreadByte unreads the last byte. Only the most recently read byte can be unread. // UnreadByte unreads the last byte. Only the most recently read byte can be unread.
//
// UnreadByte returns an error if the most recent method called on the
// Reader was not a read operation. Notably, Peek is not considered a
// read operation.
func (b *Reader) UnreadByte() error { func (b *Reader) UnreadByte() error {
if b.lastByte < 0 || b.r == 0 && b.w > 0 { if b.lastByte < 0 || b.r == 0 && b.w > 0 {
return ErrInvalidUnreadByte return ErrInvalidUnreadByte
...@@ -290,8 +297,8 @@ func (b *Reader) ReadRune() (r rune, size int, err error) { ...@@ -290,8 +297,8 @@ func (b *Reader) ReadRune() (r rune, size int, err error) {
return r, size, nil return r, size, nil
} }
// UnreadRune unreads the last rune. If the most recent read operation on // UnreadRune unreads the last rune. If the most recent method called on
// the buffer was not a ReadRune, UnreadRune returns an error. (In this // the Reader was not a ReadRune, UnreadRune returns an error. (In this
// regard it is stricter than UnreadByte, which will unread the last byte // regard it is stricter than UnreadByte, which will unread the last byte
// from any read operation.) // from any read operation.)
func (b *Reader) UnreadRune() error { func (b *Reader) UnreadRune() error {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment