forked from monochromegane/the_platinum_searcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
79 lines (66 loc) · 1.49 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
76
77
78
79
package mack
import (
"io"
"regexp"
)
type search struct {
roots []string
out io.Writer
}
func (s search) start(pattern string) error {
searchChan := 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
}
if opts.OutputOption.Context > 0 {
opts.OutputOption.Before = opts.OutputOption.Context
opts.OutputOption.After = opts.OutputOption.Context
}
var regFile *regexp.Regexp
var err error
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: searchChan,
opts: opts,
}.start(s.roots, regFile)
// original
// constructs a "pattern" with a regular expression for matching
p, err := newPattern(pattern, opts)
if err != nil {
return err
}
// performs the match, with pattern p
go newGrep(
p,
searchChan,
done,
opts,
newPrinter(p, s.out, opts),
).start()
<-done
return nil
}