-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
57 lines (44 loc) · 1.11 KB
/
util.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
package aferosteps
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
func strToPerm(s string) (os.FileMode, error) {
base := 10
if strings.HasPrefix(s, "0") {
base = 8
}
mode, err := strconv.ParseUint(s, base, 32)
if err != nil {
return 0, err
}
return os.FileMode(mode), nil
}
func mustNoError(err error) {
if err != nil {
panic(err)
}
}
func fileContentRegexp(s string) string {
pattern := regexp.MustCompile(`<regexp:[^/]+/>`)
matches := pattern.FindAllString(s, -1)
cnt := len(matches)
if cnt == 0 {
return regexp.QuoteMeta(s)
}
replacementsBefore := make([]string, 0, cnt*2)
replacementAfter := make([]string, 0, cnt*2)
for i, match := range matches {
token := fmt.Sprintf("<regexp%d/>", i)
replacementsBefore = append(replacementsBefore, match, token)
replacementAfter = append(replacementAfter, token, strings.TrimPrefix(strings.TrimSuffix(match, "/>"), "<regexp:"))
}
replaceBefore := strings.NewReplacer(replacementsBefore...)
replaceAfter := strings.NewReplacer(replacementAfter...)
s = replaceBefore.Replace(s)
s = regexp.QuoteMeta(s)
return replaceAfter.Replace(s)
}