-
Notifications
You must be signed in to change notification settings - Fork 342
/
strings.go
72 lines (59 loc) · 1.48 KB
/
strings.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
package strings
import (
"strings"
"github.com/Shopify/go-lua"
"github.com/treeverse/lakefs/pkg/actions/lua/util"
)
func Open(l *lua.State) {
strOpen := func(l *lua.State) int {
lua.NewLibrary(l, stringLibrary)
return 1
}
lua.Require(l, "strings", strOpen, false)
l.Pop(1)
}
var stringLibrary = []lua.RegistryFunction{
{Name: "split", Function: split},
{Name: "trim", Function: trim},
{Name: "replace", Function: replace},
{Name: "has_prefix", Function: hasPrefix},
{Name: "has_suffix", Function: hasSuffix},
{Name: "contains", Function: contains},
}
func split(l *lua.State) int {
str := lua.CheckString(l, 1)
sep := lua.CheckString(l, 2)
strArr := strings.Split(str, sep)
return util.DeepPush(l, strArr)
}
func trim(l *lua.State) int {
str := lua.CheckString(l, 1)
l.PushString(strings.TrimSpace(str))
return 1
}
func replace(l *lua.State) int {
s := lua.CheckString(l, 1)
old := lua.CheckString(l, 2)
new := lua.CheckString(l, 3)
n := lua.CheckInteger(l, 4)
l.PushString(strings.Replace(s, old, new, n))
return 1
}
func hasPrefix(l *lua.State) int {
s := lua.CheckString(l, 1)
prefix := lua.CheckString(l, 2)
l.PushBoolean(strings.HasPrefix(s, prefix))
return 1
}
func hasSuffix(l *lua.State) int {
s := lua.CheckString(l, 1)
suffix := lua.CheckString(l, 2)
l.PushBoolean(strings.HasSuffix(s, suffix))
return 1
}
func contains(l *lua.State) int {
s := lua.CheckString(l, 1)
substr := lua.CheckString(l, 2)
l.PushBoolean(strings.Contains(s, substr))
return 1
}