forked from google/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdociter.go
145 lines (123 loc) · 3.68 KB
/
dociter.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zoekt
import (
"bytes"
"fmt"
"log"
"sort"
"github.com/google/zoekt/query"
)
var _ = log.Println
// candidateMatch is a candidate match for a substring.
type candidateMatch struct {
caseSensitive bool
fileName bool
substrBytes []byte
substrLowered []byte
file uint32
// Offsets are relative to the start of the filename or file contents.
runeOffset uint32
byteOffset uint32
byteMatchSz uint32
}
func (m *candidateMatch) String() string {
return fmt.Sprintf("%d:%d", m.file, m.runeOffset)
}
func (m *candidateMatch) matchContent(content []byte) bool {
if m.caseSensitive {
comp := bytes.Compare(m.substrBytes, content[m.byteOffset:m.byteOffset+uint32(len(m.substrBytes))]) == 0
return comp
} else {
// It is tempting to try a simple ASCII based
// comparison if possible, but we need more
// information. Simple ASCII chars have unicode upper
// case variants (the ASCII 'k' has the Kelvin symbol
// as upper case variant). We can only degrade to
// ASCII if we are sure that both the corpus and the
// query is ASCII only
return caseFoldingEqualsRunes(m.substrLowered, content[m.byteOffset:])
}
}
// line returns the line holding the match. If the match starts with
// the newline ending line M, we return M. The line is characterized
// by its linenumber (base-1, byte index of line start, byte index of
// line end). The line end is the index of a newline, or the filesize
// (if matching the last line of the file.)
func (m *candidateMatch) line(newlines []uint32, fileSize uint32) (lineNum, lineStart, lineEnd int) {
idx := sort.Search(len(newlines), func(n int) bool {
return newlines[n] >= m.byteOffset
})
end := int(fileSize)
if idx < len(newlines) {
end = int(newlines[idx])
}
start := 0
if idx > 0 {
start = int(newlines[idx-1] + 1)
}
return idx + 1, start, end
}
type ngramDocIterator struct {
query *query.Substring
leftPad uint32
rightPad uint32
distance uint32
first []uint32
last []uint32
ends []uint32
}
func (s *ngramDocIterator) candidates() []*candidateMatch {
patBytes := []byte(s.query.Pattern)
lowerPatBytes := toLower(patBytes)
fileIdx := 0
var candidates []*candidateMatch
for {
if len(s.first) == 0 || len(s.last) == 0 {
break
}
p1 := s.first[0]
p2 := s.last[0]
for fileIdx < len(s.ends) && s.ends[fileIdx] <= p1 {
fileIdx++
}
if p1+s.distance < p2 {
s.first = s.first[1:]
} else if p1+s.distance > p2 {
s.last = s.last[1:]
} else {
s.first = s.first[1:]
s.last = s.last[1:]
var fileStart uint32
if fileIdx > 0 {
fileStart = s.ends[fileIdx-1]
}
if p1 < s.leftPad+fileStart || p1+s.distance+ngramSize+s.rightPad > s.ends[fileIdx] {
continue
}
cand := &candidateMatch{
caseSensitive: s.query.CaseSensitive,
fileName: s.query.FileName,
substrBytes: patBytes,
substrLowered: lowerPatBytes,
// TODO - this is wrong for casefolding searches.
byteMatchSz: uint32(len(lowerPatBytes)),
file: uint32(fileIdx),
runeOffset: p1 - fileStart - s.leftPad,
}
candidates = append(candidates, cand)
}
}
return candidates
}