-
Notifications
You must be signed in to change notification settings - Fork 3
/
bexp.go
386 lines (339 loc) · 9.02 KB
/
bexp.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2020 The Happy Authors
// Package bexp implements Brace Expansion mechanism to generate arbitrary strings.
// Patterns to be brace expanded take the form of an optional preamble,
// followed by either a series of comma-separated strings or a sequence
// expression between a pair of braces, followed by an optional postscript.
// The preamble is prefixed to each string contained within the braces, and the
// postscript is then appended to each resulting string, expanding left to right.
//
// Brace expansions may be nested. The results of each expanded string are not
// sorted; left to right order is preserved. For example,
//
// Parse("a{d,c,b}e")
// []string{"ade", "ace", "abe"}
//
// Any incorrectly formed brace expansion is left unchanged.
//
// More info about Bash Brace Expansion can be found at
// https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html
package bexp
import (
"errors"
"math"
"os"
"regexp"
"strconv"
"strings"
"unicode"
)
var (
// ErrEmptyResult representing empty result by parser.
ErrEmptyResult = errors.New("result is empty")
// ErrUnchangedBraceExpansion for any incorrectly formed brace expansion
// where input string is left unchanged.
ErrUnchangedBraceExpansion = errors.New("brace expansion left unchanged")
)
const (
// token version.
tv = "\u0000v3"
escSlash = tv + "SLASH\u0000"
escOpen = tv + "OPEN\u0000"
escClose = tv + "CLOSE\u0000"
escComma = tv + "COMMA\u0000"
escPeriod = tv + "PERIOD\u0000"
)
// BraceExpansion represents bash style brace expansion.
type BraceExpansion []string
// Parse string expresion into BraceExpansion result.
func Parse(expr string) []string {
if expr == "" {
return []string{""}
}
// escape a leading {} for case {},a}b / a{},b}c
if strings.HasPrefix(expr, "{}") {
expr = "\\{\\}" + expr[2:]
}
res := mapArray(expand(escapeBraces(expr), true), unescapeBraces)
if len(res) == 0 && len(expr) > 0 {
return []string{""}
}
return res
}
// ParseValid is for convienience to get errors on input:
// 1. ErrEmptyResult when provided string is empty
// When working with Brace Expansions this method is for convinience to handle
// only empty string as errors in your program.
// Note that even then it is actually not invalid. As Brace Expansion docs say:
// "Any incorrectly formed brace expansion is left unchanged.".
//
// 2. ErrUnchangedBraceExpansion when provided string was left unchanged
// Result will always be `BraceExpansion` with min len 1 to satisfy
// "Any incorrectly formed brace expansion is left unchanged.".
func ParseValid(expr string) (res []string, err error) {
res = Parse(expr)
if len(res) == 1 && res[0] == "" {
return res, ErrEmptyResult
}
if len(res) == 1 && res[0] == expr {
return res, ErrUnchangedBraceExpansion
}
return res, err
}
// MkdirAll calls os.MkdirAll on each math from provided string
// to create a directory tree from brace expansion.
// Error can be ErrEmptyResult if parsing provided str results no paths
// or first error of os.MkdirAll.
func MkdirAll(expr string, perm os.FileMode) error {
if p, err := ParseValid(expr); err == nil || errors.Is(err, ErrUnchangedBraceExpansion) {
for _, dir := range p {
if err := os.MkdirAll(dir, perm); err != nil {
return err
}
}
}
return nil
}
// String calls strings.Join(b, " ") and returns resulting string.
func (b BraceExpansion) String() string {
return strings.Join(b, " ")
}
// Result is convience to get result as string slice.
func (b BraceExpansion) Result() []string {
return b
}
func parseCommaParts(str string) []string {
if str == "" {
return []string{""}
}
parts := []string{}
m := Balanced("{", "}", str)
if !m.Valid {
return strings.Split(str, ",")
}
pre := m.Pre
body := m.Body
post := m.Post
p := strings.Split(pre, ",")
p[len(p)-1] += "{" + body + "}"
postParts := parseCommaParts(post)
if len(post) > 0 {
p[len(p)-1] += postParts[0]
p = append(p, postParts[1:]...)
}
parts = append(parts, p...)
return parts
}
var (
numericSequenceRegex = regexp.MustCompile(`^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$`)
alphaSequenceRegex = regexp.MustCompile(`^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$`)
commaInBracesRegex = regexp.MustCompile(`,.*\}`)
)
func expand(str string, isTop bool) []string {
m := Balanced("{", "}", str)
if !m.Valid {
return []string{str}
}
var post []string
if len(m.Post) > 0 {
post = expand(m.Post, false)
} else {
post = []string{""}
}
if strings.HasSuffix(m.Pre, "$") {
var expansions []string
for _, p := range post {
expansions = append(expansions, m.Pre+"{"+m.Body+"}"+p)
}
return expansions
}
isNumericSequence := numericSequenceRegex.MatchString(m.Body)
isAlphaSequence := alphaSequenceRegex.MatchString(m.Body)
isSequence := isNumericSequence || isAlphaSequence
isOptions := strings.Contains(m.Body, ",")
if !isSequence && !isOptions {
if commaInBracesRegex.MatchString(m.Post) {
return expand(m.Pre+"{"+m.Body+escClose+m.Post, false)
}
return []string{str}
}
var n, n2 []string
if isSequence {
n = strings.Split(m.Body, "..")
if len(n) == 3 {
n[2] = strings.TrimLeft(n[2], "0")
}
n2 = expandSequence(n, isAlphaSequence)
} else {
n = parseCommaParts(m.Body)
if len(n) == 1 {
n = mapArray(expand(n[0], false), embrace)
}
n2 = concatMap(n, func(el string) []string { return expand(el, false) })
}
return expandToExpansionSlice(n2, post, m.Pre, isTop, isSequence)
}
func expandSequence(n []string, isAlphaSequence bool) []string {
x := numeric(n[0]) //nolint: ifshort
y := numeric(n[1]) //nolint: ifshort
width := max(len(n[0]), len(n[1]))
var incr int64
if len(n) == 3 {
incr = int64(math.Abs(float64(numeric(n[2]))))
} else {
incr = 1
}
test := lte
// reverse
if y < x {
incr *= -1
test = gte
}
pad := some(n, isPadded)
n2 := []string{}
for i := x; test(i, y); i += incr {
var c string
if isAlphaSequence {
c = string(rune(i))
if c == "\\" {
c = ""
}
} else {
c = expandNonAlphaSequence(i, width, pad)
}
n2 = append(n2, c)
}
return n2
}
func expandNonAlphaSequence(i int64, width int, pad bool) string {
c := strconv.FormatInt(i, 10) //nolint: gomnd
if pad {
var need = width - len(c)
if need > 0 {
var z = strings.Join(make([]string, need+1), "0")
if i < 0 {
c = "-" + z + c[1:]
} else {
c = z + c
}
}
}
return c
}
func expandToExpansionSlice(n2, post []string, pre string, isTop, isSequence bool) []string {
expansions := []string{}
for j := 0; j < len(n2); j++ {
for k := 0; k < len(post); k++ {
expansion := pre + n2[j] + post[k]
if !isTop || isSequence || expansion != "" {
expansions = append(expansions, expansion)
}
}
}
return expansions
}
func concatMap(xs []string, fn func(el string) []string) []string {
res := []string{}
for i := 0; i < len(xs); i++ {
var x = fn(xs[i])
res = append(res, x...)
}
return res
}
func some(arr []string, fn func(el string) bool) bool {
for _, v := range arr {
if fn(v) {
return true
}
}
return false
}
func mapArray(arr []string, call func(str string) string) []string {
ret := []string{}
for _, v := range arr {
ret = append(ret, call(v))
}
return ret
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
// isPadded reports whether string starts zero left padded number.
// e.g (-?) 00, 01, 0001, -01 etc.
// Current implementation is dropin replacement to
// regexp.MustCompile(`^-?0\d`).Match([]byte(el)) and is about 15x faster.
func isPadded(el string) bool {
if len(el) < 2 {
return false
}
if !strings.HasPrefix(el, "0") && !strings.HasPrefix(el, "-0") {
return false
}
test := strings.TrimLeft(el, "-")[1:]
if test == "" {
return false
}
return unicode.IsNumber(rune(test[0]))
}
func embrace(str string) string {
return "{" + str + "}"
}
func lte(i, y int64) bool {
return i <= y
}
func gte(i, y int64) bool {
return i >= y
}
func numeric(str string) int64 {
v, err := strconv.Atoi(str)
if err != nil {
return int64(str[0])
}
return int64(v)
}
// escapeBraces is cheaper strings.NewReplacer to escape braces
//
// var escapeBraces = strings.NewReplacer(
//
// "\\\\", escSlash,
// "\\{", escOpen,
// "\\}", escClose,
// "\\,", escComma,
// "\\.", escPeriod,
//
// )
//
// escapeBraces.Replace(str).
func escapeBraces(str string) string {
return sliceAndJoin(
sliceAndJoin(
sliceAndJoin(
sliceAndJoin(
sliceAndJoin(str, escSlash, "\\\\"), escOpen, "\\{"), escClose, "\\}"), escComma, "\\,"), escPeriod, "\\.")
}
// unescapeBraces is cheaper strings.NewReplacer to escape braces
// var unescapeBraces = strings.NewReplacer(
//
// escSlash, "",
// escOpen, "{",
// escClose, "}",
// escComma, ",",
// escPeriod, ".",
//
// ).
func unescapeBraces(str string) string {
return sliceAndJoin(
sliceAndJoin(
sliceAndJoin(
sliceAndJoin(
sliceAndJoin(str, "\\", escSlash), "{", escOpen), "}", escClose), ",", escComma), ".", escPeriod)
}
// sliceAndJoin replaces separators
// return strings.Join(strings.Split(str, slice), join).
func sliceAndJoin(str, join, slice string) string {
return strings.ReplaceAll(str, slice, join)
}