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) {
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)

Loading…
Cancel
Save