-
Notifications
You must be signed in to change notification settings - Fork 653
/
stringlib.go
448 lines (408 loc) · 9.38 KB
/
stringlib.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package lua
import (
"fmt"
"strings"
"github.com/yuin/gopher-lua/pm"
)
const emptyLString LString = LString("")
func OpenString(L *LState) int {
var mod *LTable
//_, ok := L.G.builtinMts[int(LTString)]
//if !ok {
mod = L.RegisterModule(StringLibName, strFuncs).(*LTable)
gmatch := L.NewClosure(strGmatch, L.NewFunction(strGmatchIter))
mod.RawSetString("gmatch", gmatch)
mod.RawSetString("gfind", gmatch)
mod.RawSetString("__index", mod)
L.G.builtinMts[int(LTString)] = mod
//}
L.Push(mod)
return 1
}
var strFuncs = map[string]LGFunction{
"byte": strByte,
"char": strChar,
"dump": strDump,
"find": strFind,
"format": strFormat,
"gsub": strGsub,
"len": strLen,
"lower": strLower,
"match": strMatch,
"rep": strRep,
"reverse": strReverse,
"sub": strSub,
"upper": strUpper,
}
func strByte(L *LState) int {
str := L.CheckString(1)
start := L.OptInt(2, 1) - 1
end := L.OptInt(3, -1)
l := len(str)
if start < 0 {
start = l + start + 1
}
if end < 0 {
end = l + end + 1
}
if L.GetTop() == 2 {
if start < 0 || start >= l {
return 0
}
L.Push(LNumber(str[start]))
return 1
}
start = intMax(start, 0)
end = intMin(end, l)
if end < 0 || end <= start || start >= l {
return 0
}
for i := start; i < end; i++ {
L.Push(LNumber(str[i]))
}
return end - start
}
func strChar(L *LState) int {
top := L.GetTop()
bytes := make([]byte, L.GetTop())
for i := 1; i <= top; i++ {
bytes[i-1] = uint8(L.CheckInt(i))
}
L.Push(LString(string(bytes)))
return 1
}
func strDump(L *LState) int {
L.RaiseError("GopherLua does not support the string.dump")
return 0
}
func strFind(L *LState) int {
str := L.CheckString(1)
pattern := L.CheckString(2)
if len(pattern) == 0 {
L.Push(LNumber(1))
L.Push(LNumber(0))
return 2
}
init := luaIndex2StringIndex(str, L.OptInt(3, 1), true)
plain := false
if L.GetTop() == 4 {
plain = LVAsBool(L.Get(4))
}
if plain {
pos := strings.Index(str[init:], pattern)
if pos < 0 {
L.Push(LNil)
return 1
}
L.Push(LNumber(init+pos) + 1)
L.Push(LNumber(init + pos + len(pattern)))
return 2
}
mds, err := pm.Find(pattern, unsafeFastStringToReadOnlyBytes(str), init, 1)
if err != nil {
L.RaiseError(err.Error())
}
if len(mds) == 0 {
L.Push(LNil)
return 1
}
md := mds[0]
L.Push(LNumber(md.Capture(0) + 1))
L.Push(LNumber(md.Capture(1)))
for i := 2; i < md.CaptureLength(); i += 2 {
if md.IsPosCapture(i) {
L.Push(LNumber(md.Capture(i)))
} else {
L.Push(LString(str[md.Capture(i):md.Capture(i+1)]))
}
}
return md.CaptureLength()/2 + 1
}
func strFormat(L *LState) int {
str := L.CheckString(1)
args := make([]interface{}, L.GetTop()-1)
top := L.GetTop()
for i := 2; i <= top; i++ {
args[i-2] = L.Get(i)
}
npat := strings.Count(str, "%") - strings.Count(str, "%%")
L.Push(LString(fmt.Sprintf(str, args[:intMin(npat, len(args))]...)))
return 1
}
func strGsub(L *LState) int {
str := L.CheckString(1)
pat := L.CheckString(2)
L.CheckTypes(3, LTString, LTTable, LTFunction)
repl := L.CheckAny(3)
limit := L.OptInt(4, -1)
mds, err := pm.Find(pat, unsafeFastStringToReadOnlyBytes(str), 0, limit)
if err != nil {
L.RaiseError(err.Error())
}
if len(mds) == 0 {
L.SetTop(1)
L.Push(LNumber(0))
return 2
}
switch lv := repl.(type) {
case LString:
L.Push(LString(strGsubStr(L, str, string(lv), mds)))
case *LTable:
L.Push(LString(strGsubTable(L, str, lv, mds)))
case *LFunction:
L.Push(LString(strGsubFunc(L, str, lv, mds)))
}
L.Push(LNumber(len(mds)))
return 2
}
type replaceInfo struct {
Indicies []int
String string
}
func checkCaptureIndex(L *LState, m *pm.MatchData, idx int) {
if idx <= 2 {
return
}
if idx >= m.CaptureLength() {
L.RaiseError("invalid capture index")
}
}
func capturedString(L *LState, m *pm.MatchData, str string, idx int) string {
checkCaptureIndex(L, m, idx)
if idx >= m.CaptureLength() && idx == 2 {
idx = 0
}
if m.IsPosCapture(idx) {
return fmt.Sprint(m.Capture(idx))
} else {
return str[m.Capture(idx):m.Capture(idx+1)]
}
}
func strGsubDoReplace(str string, info []replaceInfo) string {
offset := 0
buf := []byte(str)
for _, replace := range info {
oldlen := len(buf)
b1 := append([]byte(""), buf[0:offset+replace.Indicies[0]]...)
b2 := []byte("")
index2 := offset + replace.Indicies[1]
if index2 <= len(buf) {
b2 = append(b2, buf[index2:len(buf)]...)
}
buf = append(b1, replace.String...)
buf = append(buf, b2...)
offset += len(buf) - oldlen
}
return string(buf)
}
func strGsubStr(L *LState, str string, repl string, matches []*pm.MatchData) string {
infoList := make([]replaceInfo, 0, len(matches))
for _, match := range matches {
start, end := match.Capture(0), match.Capture(1)
sc := newFlagScanner('%', "", "", repl)
for c, eos := sc.Next(); !eos; c, eos = sc.Next() {
if !sc.ChangeFlag {
if sc.HasFlag {
if c >= '0' && c <= '9' {
sc.AppendString(capturedString(L, match, str, 2*(int(c)-48)))
} else {
sc.AppendChar('%')
sc.AppendChar(c)
}
sc.HasFlag = false
} else {
sc.AppendChar(c)
}
}
}
infoList = append(infoList, replaceInfo{[]int{start, end}, sc.String()})
}
return strGsubDoReplace(str, infoList)
}
func strGsubTable(L *LState, str string, repl *LTable, matches []*pm.MatchData) string {
infoList := make([]replaceInfo, 0, len(matches))
for _, match := range matches {
idx := 0
if match.CaptureLength() > 2 { // has captures
idx = 2
}
var value LValue
if match.IsPosCapture(idx) {
value = L.GetTable(repl, LNumber(match.Capture(idx)))
} else {
value = L.GetField(repl, str[match.Capture(idx):match.Capture(idx+1)])
}
if !LVIsFalse(value) {
infoList = append(infoList, replaceInfo{[]int{match.Capture(0), match.Capture(1)}, LVAsString(value)})
}
}
return strGsubDoReplace(str, infoList)
}
func strGsubFunc(L *LState, str string, repl *LFunction, matches []*pm.MatchData) string {
infoList := make([]replaceInfo, 0, len(matches))
for _, match := range matches {
start, end := match.Capture(0), match.Capture(1)
L.Push(repl)
nargs := 0
if match.CaptureLength() > 2 { // has captures
for i := 2; i < match.CaptureLength(); i += 2 {
if match.IsPosCapture(i) {
L.Push(LNumber(match.Capture(i)))
} else {
L.Push(LString(capturedString(L, match, str, i)))
}
nargs++
}
} else {
L.Push(LString(capturedString(L, match, str, 0)))
nargs++
}
L.Call(nargs, 1)
ret := L.reg.Pop()
if !LVIsFalse(ret) {
infoList = append(infoList, replaceInfo{[]int{start, end}, LVAsString(ret)})
}
}
return strGsubDoReplace(str, infoList)
}
type strMatchData struct {
str string
pos int
matches []*pm.MatchData
}
func strGmatchIter(L *LState) int {
md := L.CheckUserData(1).Value.(*strMatchData)
str := md.str
matches := md.matches
idx := md.pos
md.pos += 1
if idx == len(matches) {
return 0
}
L.Push(L.Get(1))
match := matches[idx]
if match.CaptureLength() == 2 {
L.Push(LString(str[match.Capture(0):match.Capture(1)]))
return 1
}
for i := 2; i < match.CaptureLength(); i += 2 {
if match.IsPosCapture(i) {
L.Push(LNumber(match.Capture(i)))
} else {
L.Push(LString(str[match.Capture(i):match.Capture(i+1)]))
}
}
return match.CaptureLength()/2 - 1
}
func strGmatch(L *LState) int {
str := L.CheckString(1)
pattern := L.CheckString(2)
mds, err := pm.Find(pattern, []byte(str), 0, -1)
if err != nil {
L.RaiseError(err.Error())
}
L.Push(L.Get(UpvalueIndex(1)))
ud := L.NewUserData()
ud.Value = &strMatchData{str, 0, mds}
L.Push(ud)
return 2
}
func strLen(L *LState) int {
str := L.CheckString(1)
L.Push(LNumber(len(str)))
return 1
}
func strLower(L *LState) int {
str := L.CheckString(1)
L.Push(LString(strings.ToLower(str)))
return 1
}
func strMatch(L *LState) int {
str := L.CheckString(1)
pattern := L.CheckString(2)
offset := L.OptInt(3, 1)
l := len(str)
if offset < 0 {
offset = l + offset + 1
}
offset--
if offset < 0 {
offset = 0
}
mds, err := pm.Find(pattern, unsafeFastStringToReadOnlyBytes(str), offset, 1)
if err != nil {
L.RaiseError(err.Error())
}
if len(mds) == 0 {
L.Push(LNil)
return 0
}
md := mds[0]
nsubs := md.CaptureLength() / 2
switch nsubs {
case 1:
L.Push(LString(str[md.Capture(0):md.Capture(1)]))
return 1
default:
for i := 2; i < md.CaptureLength(); i += 2 {
if md.IsPosCapture(i) {
L.Push(LNumber(md.Capture(i)))
} else {
L.Push(LString(str[md.Capture(i):md.Capture(i+1)]))
}
}
return nsubs - 1
}
}
func strRep(L *LState) int {
str := L.CheckString(1)
n := L.CheckInt(2)
if n < 0 {
L.Push(emptyLString)
} else {
L.Push(LString(strings.Repeat(str, n)))
}
return 1
}
func strReverse(L *LState) int {
str := L.CheckString(1)
bts := []byte(str)
out := make([]byte, len(bts))
for i, j := 0, len(bts)-1; j >= 0; i, j = i+1, j-1 {
out[i] = bts[j]
}
L.Push(LString(string(out)))
return 1
}
func strSub(L *LState) int {
str := L.CheckString(1)
start := luaIndex2StringIndex(str, L.CheckInt(2), true)
end := luaIndex2StringIndex(str, L.OptInt(3, -1), false)
l := len(str)
if start >= l || end < start {
L.Push(emptyLString)
} else {
L.Push(LString(str[start:end]))
}
return 1
}
func strUpper(L *LState) int {
str := L.CheckString(1)
L.Push(LString(strings.ToUpper(str)))
return 1
}
func luaIndex2StringIndex(str string, i int, start bool) int {
if start && i != 0 {
i -= 1
}
l := len(str)
if i < 0 {
i = l + i + 1
}
i = intMax(0, i)
if !start && i > l {
i = l
}
return i
}
//