-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.go
156 lines (128 loc) · 3.73 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
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
// Package strings contains functions for dealing with strings.
package strings
import (
"strings"
"github.com/DeedleFake/wdte"
"github.com/DeedleFake/wdte/std"
)
// Contains is a WDTE function with the following signatures:
//
// contains outer inner
// (contains inner) outer
//
// Returns true if inner is a substring of outer.
func Contains(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("contains")
switch len(args) {
case 0:
return wdte.GoFunc(Contains)
case 1:
return wdte.GoFunc(func(frame wdte.Frame, next ...wdte.Func) wdte.Func {
return Contains(frame, append(next, args...)...)
})
}
haystack := args[0].Call(frame).(wdte.String)
needle := args[1].Call(frame).(wdte.String)
return wdte.Bool(strings.Contains(string(haystack), string(needle)))
}
// Prefix is a WDTE function with the following signatures:
//
// prefix s p
// (prefix p) s
//
// Returns true if p is a prefix of s.
func Prefix(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("prefix")
switch len(args) {
case 0:
return wdte.GoFunc(Prefix)
case 1:
return wdte.GoFunc(func(frame wdte.Frame, next ...wdte.Func) wdte.Func {
return Prefix(frame, append(next, args...)...)
})
}
haystack := args[0].Call(frame).(wdte.String)
needle := args[1].Call(frame).(wdte.String)
return wdte.Bool(strings.HasPrefix(string(haystack), string(needle)))
}
// Suffix is a WDTE function with the following signatures:
//
// suffix s p
// (suffix p) s
//
// Returns true if p is a suffix of s.
func Suffix(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("suffix")
switch len(args) {
case 0:
return wdte.GoFunc(Suffix)
case 1:
return wdte.GoFunc(func(frame wdte.Frame, next ...wdte.Func) wdte.Func {
return Suffix(frame, append(next, args...)...)
})
}
haystack := args[0].Call(frame).(wdte.String)
needle := args[1].Call(frame).(wdte.String)
return wdte.Bool(strings.HasSuffix(string(haystack), string(needle)))
}
// Index is a WDTE function with the following signatures:
//
// index outer inner
// (index inner) outer
//
// It returns the index of the first character of the first instances
// of inner in outer. If inner is not a substring of outer, it returns
// -1.
func Index(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("index")
switch len(args) {
case 0:
return wdte.GoFunc(Index)
case 1:
return wdte.GoFunc(func(frame wdte.Frame, next ...wdte.Func) wdte.Func {
return Index(frame, append(next, args...)...)
})
}
haystack := args[0].Call(frame).(wdte.String)
needle := args[1].Call(frame).(wdte.String)
return wdte.Number(strings.Index(string(haystack), string(needle)))
}
// Upper is a WDTE function with the following signatures:
//
// upper s
//
// It returns s converted to uppercase.
func Upper(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("upper")
switch len(args) {
case 0:
return wdte.GoFunc(Upper)
}
return wdte.String(strings.ToUpper(string(args[0].Call(frame).(wdte.String))))
}
// Lower is a WDTE function with the following signature:
//
// lower s
//
// It returns s converted to lowercase.
func Lower(frame wdte.Frame, args ...wdte.Func) wdte.Func {
frame = frame.Sub("lower")
switch len(args) {
case 0:
return wdte.GoFunc(Lower)
}
return wdte.String(strings.ToLower(string(args[0].Call(frame).(wdte.String))))
}
// Scope is a scope containing the functions in this package.
var Scope = wdte.S().Map(map[wdte.ID]wdte.Func{
"contains": wdte.GoFunc(Contains),
"prefix": wdte.GoFunc(Prefix),
"suffix": wdte.GoFunc(Suffix),
"index": wdte.GoFunc(Index),
"upper": wdte.GoFunc(Upper),
"lower": wdte.GoFunc(Lower),
"format": wdte.GoFunc(Format),
})
func init() {
std.Register("strings", Scope)
}