-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathstrings.go
180 lines (143 loc) · 3.83 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package exp
import "strings"
// Match
type expMatch struct {
key, str string
}
func (e expMatch) Eval(p Params) bool {
return p.Get(e.key) == e.str
}
func (e expMatch) String() string {
return sprintf("[%s==%s]", e.key, e.str)
}
// Match is an expression that evaluates to true if str is equal to the value
// pointed to by key.
//
// m := Map{"foo": "bar"}
// Match("foo", "bar").Eval(m) // true
// Match("foo", "baz").Eval(m) // false
func Match(key, str string) Exp {
return expMatch{key, str}
}
// MatchAny is an expression that evaluates to true if any of the strs are equal
// to the value pointed to by key.
//
// m := Map{"foo": "bar"}
// MatchAny("foo", "bar", "baz", "cux").Eval(m) // true
// MatchAny("foo", "baf", "baz", "lux").Eval(m) // false
func MatchAny(key string, strs ...string) Exp {
exp := make([]Exp, len(strs))
for i, str := range strs {
exp[i] = Match(key, str)
}
return Or(exp...)
}
// Contains
type expContains struct {
key, substr string
}
func (e expContains) Eval(p Params) bool {
return strings.Contains(p.Get(e.key), e.substr)
}
func (e expContains) String() string {
return sprintf("[%s∋%s]", e.key, e.substr)
}
// Contains is an expression that evaluates to true if substr is within the
// value pointed to by key.
func Contains(key, substr string) Exp {
return expContains{key, substr}
}
// ContainsAny
type expContainsAny struct {
key, chars string
}
func (e expContainsAny) Eval(p Params) bool {
return strings.ContainsAny(p.Get(e.key), e.chars)
}
func (e expContainsAny) String() string {
return sprintf("[%s∋%s]", e.key, e.chars)
}
// ContainsAny evaluates to true if any Unicode code points in chars are within
// the value pointed to by key.
func ContainsAny(key, chars string) Exp {
return expContainsAny{key, chars}
}
// ContainsRune
type expContainsRune struct {
key string
r rune
}
func (e expContainsRune) Eval(p Params) bool {
return strings.ContainsRune(p.Get(e.key), e.r)
}
func (e expContainsRune) String() string {
return sprintf("[%s∋%c]", e.key, e.r)
}
// ContainsRune evaluates to true if the Unicode code point r is within the
// value pointed to by key.
func ContainsRune(key string, r rune) Exp {
return expContainsRune{key, r}
}
// Len
type expLen struct {
key string
length int
}
func (e expLen) Eval(p Params) bool {
return len(p.Get(e.key)) == e.length
}
func (e expLen) String() string {
return sprintf("[len(%s)==%d]", e.key, e.length)
}
// Len evalates to true if the length of the string pointed to by key is equal
// to length.
func Len(key string, length int) Exp {
return expLen{key, length}
}
// Count
type expCount struct {
key, sep string
count int
}
func (e expCount) Eval(p Params) bool {
return strings.Count(p.Get(e.key), e.sep) == e.count
}
func (e expCount) String() string {
return sprintf("[count(%s,%s)==%d]", e.key, e.sep, e.count)
}
// Count evaluates to true if the number of non-overlapping instances of sep in
// the value pointed to by key is equal to count.
//
// m := Map{
// "foo": "bar",
// "bar": "zoo"
// }
// Count("foo", "b", 1).Eval(m) // true
// Count("foo", "a", 1).Eval(m) // true
// Count("foo", "r", 1).Eval(m) // true
// Count("bar", "o", 2).Eval(m) // true
func Count(key, sep string, count int) Exp {
return expCount{key, sep, count}
}
// EqualFold
type expEqualFold struct {
key, s string
}
func (e expEqualFold) Eval(p Params) bool {
return strings.EqualFold(p.Get(e.key), e.s)
}
func (e expEqualFold) String() string {
return sprintf("[%s≈%s]", e.key, e.s)
}
// EqualFold evaluates to true if the value pointed to by key and s, interpreted
// as UTF-8 strings, are equal under Unicode case-folding.
//
// m := Map{
// "foo": "Bar",
// "bar": "BaZ"
// }
// EqualFold("foo", "bar").Eval(m) // true
// EqualFold("bar", "baz").Eval(m) // true
func EqualFold(key, s string) Exp {
return expEqualFold{key, s}
}