/
matcher-pcre.go
209 lines (182 loc) · 4.61 KB
/
matcher-pcre.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* Go PCRE Matcher
*
* Copyright 2017 Tenta, LLC
*
* 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.
*
* For any questions, please contact developer@tenta.io
*
* matcher-pcre.go: PCRE matcher implementation
*/
package matcherpcre
import (
"regexp"
"strconv"
"strings"
"github.com/tenta-browser/go-pcre-matcher"
"github.com/gijsbers/go-pcre"
)
type pcreEngine struct {
}
type pcreRegexp struct {
wre pcre.Regexp
}
type pcreMatch struct {
wm *pcre.Matcher
subject string
}
// NewEngine creates a direct libpcre regexp engine instance
func NewEngine() matcher.Engine {
return &pcreEngine{}
}
func (e *pcreEngine) Compile(pattern string, flags int) (matcher.Regexp, error) {
wre, err := pcre.Compile(pattern, flags)
if err != nil {
return nil, err
}
return &pcreRegexp{wre}, nil
}
func (e *pcreEngine) Quote(s string) string {
return regexp.QuoteMeta(s)
}
func (e *pcreEngine) FlagDotAll() int {
return pcre.DOTALL
}
func (e *pcreEngine) FlagExtended() int {
return pcre.EXTENDED
}
func (e *pcreEngine) FlagUnicode() int {
return pcre.UCP
}
func (e *pcreEngine) FlagCaseInsensitive() int {
return pcre.CASELESS
}
func (e *pcreEngine) FlagMultiline() int {
return pcre.MULTILINE
}
func (re *pcreRegexp) Search(subject string) matcher.Match {
wm := re.wre.MatcherString(subject, 0)
if !wm.Matches() {
return nil
}
return &pcreMatch{wm, subject}
}
func (re *pcreRegexp) Replace(subject, repl string) string {
//
// goals: to work like the one in Java
// non-goals: to be fast, since it's for testing only
//
ln := len(repl)
return re.replaceFuncCommon(subject, func(m matcher.Match) string {
parts := []string{}
for c := 0; ; {
b := c
for ; c < ln && repl[c] != '\\' && repl[c] != '$'; c++ {
}
parts = append(parts, repl[b:c])
if c == ln {
break
}
if repl[c] == '\\' {
c++
if c < ln {
parts = append(parts, string(repl[c]))
c++
} else {
panic("Character to be escaped is missing")
}
} else if repl[c] == '$' {
c++
grp := ""
part := ""
if c < ln && repl[c] == '{' {
c++
for ; c < ln && repl[c] != '}'; c++ {
grp += string(repl[c])
}
if c < ln {
c++
}
if grp == "" {
panic("Group name is missing")
}
part = m.GroupByName(grp)
} else {
for ; c < ln && repl[c] >= '0' && repl[c] <= '9'; c++ {
ngrp := grp + string(repl[c])
if grpn, _ := strconv.Atoi(ngrp); grpn > m.Groups() {
break
}
grp = ngrp
}
if grp == "" {
panic("Group index is missing")
}
grpn, _ := strconv.Atoi(grp)
part = m.GroupByIdx(grpn)
}
parts = append(parts, part)
}
}
return strings.Join(parts, "")
})
}
func (re *pcreRegexp) ReplaceFunc(subject string, replacer matcher.Replacer) string {
return re.replaceFuncCommon(subject, replacer.Replacement)
}
func (re *pcreRegexp) replaceFuncCommon(subject string, replacementFunc func(matcher.Match) string) string {
wm := re.wre.MatcherString(subject, 0)
parts := []string{}
for wm.Matches() {
// append substring up to match
idxs := wm.Index()
parts = append(parts, subject[:idxs[0]])
// append replacement
parts = append(parts, replacementFunc(&pcreMatch{wm, subject}))
// find next match
subject = subject[idxs[1]:]
wm.MatchString(subject, 0)
}
parts = append(parts, subject)
return strings.Join(parts, "")
}
func (m *pcreMatch) Groups() int {
return m.wm.Groups()
}
func (m *pcreMatch) GroupByIdx(idx int) string {
return m.wm.GroupString(idx)
}
func (m *pcreMatch) GroupPresentByIdx(idx int) bool {
return m.wm.Present(idx)
}
func (m *pcreMatch) GroupByName(name string) string {
group, err := m.wm.NamedString(name)
if err != nil {
return ""
}
return group
}
func (m *pcreMatch) GroupPresentByName(name string) bool {
present, err := m.wm.NamedPresent(name)
return err == nil && present
}
func (m *pcreMatch) Next() bool {
matchRegion := m.wm.Index()
if matchRegion == nil {
// there's no current match, so there's no next one either
return false
}
m.subject = m.subject[matchRegion[1]:] // trim matched part of subject
return m.wm.MatchString(m.subject, 0)
}