-
Notifications
You must be signed in to change notification settings - Fork 39
/
api.go
97 lines (88 loc) · 2.14 KB
/
api.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
// Package regexp implements golang package regexp functionality for lua.
package regexp
import (
"regexp"
lua "github.com/yuin/gopher-lua"
)
type luaRegexp struct {
*regexp.Regexp
}
func checkRegexp(L *lua.LState, n int) *luaRegexp {
ud := L.CheckUserData(n)
if v, ok := ud.Value.(*luaRegexp); ok {
return v
}
L.ArgError(n, "regexp_ud expected")
return nil
}
// regexp.compile(string) returns (regexp_ud, error)
func Compile(L *lua.LState) int {
expr := L.CheckString(1)
reg, err := regexp.Compile(expr)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
ud := L.NewUserData()
ud.Value = &luaRegexp{Regexp: reg}
L.SetMetatable(ud, L.GetTypeMetatable(`regexp_ud`))
L.Push(ud)
return 1
}
// regexp_ud:match(string) returns bool
func Match(L *lua.LState) int {
reg := checkRegexp(L, 1)
str := L.CheckString(2)
L.Push(lua.LBool(reg.MatchString(str)))
return 1
}
// regexp.match(regular expression string, string) returns (bool, error)
func SimpleMatch(L *lua.LState) int {
expr := L.CheckString(1)
str := L.CheckString(2)
reg, err := regexp.Compile(expr)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
L.Push(lua.LBool(reg.MatchString(str)))
return 1
}
// regexp_ud:find_all_string_submatch(string) returns table of table of strings
func FindAllStringSubmatch(L *lua.LState) int {
reg := checkRegexp(L, 1)
str := L.CheckString(2)
result := L.NewTable()
for _, t := range reg.FindAllStringSubmatch(str, -1) {
row := L.NewTable()
for _, v := range t {
row.Append(lua.LString(v))
}
result.Append(row)
}
L.Push(result)
return 1
}
// regexp.find_all_string_submatch(regular expression string, string) returns (table of table strings, error)
func SimpleFindAllStringSubmatch(L *lua.LState) int {
expr := L.CheckString(1)
str := L.CheckString(2)
reg, err := regexp.Compile(expr)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
result := L.NewTable()
for _, t := range reg.FindAllStringSubmatch(str, -1) {
row := L.NewTable()
for _, v := range t {
row.Append(lua.LString(v))
}
result.Append(row)
}
L.Push(result)
return 1
}