From eaa846b64b393d90f11925fc3b31eace215571a8 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 28 Aug 2023 05:01:23 +0000 Subject: [PATCH] runes.Reader: simplify buffer handling Signed-off-by: Alejandro Mery --- runes/reader.go | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/runes/reader.go b/runes/reader.go index cb23d9e..1e73048 100644 --- a/runes/reader.go +++ b/runes/reader.go @@ -87,12 +87,12 @@ func (b *Reader) needsBytes(n int) error { func (b *Reader) rebuffer(size int) { var src, dst []byte - if size <= cap(b.buf) { - // same buffer - dst = b.buf - } else { + if size > cap(b.buf) { // new buffer dst = make([]byte, size) + } else { + // same buffer + dst = b.buf } src = b.buf[b.off:] @@ -106,16 +106,7 @@ func (b *Reader) rebuffer(size int) { } func (b *Reader) prepareBuffer(n int) { - available := b.available() - - switch { - case n <= available: - // n or more already available - case n <= available+b.off: - // rebase is enough - b.rebuffer(0) - default: - // resize + if n > b.available() { needed := len(b.buf) + n - b.off size := cap(b.buf)