-
Notifications
You must be signed in to change notification settings - Fork 0
/
line-counter.go
58 lines (46 loc) · 1.19 KB
/
line-counter.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
package main
type countByRule map[string]int
type countByProfile map[string]countByRule
type lineCounter struct {
filename string
count countByProfile
tooLongLine bool
}
func newLineCounter(filename string) *lineCounter {
return &lineCounter{
filename: filename,
count: make(countByProfile),
}
}
func (l *lineCounter) countLine(line string, profiles profiles) {
for profName, p := range profiles {
if p.checkPath(l.filename) {
if _, ok := l.count[profName]; !ok {
l.count[profName] = make(countByRule)
}
for ruleName, r := range p.Rules {
if _, ok := l.count[profName][ruleName]; !ok {
l.count[profName][ruleName] = 0
}
if r.checkPath(l.filename) && r.checkLine(line) {
l.count[profName][ruleName]++
}
}
}
}
}
type lineCounters []lineCounter
func (l lineCounters) totalCount() countByProfile {
count := make(countByProfile, len(l))
for _, lc := range l {
for profileName, profileCount := range lc.count {
if _, ok := count[profileName]; !ok {
count[profileName] = make(countByRule, len(profileCount))
}
for ruleName, ruleCount := range profileCount {
count[profileName][ruleName] += ruleCount
}
}
}
return count
}