-
-
Notifications
You must be signed in to change notification settings - Fork 439
/
search.go
74 lines (63 loc) · 1.75 KB
/
search.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
. "github.com/antonmedv/fx/internal/jsonx"
)
type search struct {
err error
results []*Node
cursor int
values map[*Node][]match
keys map[*Node][]match
}
func newSearch() *search {
return &search{
results: make([]*Node, 0),
values: make(map[*Node][]match),
keys: make(map[*Node][]match),
}
}
type match struct {
start, end int
index int
}
type piece struct {
b []byte
index int
}
func splitBytesByIndexes(b []byte, indexes []match) []piece {
out := make([]piece, 0, 1)
pos := 0
for _, pair := range indexes {
out = append(out, piece{safeSlice(b, pos, pair.start), -1})
out = append(out, piece{safeSlice(b, pair.start, pair.end), pair.index})
pos = pair.end
}
out = append(out, piece{safeSlice(b, pos, len(b)), -1})
return out
}
func splitIndexesToChunks(chunks [][]byte, indexes [][]int, searchIndex int) (chunkIndexes [][]match) {
chunkIndexes = make([][]match, len(chunks))
for index, idx := range indexes {
position := 0
for i, chunk := range chunks {
// If start index lies in this chunk
if idx[0] < position+len(chunk) {
// Calculate local start and end for this chunk
localStart := idx[0] - position
localEnd := idx[1] - position
// If the end index also lies in this chunk
if idx[1] <= position+len(chunk) {
chunkIndexes[i] = append(chunkIndexes[i], match{start: localStart, end: localEnd, index: searchIndex + index})
break
} else {
// If the end index is outside this chunk, split the index
chunkIndexes[i] = append(chunkIndexes[i], match{start: localStart, end: len(chunk), index: searchIndex + index})
// Adjust the starting index for the next chunk
idx[0] = position + len(chunk)
}
}
position += len(chunk)
}
}
return
}