-
Notifications
You must be signed in to change notification settings - Fork 1
/
out_window.go
59 lines (49 loc) · 994 Bytes
/
out_window.go
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
package lzma
import "bufio"
type outWindow struct {
bufio.Writer
buf []byte
pos int
totalPos uint32
isFull bool
}
func newOutWindow(dictSite uint32) (*outWindow){
window := new(outWindow)
window.buf = make([]byte, dictSite)
window.pos = 0
window.totalPos = 0
window.isFull = false
return window
}
func (w *outWindow) putByte(aByte byte) error {
w.totalPos++
w.buf[w.pos] = aByte
w.pos++
if w.pos == len(w.buf) {
w.isFull = true
w.pos = 0
}
return w.Writer.WriteByte(aByte)
}
func (w *outWindow) getByte(dist int) (byte) {
calcPos := len(w.buf) - dist + w.pos
if dist <= w.pos {
calcPos = w.pos - dist
}
return w.buf[calcPos]
}
func (w *outWindow) copyMatch(dist int, len uint) error {
for ; len > 0 ; len-- {
err := w.putByte(w.getByte(dist))
if err != nil {
return err
}
}
return nil
}
func (w *outWindow) checkDistance(dist int) bool {
return dist <= w.pos || w.isFull
}
func (w *outWindow) isEmpty() bool {
return w.pos == 0 && !w.isFull
}