This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexp.go
227 lines (206 loc) · 4.54 KB
/
regexp.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package goal
import (
"regexp"
"strconv"
)
type rx struct {
Regexp *regexp.Regexp
}
func (r *rx) Matches(x BV) bool {
xv, ok := x.(*rx)
return ok && r.Regexp.String() == xv.Regexp.String()
}
func (r *rx) Append(ctx *Context, dst []byte) []byte {
dst = append(dst, "rx["...)
dst = strconv.AppendQuote(dst, r.Regexp.String())
dst = append(dst, ']')
return dst
}
func (r *rx) Type() string {
return "r"
}
type rxReplacer struct {
r *rx
repl V
}
func (r *rxReplacer) Matches(x BV) bool {
xv, ok := x.(*rxReplacer)
return ok && r.r.Matches(xv.r) && r.repl.Matches(xv.repl)
}
func (r *rxReplacer) Append(ctx *Context, dst []byte) []byte {
dst = append(dst, "sub["...)
dst = r.r.Append(ctx, dst)
dst = append(dst, ';')
dst = r.repl.Append(ctx, dst)
dst = append(dst, ']')
return dst
}
func (r *rxReplacer) Type() string {
return "f"
}
func (r *rxReplacer) stype() string {
return "rx"
}
func (r *rxReplacer) rank(ctx *Context) int {
return 1
}
func (r *rxReplacer) replace(ctx *Context, s string) string {
switch zv := r.repl.bv.(type) {
case S:
return r.r.Regexp.ReplaceAllString(string(s), string(zv))
default:
// zv is a function
f := func(s string) string {
r := ctx.Apply(r.repl, NewS(s))
switch rv := r.bv.(type) {
case S:
return string(rv)
default:
return r.Sprint(ctx)
}
}
r.repl.IncrRC()
rs := r.r.Regexp.ReplaceAllStringFunc(string(s), f)
r.repl.DecrRC()
return rs
}
}
// vfRx implements the rx variadic.
func vfRx(ctx *Context, args []V) V {
switch len(args) {
case 1:
return compileRegex(args[0])
default:
return panicRank("rx")
}
}
func compileRegex(x V) V {
switch xv := x.bv.(type) {
case S:
r, err := regexp.Compile(string(xv))
if err != nil {
return Errorf("rx x : %v", err)
}
return NewV(&rx{Regexp: r})
default:
return panicType("rx x", "x", x)
}
}
func applyRx(x *rx, y V) V {
if x.Regexp.NumSubexp() > 0 {
return applyRxFindSubmatch(x, y)
}
return applyRxMatch(x, y)
}
func applyRxMatch(x *rx, y V) V {
switch yv := y.bv.(type) {
case S:
return NewI(b2I(x.Regexp.MatchString(string(yv))))
case *AS:
r := make([]byte, yv.Len())
for i, s := range yv.elts {
r[i] = b2B(x.Regexp.MatchString(s))
}
return NewAB(r)
case *AV:
return mapAV(yv, func(yi V) V { return applyRxMatch(x, yi) })
default:
return panicType("r[y]", "y", y)
}
}
func applyRxFindSubmatch(x *rx, y V) V {
switch yv := y.bv.(type) {
case S:
return NewAS(x.Regexp.FindStringSubmatch(string(yv)))
case *AS:
r := make([]V, yv.Len())
for i, yi := range yv.elts {
ri := applyRxFindSubmatch(x, NewS(yi))
ri.MarkImmutable()
r[i] = ri
}
return newAVu(r)
case *AV:
return mapAV(yv, func(yi V) V { return applyRxFindSubmatch(x, yi) })
default:
return panicType("r[y]", "y", y)
}
}
func applyRx2(x *rx, y, z V) V {
var n int64
if z.IsI() {
n = z.I()
} else if z.IsF() {
if !isI(z.F()) {
return Panicf("r[x;y] : non-integer y (%g)", z.F())
}
n = int64(z.F())
} else {
return panicType("r[x;y]", "y", z)
}
if x.Regexp.NumSubexp() > 0 {
return applyRxFindAllSubmatch(x, y, n)
}
return applyRxFindAll(x, y, n)
}
func applyRxFindAllSubmatch(x *rx, y V, n int64) V {
switch yv := y.bv.(type) {
case S:
matches := x.Regexp.FindAllStringSubmatch(string(yv), int(n))
r := make([]V, len(matches))
for i, sm := range matches {
r[i] = NewV(&AS{elts: sm, flags: flagImmutable})
}
return newAVu(r)
case *AS:
r := make([]V, yv.Len())
for i, yi := range yv.elts {
ri := applyRxFindAllSubmatch(x, NewS(yi), n)
ri.MarkImmutable()
r[i] = ri
}
return newAVu(r)
case *AV:
return mapAV(yv, func(yi V) V { return applyRxFindAllSubmatch(x, yi, n) })
default:
return panicType("r[y]", "y", y)
}
}
func applyRxFindAll(x *rx, y V, n int64) V {
switch yv := y.bv.(type) {
case S:
matches := x.Regexp.FindAllString(string(yv), int(n))
return NewAS(matches)
case *AS:
r := make([]V, yv.Len())
for i, yi := range yv.elts {
ri := applyRxFindAll(x, NewS(yi), n)
ri.MarkImmutable()
r[i] = ri
}
return newAVu(r)
case *AV:
return mapAV(yv, func(yi V) V { return applyRxFindAll(x, yi, n) })
default:
return panicType("r[y]", "y", y)
}
}
func splitRx(f *rx, x V) V {
switch xv := x.bv.(type) {
case S:
r := f.Regexp.Split(string(xv), -1)
return NewAS(r)
case *AS:
r := make([]V, xv.Len())
for i, xi := range xv.elts {
ri := NewAS(f.Regexp.Split(string(xi), -1))
ri.MarkImmutable()
r[i] = ri
}
return newAVu(r)
case *AV:
return mapAV(xv, func(xi V) V { return splitRx(f, xi) })
default:
return panicType("r\\x", "x", x)
}
}