-
Notifications
You must be signed in to change notification settings - Fork 9
/
search.go
136 lines (124 loc) · 3.11 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
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
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dfa
import (
"sync"
"errors"
"math"
"matloob.io/regexp/internal/input"
"matloob.io/regexp/syntax"
)
type Searcher struct {
mu sync.Mutex
re *syntax.Regexp
prog *syntax.Prog
prefixer input.Prefixer
fdfa, ldfa, revdfa *DFA
}
func (s *Searcher) Init(prog *syntax.Prog, expr *syntax.Regexp, p input.Prefixer) {
s.prog = prog
s.re = expr
s.prefixer = p
}
var errNotDFA = errors.New("can't use dfa")
func (s *Searcher) Search(i input.Input, pos int, longest bool, matchcap *[]int, ncap int) (bool, error) {
const budget = (2 << 20)/3
rinput, ok := i.(input.Rinput)
if !ok {
return false, errNotDFA
}
var dfa *DFA
if longest {
s.mu.Lock()
if s.ldfa == nil {
s.ldfa = newDFA(s.prog, longestMatch, budget)
s.ldfa.prefixer = s.prefixer
}
dfa = s.ldfa
s.mu.Unlock()
} else {
s.mu.Lock()
if s.fdfa == nil {
s.fdfa = newDFA(s.prog, firstMatch, budget)
s.fdfa.prefixer = s.prefixer
}
dfa = s.fdfa
s.mu.Unlock()
}
var revdfa *DFA
if s.revdfa == nil {
s.mu.Lock()
revprog, err := syntax.CompileReversed(s.re)
if err != nil {
panic("CompileReversed failed")
}
s.revdfa = newDFA(revprog, longestMatch, budget)
s.mu.Unlock()
}
s.mu.Lock()
revdfa = s.revdfa
s.mu.Unlock()
var matched bool
*matchcap = (*matchcap)[:ncap]
p, ep, matched, err := search(dfa, revdfa, rinput, pos)
if err != nil {
return false, errNotDFA
}
if ncap > 0 {
(*matchcap)[0], (*matchcap)[1] = p, ep
}
return matched, nil
}
type searchParams struct {
input input.Rinput
startpos int
anchored bool
wantEarliestMatch bool
runForward bool
start *State
firstbyte int64 // int64 to be compatible with atomic ops
failed bool // "out" parameter: whether search gave up
ep int // "out" parameter: end pointer for match
matches []int
}
func isanchored(prog *syntax.Prog) bool {
return prog.StartCond() & syntax.EmptyBeginText != 0
}
func search(d, reversed *DFA, i input.Rinput, startpos int) (start int, end int, matched bool, err error) {
params := searchParams{}
params.startpos = startpos
params.wantEarliestMatch = false
params.input = i
params.anchored = isanchored(d.prog)
params.runForward = true
params.ep = int(math.MaxInt64)
if !d.analyzeSearch(¶ms) {
return -1, -1, false, errors.New("analyze search failed on forward DFA")
}
b := d.searchLoop(¶ms)
if params.failed {
return -1, -1, false, errFallBack
}
if !b {
return -1, -1, false, nil
}
end = params.ep
params = searchParams{}
params.startpos = startpos
params.ep = end
params.anchored = true
params.input = i
params.runForward = false
if !reversed.analyzeSearch(¶ms) {
return -2, -2, false, errors.New("analyze search failed on reverse DFA")
}
b = reversed.searchLoop(¶ms)
if DebugDFA {
DebugPrintf("\nkind %d\n%v\n", d.kind, d.prog)
}
if params.failed {
return -1, -1, false, errFallBack
}
return params.ep, end, b, nil
}