mirror of https://github.com/golang/go.git
bufio: avoid rescanning buffer multiple times in ReadSlice
When existing data in buffer does not have delimiter,
and new data is added with b.fill(), continue search from
previous point instead of starting from beginning.
Change-Id: Id78332afe2b0281b4a3c86bd1ffe9449cfea7848
GitHub-Last-Rev: 08e7d2f501
GitHub-Pull-Request: golang/go#25441
Reviewed-on: https://go-review.googlesource.com/113535
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
5ddecd1508
commit
a21ae28f39
|
|
@ -314,9 +314,11 @@ func (b *Reader) Buffered() int { return b.w - b.r }
|
|||
// ReadBytes or ReadString instead.
|
||||
// ReadSlice returns err != nil if and only if line does not end in delim.
|
||||
func (b *Reader) ReadSlice(delim byte) (line []byte, err error) {
|
||||
s := 0 // search start index
|
||||
for {
|
||||
// Search buffer.
|
||||
if i := bytes.IndexByte(b.buf[b.r:b.w], delim); i >= 0 {
|
||||
if i := bytes.IndexByte(b.buf[b.r+s:b.w], delim); i >= 0 {
|
||||
i += s
|
||||
line = b.buf[b.r : b.r+i+1]
|
||||
b.r += i + 1
|
||||
break
|
||||
|
|
@ -338,6 +340,8 @@ func (b *Reader) ReadSlice(delim byte) (line []byte, err error) {
|
|||
break
|
||||
}
|
||||
|
||||
s = b.w - b.r // do not rescan area we scanned before
|
||||
|
||||
b.fill() // buffer is not full
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue