1
2
3
4
5 package zstd
6
7
8
9
10
11
12
13
14 type window struct {
15 size int
16 data []byte
17 off int
18 }
19
20
21 func (w *window) reset(size int) {
22 b := w.data[:0]
23 if cap(b) < size {
24 b = make([]byte, 0, size)
25 }
26 w.data = b
27 w.off = 0
28 w.size = size
29 }
30
31
32 func (w *window) len() uint32 {
33 return uint32(len(w.data))
34 }
35
36
37 func (w *window) save(buf []byte) {
38 if w.size == 0 {
39 return
40 }
41 if len(buf) == 0 {
42 return
43 }
44
45 if len(buf) >= w.size {
46 from := len(buf) - w.size
47 w.data = append(w.data[:0], buf[from:]...)
48 w.off = 0
49 return
50 }
51
52
53 free := w.size - len(w.data)
54 if free == 0 {
55 n := copy(w.data[w.off:], buf)
56 if n == len(buf) {
57 w.off += n
58 } else {
59 w.off = copy(w.data, buf[n:])
60 }
61 } else {
62 if free >= len(buf) {
63 w.data = append(w.data, buf...)
64 } else {
65 w.data = append(w.data, buf[:free]...)
66 w.off = copy(w.data, buf[free:])
67 }
68 }
69 }
70
71
72
73 func (w *window) appendTo(buf []byte, from, to uint32) []byte {
74 dataLen := uint32(len(w.data))
75 from += uint32(w.off)
76 to += uint32(w.off)
77
78 wrap := false
79 if from > dataLen {
80 from -= dataLen
81 wrap = !wrap
82 }
83 if to > dataLen {
84 to -= dataLen
85 wrap = !wrap
86 }
87
88 if wrap {
89 buf = append(buf, w.data[from:]...)
90 return append(buf, w.data[:to]...)
91 } else {
92 return append(buf, w.data[from:to]...)
93 }
94 }
95
View as plain text