-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.go
287 lines (251 loc) · 10.8 KB
/
options.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
package splitter
import (
"strings"
)
type Option interface {
Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error)
}
var (
TrimSpaces Option = _TrimSpaces // TrimSpaces causes split parts to be trimmed of leading & trailing spaces
Trim = _Trim // Trim causes split parts to be trimmed of the leading & trailing custsets specified
NoEmpties Option = _NoEmpties // NoEmpties causes an error if a split part is empty
NoEmptiesMsg = _NoEmptiesMsg // NoEmptiesMsg is the same as NoEmpties but allows a custom error message
IgnoreEmpties Option = _IgnoreEmpties // IgnoreEmpties causes empty split parts to not be added to the result
NotEmptyFirst Option = _NotEmptyFirst // NotEmptyFirst causes an error if the first split part is empty
NotEmptyFirstMsg = _NotEmptyFirstMsg // NotEmptyFirstMsg is the same as NotEmptyFirst but allows a custom error message
IgnoreEmptyFirst Option = _IgnoreEmptyFirst // IgnoreEmptyFirst causes an empty first split part not to be added to the result
NotEmptyLast Option = _NotEmptyLast // NotEmptyLast causes an error if the last split part is empty
NotEmptyLastMsg = _NotEmptyLastMsg // NotEmptyLastMsg is the same as NotEmptyLast but allows a custom error message
IgnoreEmptyLast Option = _IgnoreEmptyLast // IgnoreEmptyLast causes an empty last split part not to be added to the result
NotEmptyInners Option = _NotEmptyInners // NotEmptyInners causes an error if an inner (i.e. not first or last) split part is empty
NotEmptyInnersMsg = _NotEmptyInnersMsg // NotEmptyInnersMsg is the same as NotEmptyInners but allows a custom error message
IgnoreEmptyInners Option = _IgnoreEmptyInners // IgnoreEmptyInners causes empty inner (i.e. not first or last) split parts not to be added to the result
NotEmptyOuters Option = _NotEmptyOuters // NotEmptyOuters causes an error if an outer (i.e. first or last) split part is empty (same as adding both NotEmptyFirst & NotEmptyLast)
NotEmptyOutersMsg = _NotEmptyOutersMsg // NotEmptyOutersMsg is the same as NotEmptyOuters but allows a custom error message
IgnoreEmptyOuters Option = _IgnoreEmptyOuters // IgnoreEmptyOuters causes empty outer (i.e. first or last) split parts not to be added to the result (same as adding both IgnoreEmptyFirst & IgnoreEmptyLast)
NoContiguousQuotes Option = _NoContiguousQuotes // NoContiguousQuotes causes an error if there are contiguous quotes within a split part
NoContiguousQuotesMsg = _NoContiguousQuotesMsg // NoContiguousQuotesMsg is the same as NoContiguousQuotes but allows a custom error message
NoMultiQuotes Option = _NoMultiQuotes // NoMultiQuotes causes an error if there are multiple (not necessarily contiguous) quotes within a split part
NoMultiQuotesMsg = _NoMultiQuotesMsg // NoMultiQuotesMsg is the same as NoMultiQuotes but allows a custom error message
NoMultis Option = _NoMultis // NoMultis causes an error if there are multiple quotes or brackets in a split part
NoMultisMsg = _NoMultisMsg // NoMultisMsg is the same as NoMultis but allows a custom error message
StripQuotes Option = _StripQuotes // StripQuotes causes quotes within a split part to be stripped
UnescapeQuotes Option = _UnescapeQuotes // UnescapeQuotes causes any quotes within the split part to have any escaped end quotes to be removed
)
var (
_TrimSpaces = &trim{cutset: " "}
_Trim = func(cutset string) Option {
return &trim{cutset: cutset}
}
_NoEmpties = &noEmpties{message: "split items cannot be empty"}
_NoEmptiesMsg = func(message string) Option {
return &noEmpties{message: message}
}
_IgnoreEmpties = &ignoreEmpties{}
_NotEmptyFirst = ¬EmptyFirst{message: "first split item cannot be empty"}
_NotEmptyFirstMsg = func(message string) Option {
return ¬EmptyFirst{message: message}
}
_IgnoreEmptyFirst = &ignoreEmptyFirst{}
_NotEmptyLast = ¬EmptyLast{message: "last split item cannot be empty"}
_NotEmptyLastMsg = func(message string) Option {
return ¬EmptyLast{message: message}
}
_IgnoreEmptyLast = &ignoreEmptyLast{}
_NotEmptyInners = ¬EmptyInners{message: "inner items cannot be empty"}
_NotEmptyInnersMsg = func(message string) Option {
return ¬EmptyInners{message: message}
}
_IgnoreEmptyInners = &ignoreEmptyInners{}
_NotEmptyOuters = ¬EmptyOuters{message: "first/last items cannot be empty"}
_NotEmptyOutersMsg = func(message string) Option {
return ¬EmptyOuters{message: message}
}
_IgnoreEmptyOuters = &ignoreEmptyOuters{}
_NoContiguousQuotes = &noContiguousQuotes{message: "split item cannot have contiguous quotes"}
_NoContiguousQuotesMsg = func(message string) Option {
return &noContiguousQuotes{message: message}
}
_NoMultiQuotes = &noMultiQuotes{message: "split item cannot have multiple quotes"}
_NoMultiQuotesMsg = func(message string) Option {
return &noMultiQuotes{message: message}
}
_NoMultis = &noMultis{message: "split item cannot have multiple parts"}
_NoMultisMsg = func(message string) Option {
return &noMultis{message: message}
}
_StripQuotes = &stripQuotes{}
_UnescapeQuotes = &unescapeQuotes{}
)
type trim struct {
cutset string
}
func (o *trim) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
return strings.Trim(s, o.cutset), true, nil
}
type noEmpties struct {
message string
}
func (o *noEmpties) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if s == "" {
return "", false, NewOptionFailError(o.message, pos, nil)
}
return s, true, nil
}
type ignoreEmpties struct {
}
func (o *ignoreEmpties) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if s == "" {
return "", false, nil
}
return s, true, nil
}
type notEmptyFirst struct {
message string
}
func (o *notEmptyFirst) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if s == "" && captured == 0 && skipped == 0 {
return "", false, NewOptionFailError(o.message, pos, nil)
}
return s, true, nil
}
type ignoreEmptyFirst struct {
}
func (o *ignoreEmptyFirst) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if s == "" && captured == 0 && skipped == 0 {
return "", false, nil
}
return s, true, nil
}
type notEmptyLast struct {
message string
}
func (o *notEmptyLast) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if s == "" && isLast {
return "", false, NewOptionFailError(o.message, pos, nil)
}
return s, true, nil
}
type ignoreEmptyLast struct {
}
func (o *ignoreEmptyLast) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if s == "" && isLast {
return "", false, nil
}
return s, true, nil
}
type notEmptyInners struct {
message string
}
func (o *notEmptyInners) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if s == "" && !isLast && !(captured == 0 && skipped == 0) {
return "", false, NewOptionFailError(o.message, pos, nil)
}
return s, true, nil
}
type ignoreEmptyInners struct {
}
func (o *ignoreEmptyInners) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if s != "" {
return s, true, nil
}
isInner := !isLast && !(captured == 0 && skipped == 0)
return s, !isInner, nil
}
type notEmptyOuters struct {
message string
}
func (o *notEmptyOuters) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if s == "" && (isLast || (captured == 0 && skipped == 0)) {
return "", false, NewOptionFailError(o.message, pos, nil)
}
return s, true, nil
}
type ignoreEmptyOuters struct {
}
func (o *ignoreEmptyOuters) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if s != "" {
return s, true, nil
}
isOuter := isLast || (captured == 0 && skipped == 0)
return s, !isOuter, nil
}
type noContiguousQuotes struct {
message string
}
func (o *noContiguousQuotes) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
for i := 1; i < len(subParts); i++ {
if subParts[i].IsQuote() && subParts[i-1].IsQuote() {
return "", false, NewOptionFailError(o.message, pos, subParts[i])
}
}
return s, true, nil
}
type noMultiQuotes struct {
message string
}
func (o *noMultiQuotes) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
qs := 0
var second SubPart
for _, sp := range subParts {
if sp.IsQuote() {
qs++
if second == nil {
second = sp
}
}
}
if qs > 1 {
return "", false, NewOptionFailError(o.message, pos, second)
}
return s, true, nil
}
type noMultis struct {
message string
}
func (o *noMultis) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if len(subParts) > 1 {
return "", false, NewOptionFailError(o.message, pos, subParts[1])
}
return s, true, nil
}
type stripQuotes struct {
}
func (o *stripQuotes) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if len(subParts) == 1 {
if subParts[0].IsQuote() {
str := subParts[0].String()
return str[1 : len(str)-1], true, nil
}
return s, true, nil
}
var sb strings.Builder
sb.Grow(len(s))
for _, sub := range subParts {
str := sub.String()
if sub.IsQuote() {
str = str[1 : len(str)-1]
}
sb.WriteString(str)
}
return sb.String(), true, nil
}
type unescapeQuotes struct {
}
func (o *unescapeQuotes) Apply(s string, pos int, totalLen int, captured int, skipped int, isLast bool, subParts ...SubPart) (string, bool, error) {
if len(subParts) == 1 && subParts[0].IsQuote() {
return subParts[0].UnEscaped(), true, nil
} else if len(subParts) == 1 {
return s, true, nil
}
var sb strings.Builder
sb.Grow(len(s))
for _, sub := range subParts {
str := sub.String()
if sub.IsQuote() {
str = sub.UnEscaped()
}
sb.WriteString(str)
}
return sb.String(), true, nil
}