-
Notifications
You must be signed in to change notification settings - Fork 142
/
search.go
75 lines (62 loc) · 1.37 KB
/
search.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
package the_platinum_searcher
import (
"io"
"regexp"
)
type search struct {
roots []string
out io.Writer
}
func (s search) start(pattern string) error {
grepChan := make(chan string, 5000)
done := make(chan struct{})
if opts.SearchOption.WordRegexp {
opts.SearchOption.Regexp = true
pattern = "\\b" + pattern + "\\b"
}
if opts.SearchOption.SmartCase {
if !regexp.MustCompile(`[[:upper:]]`).MatchString(pattern) {
opts.SearchOption.IgnoreCase = true
}
}
if opts.SearchOption.IgnoreCase {
opts.SearchOption.Regexp = true
}
p, err := newPattern(pattern, opts)
if err != nil {
return err
}
if opts.OutputOption.Context > 0 {
opts.OutputOption.Before = opts.OutputOption.Context
opts.OutputOption.After = opts.OutputOption.Context
}
var regFile *regexp.Regexp
if opts.SearchOption.FileSearchRegexp != "" {
regFile, err = regexp.Compile(opts.SearchOption.FileSearchRegexp)
if err != nil {
return err
}
}
if opts.SearchOption.EnableFilesWithRegexp {
opts.OutputOption.FilesWithMatches = true
if opts.SearchOption.PatternFilesWithRegexp != "" {
regFile, err = regexp.Compile(opts.SearchOption.PatternFilesWithRegexp)
if err != nil {
return err
}
}
}
go find{
out: grepChan,
opts: opts,
}.start(s.roots, regFile)
go newGrep(
p,
grepChan,
done,
opts,
newPrinter(p, s.out, opts),
).start()
<-done
return nil
}