-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
135 lines (108 loc) · 3.5 KB
/
helpers.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
package ini
import (
"fmt"
"log"
"regexp"
"strings"
"github.com/shellucas/go-ini/config"
"github.com/shellucas/go-ini/enums/subsection"
"github.com/shellucas/go-ini/feature"
"github.com/shellucas/go-ini/utils"
)
func isSection(line string) bool {
line = strings.TrimSpace(line)
return strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]")
}
func isProperty(line string, config config.Config) bool {
keyRegex := "([a-zA-Z]+[a-zA-Z0-9]*\\s*)"
valueRegex := "(.+)"
regex := regexp.MustCompile(fmt.Sprintf("^%s%s%s$", keyRegex, config.GetSeperatorRegex(), valueRegex))
return regex.MatchString(strings.TrimSpace(line))
}
func createSection(line string, parent *feature.Section, subType subsection.SubSectionType) *feature.Section {
prefix := getFeaturePrefix(line, subType)
section := feature.Section{
Name: strings.TrimSuffix(strings.TrimPrefix(line[len(prefix)+1:], "["), "]"),
Prefix: prefix,
}
if parent != nil {
parent.AddSection(§ion)
}
return §ion
}
func getProperty(line string, config config.Config) (feature.Property, error) {
var property feature.Property
if !isProperty(line, config) {
return property, fmt.Errorf("\"%s\" is not a property", line)
}
// Handle the split
split, err := utils.RegSplitFirst(line, config.GetSeperatorRegex())
if err != nil {
return property, err
} else if len(split) <= 2 {
return property, fmt.Errorf("\"%s\" is not a valid property", line)
}
seperator := split[1]
split[1] = strings.Join(split[2:], " ")
split = split[:2]
trimmedValue := strings.TrimSpace(split[1])
quotesRegex := config.GetQuotesRegex()
commentRegex, err := utils.RegSplitFirst(trimmedValue, fmt.Sprintf("(%s?.+%s?)(%s)", quotesRegex, quotesRegex, config.GetCommentsRegex()))
key := strings.TrimSpace(split[0])
value := trimmedValue
if commentRegex != nil {
property.SetComment(commentRegex[1][len(commentRegex[1])-1:], strings.TrimSpace(commentRegex[2]))
value = strings.TrimSpace(commentRegex[1][:len(commentRegex[1])-1])
}
value = removeQuotes(value, config)
property = feature.CreateProperty(key, value, seperator, config)
return property, nil
}
func removeQuotes(value string, config config.Config) string {
var noQuotes string
for _, char := range config.GetQuotesChars() {
if strings.HasPrefix(value, char) && strings.HasSuffix(value, char) {
noQuotes = strings.TrimSuffix(strings.TrimPrefix(value, char), char)
break
}
}
if noQuotes == "" {
noQuotes = value
}
return noQuotes
}
func getFeaturePrefix(line string, subType subsection.SubSectionType) string {
switch subType {
case subsection.Indented:
return utils.Match(line, `^[\s]+`)
case subsection.Seperated:
return utils.Match(line, `[^\[].*\.`)
}
log.Fatal("No subsection config specified")
return ""
}
func checkSectionLineDepth(line *string, currentSection *feature.Section, subType subsection.SubSectionType) int {
prefixLen := getPrefixLen(getFeaturePrefix(*line, subType), subType)
currentSectionPrefixLen := getPrefixLen(getFeaturePrefix(currentSection.Prefix, subType), subType)
if prefixLen > currentSectionPrefixLen {
return 1
}
if prefixLen == currentSectionPrefixLen {
return 0
}
if prefixLen < currentSectionPrefixLen {
return -1
}
log.Fatal("No subsection config specified")
return 0
}
func getPrefixLen(prefix string, subType subsection.SubSectionType) int {
switch subType {
case subsection.Indented:
return len(prefix)
case subsection.Seperated:
regex := regexp.MustCompile(`\.`)
return len(regex.FindAllStringIndex(prefix, -1))
}
return 0
}