-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcac.go
67 lines (57 loc) · 1.4 KB
/
cac.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
package cac
import (
"strings"
)
// Parse parses the file content then turn to string slice.
func Parse(fileContent string) []string {
list := strings.Split(fileContent, "\n")
// Remove commands
var listWihoutCommands []string
for i := range list {
trimedSpace := strings.TrimSpace(list[i])
if strings.HasPrefix(trimedSpace, "#") {
continue
}
listWihoutCommands = append(listWihoutCommands, trimedSpace)
}
clear(list)
list = nil
pureList := strings.Fields(strings.Join(listWihoutCommands, " "))
return mergeLine(pureList)
}
// mergeLine merges near line if the first line ends with backslash.
func mergeLine(list []string) (mergedList []string) {
length := len(list)
for i := 0; i < length; i++ {
if endWithValidBackslash(list[i]) {
var needMergeCount int
for j := i + 1; j < length; j++ {
if !endWithValidBackslash(list[j]) {
break
}
needMergeCount++
}
mergedList = append(mergedList, list[i:i+needMergeCount]...)
i += needMergeCount
continue
}
mergedList = append(mergedList, list[i])
}
return
}
// endWithValidBackslash used to check wheaher the suffix is valid backslash.
func endWithValidBackslash(str string) bool {
list := strings.Split(str, "")
length := len(list)
if length == 0 {
return false
}
var backslashCount int
for i := length - 1; i >= 0; i-- {
if list[i] != "\\" {
break
}
backslashCount++
}
return backslashCount%2 != 0
}