typenative 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -0
- package/README.md +17 -1
- package/bin/cli.js +25 -11
- package/bin/go/convert.js +11 -1
- package/bin/index.js +221 -47
- package/bin/main.js +35 -14
- package/bin/node/assert.js +2 -2
- package/bin/node/buffer.js +9 -0
- package/bin/node/dns.js +1 -1
- package/bin/node/events.js +1 -1
- package/bin/node/fs.js +1 -1
- package/bin/node/fs_watch.js +407 -0
- package/bin/node/http2.js +33 -6
- package/bin/node/readline.js +1 -1
- package/bin/node/registry.js +2 -1
- package/bin/node/stream.js +6 -3
- package/bin/node/timers.js +1 -1
- package/bin/node/typed_arrays.js +608 -0
- package/bin/node/util.js +3 -0
- package/bin/node/zlib.js +131 -16
- package/bin/transpiler.js +635 -44
- package/bin/version.js +1 -1
- package/conformance.md +16 -4
- package/examples/README.md +54 -0
- package/examples/fibonacci/README.md +89 -0
- package/examples/fibonacci/bench.mjs +87 -0
- package/examples/fibonacci/main.ts +21 -0
- package/examples/hello-world/README.md +62 -0
- package/examples/hello-world/bench.mjs +66 -0
- package/examples/hello-world/main.ts +7 -0
- package/examples/json-server/README.md +125 -0
- package/examples/json-server/bench.mjs +171 -0
- package/examples/json-server/main.ts +67 -0
- package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
- package/govendor/github.com/andybalholm/brotli/README.md +19 -0
- package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
- package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
- package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
- package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
- package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
- package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
- package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
- package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
- package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
- package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
- package/govendor/github.com/andybalholm/brotli/command.go +254 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
- package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
- package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
- package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
- package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
- package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
- package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
- package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
- package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
- package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
- package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
- package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
- package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
- package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
- package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
- package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
- package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
- package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
- package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
- package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
- package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
- package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
- package/govendor/github.com/andybalholm/brotli/http.go +201 -0
- package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
- package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
- package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
- package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
- package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
- package/govendor/github.com/andybalholm/brotli/params.go +37 -0
- package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
- package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
- package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
- package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
- package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
- package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
- package/govendor/github.com/andybalholm/brotli/state.go +313 -0
- package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
- package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
- package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
- package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
- package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
- package/govendor/github.com/andybalholm/brotli/util.go +7 -0
- package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
- package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
- package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
- package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
- package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
- package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
- package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
- package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
- package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
- package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
- package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
- package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
- package/govendor/manifest.txt +3 -1
- package/package.json +1 -1
- package/types/typenative-npm.d.ts +72 -0
- package/types/typenative.d.ts +117 -1
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
package matchfinder
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/binary"
|
|
5
|
+
"math"
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
const (
|
|
9
|
+
zdfastLongTableBits = 17
|
|
10
|
+
zdfastLongTableSize = 1 << zdfastLongTableBits
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
// ZDFast is a MatchFinder based on the "Default" setting in
|
|
14
|
+
// github.com/klauspost/compress/zstd.
|
|
15
|
+
type ZDFast struct {
|
|
16
|
+
MaxDistance int
|
|
17
|
+
|
|
18
|
+
history []byte
|
|
19
|
+
// current is the offset at the start of history
|
|
20
|
+
current int32
|
|
21
|
+
table [zfastTableSize]tableEntry
|
|
22
|
+
longTable [zdfastLongTableSize]tableEntry
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
func (z *ZDFast) Reset() {
|
|
26
|
+
z.current = 0
|
|
27
|
+
z.table = [zfastTableSize]tableEntry{}
|
|
28
|
+
z.longTable = [zdfastLongTableSize]tableEntry{}
|
|
29
|
+
z.history = z.history[:0]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
func (z *ZDFast) FindMatches(dst []Match, src []byte) []Match {
|
|
33
|
+
if z.MaxDistance == 0 {
|
|
34
|
+
z.MaxDistance = 1 << 16
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Protect against overflow of current.
|
|
38
|
+
if int(z.current) >= int(math.MaxInt32)-2*z.MaxDistance-len(z.history) {
|
|
39
|
+
minOffset := z.current + int32(len(z.history)) - int32(z.MaxDistance)
|
|
40
|
+
for i := range z.table {
|
|
41
|
+
v := z.table[i].offset
|
|
42
|
+
if v < minOffset {
|
|
43
|
+
v = 0
|
|
44
|
+
} else {
|
|
45
|
+
v = v - z.current + int32(z.MaxDistance)
|
|
46
|
+
}
|
|
47
|
+
z.table[i].offset = v
|
|
48
|
+
}
|
|
49
|
+
for i := range z.longTable {
|
|
50
|
+
v := z.longTable[i].offset
|
|
51
|
+
if v < minOffset {
|
|
52
|
+
v = 0
|
|
53
|
+
} else {
|
|
54
|
+
v = v - z.current + int32(z.MaxDistance)
|
|
55
|
+
}
|
|
56
|
+
z.longTable[i].offset = v
|
|
57
|
+
}
|
|
58
|
+
z.current = int32(z.MaxDistance)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if len(z.history)+len(src) > cap(z.history) {
|
|
62
|
+
// history doesn't have enough capacity to hold the new block.
|
|
63
|
+
if cap(z.history) == 0 {
|
|
64
|
+
historySize := max(2*z.MaxDistance, 1<<20, len(src))
|
|
65
|
+
z.history = make([]byte, 0, historySize)
|
|
66
|
+
} else if len(z.history) > z.MaxDistance {
|
|
67
|
+
// Move down
|
|
68
|
+
offset := len(z.history) - z.MaxDistance
|
|
69
|
+
copy(z.history[:z.MaxDistance], z.history[offset:])
|
|
70
|
+
z.current += int32(offset)
|
|
71
|
+
z.history = z.history[:z.MaxDistance]
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
s := int32(len(z.history))
|
|
75
|
+
z.history = append(z.history, src...)
|
|
76
|
+
|
|
77
|
+
if len(src) < 16 {
|
|
78
|
+
return append(dst, Match{
|
|
79
|
+
Unmatched: len(src),
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
src = z.history
|
|
84
|
+
sLimit := int32(len(src)) - 10
|
|
85
|
+
|
|
86
|
+
const stepSize = 1
|
|
87
|
+
|
|
88
|
+
nextEmit := s
|
|
89
|
+
cv := binary.LittleEndian.Uint64(src[s:])
|
|
90
|
+
var offset1, offset2 int32
|
|
91
|
+
|
|
92
|
+
mainLoop:
|
|
93
|
+
for {
|
|
94
|
+
// t will contain the match offset when we find one.
|
|
95
|
+
// When exiting the search loop, we have already checked 4 bytes.
|
|
96
|
+
var t int32
|
|
97
|
+
|
|
98
|
+
for {
|
|
99
|
+
nextHashL := z.hashLong(cv)
|
|
100
|
+
nextHashS := z.hashShort(cv)
|
|
101
|
+
candidateL := z.longTable[nextHashL]
|
|
102
|
+
candidateS := z.table[nextHashS]
|
|
103
|
+
|
|
104
|
+
repIndex := s - offset1 + 1
|
|
105
|
+
|
|
106
|
+
entry := tableEntry{offset: s + z.current, val: uint32(cv)}
|
|
107
|
+
z.longTable[nextHashL] = entry
|
|
108
|
+
z.table[nextHashS] = entry
|
|
109
|
+
|
|
110
|
+
if offset1 != 0 && repIndex >= 0 && binary.LittleEndian.Uint32(src[repIndex:]) == uint32(cv>>8) {
|
|
111
|
+
// There is a repeated match at s+1.
|
|
112
|
+
end := extendMatch(src, int(repIndex+4), int(s+5))
|
|
113
|
+
start := s + 1
|
|
114
|
+
for repIndex > 0 && start > nextEmit && src[repIndex-1] == src[start-1] {
|
|
115
|
+
repIndex--
|
|
116
|
+
start--
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
dst = append(dst, Match{
|
|
120
|
+
Unmatched: int(start - nextEmit),
|
|
121
|
+
Length: end - int(start),
|
|
122
|
+
Distance: int(start - repIndex),
|
|
123
|
+
})
|
|
124
|
+
s = int32(end)
|
|
125
|
+
nextEmit = s
|
|
126
|
+
if s >= sLimit {
|
|
127
|
+
break mainLoop
|
|
128
|
+
}
|
|
129
|
+
cv = binary.LittleEndian.Uint64(src[s:])
|
|
130
|
+
continue
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
coffsetL := s - (candidateL.offset - z.current)
|
|
134
|
+
coffsetS := s - (candidateS.offset - z.current)
|
|
135
|
+
if coffsetL < int32(z.MaxDistance) && uint32(cv) == candidateL.val && candidateL.offset != 0 {
|
|
136
|
+
t = candidateL.offset - z.current
|
|
137
|
+
if binary.LittleEndian.Uint32(src[t:]) == uint32(cv) {
|
|
138
|
+
// found a long match (likely at least 8 bytes)
|
|
139
|
+
break
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if coffsetS < int32(z.MaxDistance) && uint32(cv) == candidateS.val && candidateS.offset != 0 {
|
|
143
|
+
t = candidateS.offset - z.current
|
|
144
|
+
if binary.LittleEndian.Uint32(src[t:]) != uint32(cv) {
|
|
145
|
+
goto noMatch
|
|
146
|
+
}
|
|
147
|
+
// Found a regular match.
|
|
148
|
+
// See if we can find a long match at s+1
|
|
149
|
+
cv := binary.LittleEndian.Uint64(src[s+1:])
|
|
150
|
+
nextHashL = z.hashLong(cv)
|
|
151
|
+
candidateL = z.longTable[nextHashL]
|
|
152
|
+
coffsetL = s - (candidateL.offset - z.current) + 1
|
|
153
|
+
z.longTable[nextHashL] = tableEntry{offset: s + 1 + z.current, val: uint32(cv)}
|
|
154
|
+
if coffsetL < int32(z.MaxDistance) && uint32(cv) == candidateL.val {
|
|
155
|
+
t = candidateL.offset - z.current
|
|
156
|
+
if binary.LittleEndian.Uint32(src[t:]) == uint32(cv) {
|
|
157
|
+
// We found a long match at s+1, so we'll use that instead
|
|
158
|
+
// of the regular match at s.
|
|
159
|
+
s++
|
|
160
|
+
break
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
t = candidateS.offset - z.current
|
|
165
|
+
break
|
|
166
|
+
}
|
|
167
|
+
noMatch:
|
|
168
|
+
|
|
169
|
+
s += stepSize + ((s - nextEmit) >> 7)
|
|
170
|
+
if s > sLimit {
|
|
171
|
+
break mainLoop
|
|
172
|
+
}
|
|
173
|
+
cv = binary.LittleEndian.Uint64(src[s:])
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// A 4-byte match has been found. We'll later see if more than
|
|
177
|
+
// 4 bytes.
|
|
178
|
+
offset2 = offset1
|
|
179
|
+
offset1 = s - t
|
|
180
|
+
|
|
181
|
+
end := extendMatch(src, int(t+4), int(s+4))
|
|
182
|
+
for t > 0 && s > nextEmit && src[t-1] == src[s-1] {
|
|
183
|
+
s--
|
|
184
|
+
t--
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
dst = append(dst, Match{
|
|
188
|
+
Unmatched: int(s - nextEmit),
|
|
189
|
+
Length: end - int(s),
|
|
190
|
+
Distance: int(s - t),
|
|
191
|
+
})
|
|
192
|
+
prevS := s
|
|
193
|
+
s = int32(end)
|
|
194
|
+
nextEmit = s
|
|
195
|
+
if s >= sLimit {
|
|
196
|
+
break mainLoop
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Store some table entries near the start and end of the match.
|
|
200
|
+
index0 := prevS + 1
|
|
201
|
+
index1 := s - 2
|
|
202
|
+
cv0 := binary.LittleEndian.Uint64(src[index0:])
|
|
203
|
+
cv1 := binary.LittleEndian.Uint64(src[index1:])
|
|
204
|
+
te0 := tableEntry{offset: index0 + z.current, val: uint32(cv0)}
|
|
205
|
+
te1 := tableEntry{offset: index1 + z.current, val: uint32(cv1)}
|
|
206
|
+
z.longTable[z.hashLong(cv0)] = te0
|
|
207
|
+
z.longTable[z.hashLong(cv1)] = te1
|
|
208
|
+
cv0 >>= 8
|
|
209
|
+
cv1 >>= 8
|
|
210
|
+
te0.offset++
|
|
211
|
+
te1.offset++
|
|
212
|
+
te0.val = uint32(cv0)
|
|
213
|
+
te1.val = uint32(cv1)
|
|
214
|
+
z.table[z.hashShort(cv0)] = te0
|
|
215
|
+
z.table[z.hashShort(cv1)] = te1
|
|
216
|
+
|
|
217
|
+
cv = binary.LittleEndian.Uint64(src[s:])
|
|
218
|
+
|
|
219
|
+
// Check offset 2
|
|
220
|
+
if o2 := s - offset2; offset2 != 0 && binary.LittleEndian.Uint32(src[o2:]) == uint32(cv) {
|
|
221
|
+
end := extendMatch(src, int(o2+4), int(s+4))
|
|
222
|
+
|
|
223
|
+
// Store the hashes, since we have them.
|
|
224
|
+
nextHashS := z.hashShort(cv)
|
|
225
|
+
nextHashL := z.hashLong(cv)
|
|
226
|
+
entry := tableEntry{offset: s + z.current, val: uint32(cv)}
|
|
227
|
+
z.table[nextHashS] = entry
|
|
228
|
+
z.longTable[nextHashL] = entry
|
|
229
|
+
dst = append(dst, Match{
|
|
230
|
+
Length: end - int(s),
|
|
231
|
+
Distance: int(offset2),
|
|
232
|
+
})
|
|
233
|
+
s = int32(end)
|
|
234
|
+
nextEmit = s
|
|
235
|
+
offset1, offset2 = offset2, offset1
|
|
236
|
+
if s >= sLimit {
|
|
237
|
+
break mainLoop
|
|
238
|
+
}
|
|
239
|
+
cv = binary.LittleEndian.Uint64(src[s:])
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if int(nextEmit) < len(src) {
|
|
244
|
+
dst = append(dst, Match{
|
|
245
|
+
Unmatched: len(src) - int(nextEmit),
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return dst
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
func (z *ZDFast) hashShort(u uint64) uint32 {
|
|
253
|
+
return uint32(((u << 24) * 889523592379) >> (64 - zfastTableBits))
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
func (z *ZDFast) hashLong(u uint64) uint32 {
|
|
257
|
+
return uint32((u * 0xcf1bbcdcb7a56463) >> (64 - zdfastLongTableBits))
|
|
258
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
package matchfinder
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/binary"
|
|
5
|
+
"math"
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
type tableEntry struct {
|
|
9
|
+
val uint32
|
|
10
|
+
offset int32
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const (
|
|
14
|
+
zfastTableBits = 15
|
|
15
|
+
zfastTableSize = 1 << zfastTableBits
|
|
16
|
+
zfastHashLen = 6
|
|
17
|
+
|
|
18
|
+
prime6Bytes = 227718039650203
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
// ZFast is a MatchFinder based on the "Fastest" setting in
|
|
22
|
+
// github.com/klauspost/compress/zstd.
|
|
23
|
+
type ZFast struct {
|
|
24
|
+
MaxDistance int
|
|
25
|
+
|
|
26
|
+
history []byte
|
|
27
|
+
// current is the offset at the start of history
|
|
28
|
+
current int32
|
|
29
|
+
table [zfastTableSize]tableEntry
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
func (z *ZFast) Reset() {
|
|
33
|
+
z.current = 0
|
|
34
|
+
z.table = [zfastTableSize]tableEntry{}
|
|
35
|
+
z.history = z.history[:0]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
func (z *ZFast) FindMatches(dst []Match, src []byte) []Match {
|
|
39
|
+
if z.MaxDistance == 0 {
|
|
40
|
+
z.MaxDistance = 1 << 16
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Protect against overflow of current.
|
|
44
|
+
if int(z.current) >= int(math.MaxInt32)-2*z.MaxDistance-len(z.history) {
|
|
45
|
+
minOffset := z.current + int32(len(z.history)) - int32(z.MaxDistance)
|
|
46
|
+
for i := range z.table {
|
|
47
|
+
v := z.table[i].offset
|
|
48
|
+
if v < minOffset {
|
|
49
|
+
v = 0
|
|
50
|
+
} else {
|
|
51
|
+
v = v - z.current + int32(z.MaxDistance)
|
|
52
|
+
}
|
|
53
|
+
z.table[i].offset = v
|
|
54
|
+
}
|
|
55
|
+
z.current = int32(z.MaxDistance)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if len(z.history)+len(src) > cap(z.history) {
|
|
59
|
+
// history doesn't have enough capacity to hold the new block.
|
|
60
|
+
if cap(z.history) == 0 {
|
|
61
|
+
historySize := max(2*z.MaxDistance, 1<<20, len(src))
|
|
62
|
+
z.history = make([]byte, 0, historySize)
|
|
63
|
+
} else if len(z.history) > z.MaxDistance {
|
|
64
|
+
// Move down
|
|
65
|
+
offset := len(z.history) - z.MaxDistance
|
|
66
|
+
copy(z.history[:z.MaxDistance], z.history[offset:])
|
|
67
|
+
z.current += int32(offset)
|
|
68
|
+
z.history = z.history[:z.MaxDistance]
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
s := int32(len(z.history))
|
|
72
|
+
z.history = append(z.history, src...)
|
|
73
|
+
|
|
74
|
+
if len(src) < 10 {
|
|
75
|
+
return append(dst, Match{
|
|
76
|
+
Unmatched: len(src),
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
src = z.history
|
|
81
|
+
sLimit := int32(len(src)) - 8
|
|
82
|
+
|
|
83
|
+
const stepSize = 2
|
|
84
|
+
|
|
85
|
+
nextEmit := s
|
|
86
|
+
cv := binary.LittleEndian.Uint64(src[s:])
|
|
87
|
+
var offset1, offset2 int32
|
|
88
|
+
|
|
89
|
+
mainLoop:
|
|
90
|
+
for {
|
|
91
|
+
// t will contain the match offset when we find one.
|
|
92
|
+
// When exiting the search loop, we have already checked 4 bytes.
|
|
93
|
+
var t int32
|
|
94
|
+
|
|
95
|
+
for {
|
|
96
|
+
nextHash := z.hash(cv)
|
|
97
|
+
nextHash2 := z.hash(cv >> 8)
|
|
98
|
+
candidate := z.table[nextHash]
|
|
99
|
+
candidate2 := z.table[nextHash2]
|
|
100
|
+
repIndex := s - offset1 + 2
|
|
101
|
+
|
|
102
|
+
z.table[nextHash] = tableEntry{offset: s + z.current, val: uint32(cv)}
|
|
103
|
+
z.table[nextHash2] = tableEntry{offset: s + z.current + 1, val: uint32(cv >> 8)}
|
|
104
|
+
|
|
105
|
+
if offset1 != 0 && repIndex >= 0 && binary.LittleEndian.Uint32(src[repIndex:]) == uint32(cv>>16) {
|
|
106
|
+
// There is a repeated match at s+2.
|
|
107
|
+
end := extendMatch(src, int(repIndex+4), int(s+6))
|
|
108
|
+
start := s + 2
|
|
109
|
+
for repIndex > 0 && start > nextEmit && src[repIndex-1] == src[start-1] {
|
|
110
|
+
repIndex--
|
|
111
|
+
start--
|
|
112
|
+
}
|
|
113
|
+
dst = append(dst, Match{
|
|
114
|
+
Unmatched: int(start - nextEmit),
|
|
115
|
+
Length: end - int(start),
|
|
116
|
+
Distance: int(start - repIndex),
|
|
117
|
+
})
|
|
118
|
+
s = int32(end)
|
|
119
|
+
nextEmit = s
|
|
120
|
+
if s >= sLimit {
|
|
121
|
+
break mainLoop
|
|
122
|
+
}
|
|
123
|
+
cv = binary.LittleEndian.Uint64(src[s:])
|
|
124
|
+
continue
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
coffset0 := s - (candidate.offset - z.current)
|
|
128
|
+
coffset1 := s - (candidate2.offset - z.current) + 1
|
|
129
|
+
if coffset0 < int32(z.MaxDistance) && uint32(cv) == candidate.val && candidate.offset != 0 {
|
|
130
|
+
t = candidate.offset - z.current
|
|
131
|
+
if binary.LittleEndian.Uint32(src[t:]) == uint32(cv) {
|
|
132
|
+
// found a regular match
|
|
133
|
+
break
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if coffset1 < int32(z.MaxDistance) && uint32(cv>>8) == candidate2.val && candidate2.offset != 0 {
|
|
137
|
+
t = candidate2.offset - z.current
|
|
138
|
+
if binary.LittleEndian.Uint32(src[t:]) == uint32(cv>>8) {
|
|
139
|
+
s++
|
|
140
|
+
break
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
s += stepSize + ((s - nextEmit) >> 5)
|
|
145
|
+
if s > sLimit {
|
|
146
|
+
break mainLoop
|
|
147
|
+
}
|
|
148
|
+
cv = binary.LittleEndian.Uint64(src[s:])
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// A 4-byte match has been found. We'll later see if more than
|
|
152
|
+
// 4 bytes.
|
|
153
|
+
offset2 = offset1
|
|
154
|
+
offset1 = s - t
|
|
155
|
+
|
|
156
|
+
end := extendMatch(src, int(t+4), int(s+4))
|
|
157
|
+
for t > 0 && s > nextEmit && src[t-1] == src[s-1] {
|
|
158
|
+
s--
|
|
159
|
+
t--
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
dst = append(dst, Match{
|
|
163
|
+
Unmatched: int(s - nextEmit),
|
|
164
|
+
Length: end - int(s),
|
|
165
|
+
Distance: int(s - t),
|
|
166
|
+
})
|
|
167
|
+
s = int32(end)
|
|
168
|
+
nextEmit = s
|
|
169
|
+
if s >= sLimit {
|
|
170
|
+
break mainLoop
|
|
171
|
+
}
|
|
172
|
+
cv = binary.LittleEndian.Uint64(src[s:])
|
|
173
|
+
|
|
174
|
+
// Check offset 2
|
|
175
|
+
if o2 := s - offset2; offset2 != 0 && binary.LittleEndian.Uint32(src[o2:]) == uint32(cv) {
|
|
176
|
+
end := extendMatch(src, int(o2+4), int(s+4))
|
|
177
|
+
|
|
178
|
+
// Store the hash, since we have it.
|
|
179
|
+
nextHash := z.hash(cv)
|
|
180
|
+
z.table[nextHash] = tableEntry{offset: s + z.current, val: uint32(cv)}
|
|
181
|
+
dst = append(dst, Match{
|
|
182
|
+
Length: end - int(s),
|
|
183
|
+
Distance: int(offset2),
|
|
184
|
+
})
|
|
185
|
+
s = int32(end)
|
|
186
|
+
nextEmit = s
|
|
187
|
+
offset1, offset2 = offset2, offset1
|
|
188
|
+
if s >= sLimit {
|
|
189
|
+
break mainLoop
|
|
190
|
+
}
|
|
191
|
+
cv = binary.LittleEndian.Uint64(src[s:])
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if int(nextEmit) < len(src) {
|
|
196
|
+
dst = append(dst, Match{
|
|
197
|
+
Unmatched: len(src) - int(nextEmit),
|
|
198
|
+
})
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return dst
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
func (z *ZFast) hash(u uint64) uint32 {
|
|
205
|
+
return uint32(((u << 16) * prime6Bytes) >> (64 - zfastTableBits))
|
|
206
|
+
}
|