-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp.go
69 lines (62 loc) · 1.61 KB
/
regexp.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
package main
import (
"bufio"
"log"
"os"
"regexp"
"strings"
)
type RegCat struct {
Regexp *regexp.Regexp
Category string
}
var regexps struct {
Filename []RegCat //1
Content []RegCat //2
IgnoreContent []RegCat //3
IgnoreFilename []RegCat //4
}
func loadRegexps(filename string) {
fileHandle, err := os.Open(filename)
testErrDie("Unable to open regexp file", err)
scanner := bufio.NewScanner(fileHandle)
section := 0
category := "none"
lineNumber := 0
for scanner.Scan() {
lineNumber++
line := scanner.Text()
if line == "" || strings.HasPrefix(line, "#") {
continue
}
if strings.HasPrefix(line, ";section") {
category = "none"
if strings.HasSuffix(line, "ignore-filename") {
section = 4
} else if strings.HasSuffix(line, "ignore-content") {
section = 3
} else if strings.HasSuffix(line, "content") {
section = 2
} else if strings.HasSuffix(line, "filename") {
section = 1
} else {
log.Fatalf("Error in regexp file at line: %d\n", lineNumber)
}
} else if strings.HasPrefix(line, ";category") {
v := strings.SplitN(line, " ", 2)
category = v[1]
} else {
reg := regexp.MustCompile(line)
switch section {
case 1:
regexps.Filename = append(regexps.Filename, RegCat{Regexp: reg, Category: category})
case 2:
regexps.Content = append(regexps.Content, RegCat{Regexp: reg, Category: category})
case 3:
regexps.IgnoreContent = append(regexps.IgnoreContent, RegCat{Regexp: reg, Category: category})
case 4:
regexps.IgnoreFilename = append(regexps.IgnoreFilename, RegCat{Regexp: reg, Category: category})
}
}
}
}