1
0
mirror of https://github.com/golang/go synced 2024-11-12 09:20:22 -07:00

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.
This commit is contained in:
andrius4669 2018-05-17 15:32:25 +03:00
parent 750a42f42c
commit 08e7d2f501

View File

@ -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
}