File: buffer.go

package info (click to toggle)
fq 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 106,624 kB
  • sloc: xml: 2,835; makefile: 250; sh: 241; exp: 57; ansic: 21
file content (72 lines) | stat: -rw-r--r-- 1,348 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package bitio

// TODO: NewBuffer with []byte arg to save on alloc

import (
	"io"
)

// Buffer is a bitio.Reader and bitio.Writer providing a bit buffer.
// Similar to bytes.Buffer.
type Buffer struct {
	buf     []byte
	bufBits int64
	bitsOff int64
}

func (b *Buffer) Len() int64 { return b.bufBits - b.bitsOff }

func (b *Buffer) Reset() {
	b.bufBits = 0
	b.bitsOff = 0
}

// Bits return unread bits in buffer
func (b *Buffer) Bits() ([]byte, int64) {
	l := b.Len()
	buf := make([]byte, BitsByteCount(l))
	copyBufBits(buf, 0, b.buf, b.bitsOff, l, true)
	return buf, b.bufBits
}

func (b *Buffer) WriteBits(p []byte, nBits int64) (n int64, err error) {
	tBytes := BitsByteCount(b.bufBits + nBits)

	if tBytes > int64(len(b.buf)) {
		if tBytes <= int64(cap(b.buf)) {
			b.buf = b.buf[:tBytes]
		} else {
			buf := make([]byte, tBytes, tBytes*2)
			copy(buf, b.buf)
			b.buf = buf
		}
	}

	copyBufBits(b.buf, b.bufBits, p, 0, nBits, true)
	b.bufBits += nBits

	return nBits, nil
}

func (b *Buffer) empty() bool { return b.bufBits <= b.bitsOff }

func (b *Buffer) ReadBits(p []byte, nBits int64) (n int64, err error) {
	if b.empty() {
		b.Reset()
		if nBits == 0 {
			return 0, nil
		}
		return 0, io.EOF
	}

	c := nBits
	left := b.Len()
	if c > left {
		c = left
	}

	copyBufBits(p, 0, b.buf, b.bitsOff, c, true)
	b.bitsOff += c

	return c, nil
}