Browse Source

runes.Reader: simplify buffer handling

Signed-off-by: Alejandro Mery <amery@jpi.io>
pull/1/head
Alejandro Mery 1 year ago
parent
commit
eaa846b64b
  1. 19
      runes/reader.go

19
runes/reader.go

@ -87,12 +87,12 @@ func (b *Reader) needsBytes(n int) error {
func (b *Reader) rebuffer(size int) { func (b *Reader) rebuffer(size int) {
var src, dst []byte var src, dst []byte
if size <= cap(b.buf) { if size > cap(b.buf) {
// same buffer
dst = b.buf
} else {
// new buffer // new buffer
dst = make([]byte, size) dst = make([]byte, size)
} else {
// same buffer
dst = b.buf
} }
src = b.buf[b.off:] src = b.buf[b.off:]
@ -106,16 +106,7 @@ func (b *Reader) rebuffer(size int) {
} }
func (b *Reader) prepareBuffer(n int) { func (b *Reader) prepareBuffer(n int) {
available := b.available() if n > b.available() {
switch {
case n <= available:
// n or more already available
case n <= available+b.off:
// rebase is enough
b.rebuffer(0)
default:
// resize
needed := len(b.buf) + n - b.off needed := len(b.buf) + n - b.off
size := cap(b.buf) size := cap(b.buf)

Loading…
Cancel
Save