-
Notifications
You must be signed in to change notification settings - Fork 10
/
matching.go
382 lines (369 loc) · 9.01 KB
/
matching.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package stringlib
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/arnodel/golua/lib/stringlib/pattern"
"github.com/arnodel/golua/luastrings"
rt "github.com/arnodel/golua/runtime"
)
func find(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
var (
s, ptn string
init int64 = 1
plain bool
)
err := c.CheckNArgs(2)
if err == nil {
s, err = c.StringArg(0)
}
if err == nil {
ptn, err = c.StringArg(1)
}
if err == nil && c.NArgs() >= 3 {
init, err = c.IntArg(2)
if err == nil && c.NArgs() >= 4 {
plain = rt.Truth(c.Arg(3))
}
}
if err != nil {
return nil, err
}
si := luastrings.StringNormPos(s, int(init)) - 1
if si < 0 {
si = 0
}
next := c.Next()
switch {
case si < 0 || si > len(s):
t.Push1(next, rt.NilValue)
case plain || len(ptn) == 0:
// strings.Index is linear
t.RequireCPU(uint64(len(s) - si))
i := strings.Index(s[si:], ptn)
if i == -1 {
t.Push1(next, rt.NilValue)
} else {
t.Push1(next, rt.IntValue(int64(i+1)))
t.Push1(next, rt.IntValue(int64(i+len(ptn))))
}
default:
pat, err := pattern.New(string(ptn))
if err != nil {
return nil, err
}
captures, usedCPU := pat.MatchFromStart(string(s), si, t.UnusedCPU())
t.RequireCPU(usedCPU)
if len(captures) == 0 {
t.Push1(next, rt.NilValue)
} else {
first := captures[0]
t.Push1(next, rt.IntValue(int64(first.Start()+1)))
t.Push1(next, rt.IntValue(int64(first.End())))
pushExtraCaptures(t.Runtime, captures, s, next)
}
}
return next, nil
}
func match(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
var (
s, ptn string
init int64 = 1
)
err := c.CheckNArgs(2)
if err == nil {
s, err = c.StringArg(0)
}
if err == nil {
ptn, err = c.StringArg(1)
}
if err == nil && c.NArgs() >= 3 {
init, err = c.IntArg(2)
}
if err != nil {
return nil, err
}
si := luastrings.StringNormPos(s, int(init)) - 1
if si < 0 {
si = 0
}
next := c.Next()
pat, ptnErr := pattern.New(string(ptn))
if ptnErr != nil {
return nil, ptnErr
}
captures, usedCPU := pat.MatchFromStart(string(s), si, t.UnusedCPU())
t.RequireCPU(usedCPU)
pushCaptures(t.Runtime, captures, s, next)
return next, nil
}
func pushCaptures(r *rt.Runtime, captures []pattern.Capture, s string, next rt.Cont) {
switch len(captures) {
case 0:
r.Push1(next, rt.NilValue)
case 1:
c := captures[0]
r.RequireBytes(c.End() - c.Start())
r.Push1(next, rt.StringValue(s[c.Start():c.End()]))
default:
pushExtraCaptures(r, captures, s, next)
}
}
func pushExtraCaptures(r *rt.Runtime, captures []pattern.Capture, s string, next rt.Cont) {
if len(captures) < 2 {
return
}
for _, c := range captures[1:] {
r.Push1(next, captureValue(r, c, s))
}
}
func captureValue(r *rt.Runtime, c pattern.Capture, s string) rt.Value {
if c.IsEmpty() {
return rt.IntValue(int64(c.Start() + 1))
}
r.RequireBytes(c.End() - c.Start())
return rt.StringValue(s[c.Start():c.End()])
}
func gmatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
var (
s, ptn string
err = c.CheckNArgs(2)
init int64 = 1
)
if err == nil {
s, err = c.StringArg(0)
}
if err == nil {
ptn, err = c.StringArg(1)
}
if err == nil && c.NArgs() >= 3 {
init, err = c.IntArg(2)
}
if err != nil {
return nil, err
}
pat, ptnErr := pattern.New(string(ptn))
if ptnErr != nil {
return nil, ptnErr
}
si := luastrings.StringNormPos(s, int(init)) - 1
if si < 0 {
si = 0
}
allowEmpty := true
var iterator = func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
next := c.Next()
var (
captures []pattern.Capture
usedCPU uint64
)
for {
captures, usedCPU = pat.Match(s, si, t.UnusedCPU())
t.RequireCPU(usedCPU)
if len(captures) == 0 {
break
}
gc := captures[0]
start, end := gc.Start(), gc.End()
if allowEmpty || start != si || end != si {
allowEmpty = start >= end
if allowEmpty {
si = start + 1
} else {
si = end
}
break
}
si++
allowEmpty = true
}
pushCaptures(t.Runtime, captures, s, next)
return next, nil
}
iterGof := rt.NewGoFunction(iterator, "gmatchiterator", 0, false)
iterGof.SolemnlyDeclareCompliance(rt.ComplyCpuSafe | rt.ComplyMemSafe | rt.ComplyTimeSafe | rt.ComplyIoSafe)
return c.PushingNext(t.Runtime, rt.FunctionValue(iterGof)), nil
}
func gsub(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
var (
s, ptn string
n int64 = -1
repl rt.Value
)
err := c.CheckNArgs(3)
if err == nil {
s, err = c.StringArg(0)
}
if err == nil {
ptn, err = c.StringArg(1)
}
if err == nil && c.NArgs() >= 4 {
n, err = c.IntArg(3)
}
if err != nil {
return nil, err
}
repl = c.Arg(2)
pat, ptnErr := pattern.New(string(ptn))
if ptnErr != nil {
return nil, ptnErr
}
// replF will be the function that does the substitution of the match given
// the captures in the match. It must require the memory for the
// substitution string. It returns the substituted string, true if a
// substitution was actually made, and a non-nil error if something went
// wrong.
var replF func([]pattern.Capture) (string, bool, error)
if replString, ok := repl.TryString(); ok {
replF = func(captures []pattern.Capture) (string, bool, error) {
cStrings := [10]string{}
maxIndex := len(captures) - 1
for i, c := range captures {
v := captureValue(t.Runtime, c, s)
switch v.Type() {
case rt.StringType:
cStrings[i] = v.AsString()
case rt.IntType:
cStrings[i] = strconv.Itoa(int(v.AsInt()))
}
}
if len(captures) == 1 {
cStrings[1] = cStrings[0]
maxIndex = 1
}
var err error
t.RequireCPU(uint64(len(replString)))
t.RequireBytes(len(replString))
return gsubPtn.ReplaceAllStringFunc(replString, func(x string) string {
if err != nil {
return ""
}
b := x[1]
switch {
case '0' <= b && b <= '9':
idx := int(b - '0')
if idx > maxIndex {
err = pattern.ErrInvalidCaptureIdx(idx)
return ""
}
s := cStrings[b-'0']
if len(s) > 2 {
t.RequireBytes(len(s) - 2)
}
return s
case b == '%':
return x[1:]
default:
err = pattern.ErrInvalidPct
}
return x[1:]
}), false, err
}
} else if replTable, ok := repl.TryTable(); ok {
replF = func(captures []pattern.Capture) (string, bool, error) {
gc := captures[0]
i := 0
if len(captures) >= 2 {
i = 1
}
c := captures[i]
val, err := rt.Index(t, rt.TableValue(replTable), captureValue(t.Runtime, c, s))
if err != nil {
return "", false, err
}
return subToString(t.Runtime, s[gc.Start():gc.End()], val)
}
} else if replC, ok := repl.TryCallable(); ok {
replF = func(captures []pattern.Capture) (string, bool, error) {
term := rt.NewTerminationWith(c, 1, false)
cont := replC.Continuation(t, term)
gc := captures[0]
i := 0
if len(captures) >= 2 {
i = 1
}
for _, c := range captures[i:] {
t.Push1(cont, captureValue(t.Runtime, c, s))
}
err := t.RunContinuation(cont)
if err != nil {
return "", false, err
}
return subToString(t.Runtime, s[gc.Start():gc.End()], term.Get(0))
}
} else {
return nil, errors.New("#3 must be a string, table or function")
}
var (
si int // Index in s where to start finding the next match
sj int // Index in s of the first byte not yet copied
sb strings.Builder // Build the result string into this
matchCount int64
allowEmpty = true
)
// We require memory for the string we build as we go along. In order to
// save allocations in case there are no substitutions, we do not start
// copying the string until one substitution has actually taken place. This
// is achieved by keeping the variable sj the same until bytes are written
// in the string builder.
for ; matchCount != n; matchCount++ {
captures, usedCPU := pat.Match(string(s), si, t.UnusedCPU())
t.RequireCPU(usedCPU)
if len(captures) == 0 {
break
}
gc := captures[0]
start, end := gc.Start(), gc.End()
if allowEmpty || start != si || end != si {
sub, same, err := replF(captures)
if err != nil {
return nil, err
}
if !same {
t.RequireBytes(start - sj)
// No need to require memory for sub as that has been done already
// by replF
_, _ = sb.WriteString(s[sj:start])
_, _ = sb.WriteString(sub)
sj = end
}
}
allowEmpty = start >= end
if allowEmpty {
si = start + 1
} else {
si = end
}
}
var res rt.Value
switch {
case sb.Len() == 0:
// We return the input string to save an allocation.
res = c.Arg(0)
case sj < len(s):
t.RequireBytes(len(s) - sj)
_, _ = sb.WriteString(s[sj:])
fallthrough
default:
res = rt.StringValue(sb.String())
}
next := c.Next()
// Already required memory for the string below.
t.Push1(next, res)
t.Push1(next, rt.IntValue(matchCount))
return next, nil
}
var gsubPtn = regexp.MustCompile("%.")
func subToString(r *rt.Runtime, key string, val rt.Value) (string, bool, error) {
if !rt.Truth(val) {
return key, true, nil
}
res, ok := val.ToString()
if ok {
r.RequireBytes(len(res))
return res, false, nil
}
return "", false, fmt.Errorf("invalid replacement value (a %s)", val.TypeName())
}