-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
93 lines (84 loc) · 1.78 KB
/
find.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
package main
import (
"strings"
"sync"
)
type findOptions struct {
ignoreCase bool
removed bool
}
type findResult struct {
file *diffFile
occurrences []*lineOccurrence
}
func newFindResult() findResult {
return findResult{
occurrences: make([]*lineOccurrence, 0, 10),
}
}
type lineOccurrence struct {
line *diffLine
occurrences map[string][]uint
}
func newLineOccurrences() *lineOccurrence {
return &lineOccurrence{
occurrences: make(map[string][]uint),
}
}
func find(diff *diff, sts []string, fo findOptions) <-chan findResult {
c := make(chan findResult)
wg := sync.WaitGroup{}
wg.Add(len(diff.files))
for _, df := range diff.files {
go func(df *diffFile) {
r := findByKeyword(df, sts, fo)
c <- r
wg.Done()
}(df)
}
go func() {
wg.Wait()
close(c)
}()
return c
}
func findByKeyword(df *diffFile, keywords []string, fo findOptions) findResult {
r := newFindResult()
r.file = df
if fo.ignoreCase {
for i, keyword := range keywords {
keywords[i] = strings.ToLower(keyword)
}
}
for _, dl := range df.lines {
lineContent := dl.content
if fo.ignoreCase {
lineContent = strings.ToLower(lineContent)
}
ko := findKeywordOccurrences(lineContent, keywords)
if len(ko) > 0 {
lo := newLineOccurrences()
lo.line = dl
lo.occurrences = ko
r.occurrences = append(r.occurrences, lo)
}
}
return r
}
func findKeywordOccurrences(s string, ks []string) map[string][]uint {
m := make(map[string][]uint)
for _, k := range ks {
kLen := len(k)
from := 0
for i := strings.Index(s, k); i != -1; i = strings.Index(s[from:], k) {
if indexes, ok := m[k]; ok {
m[k] = append(indexes, uint(i+from))
} else {
indexes := make([]uint, 0, 10)
m[k] = append(indexes, uint(i+from))
}
from += i + kLen
}
}
return m
}