-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregex.go
53 lines (45 loc) · 1.03 KB
/
regex.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
package main
import (
"bytes"
"fmt"
"io"
"regexp"
"strings"
)
var patterns []string
var expressions []*regexp.Regexp
// Checks against each regex in the patterns array
// and returns true if any of them matched
func dirty(lyrics, song, artist, site string) bool {
for _, r := range expressions {
match := r.FindString(lyrics)
if match != "" {
dirtySong.Printf("Bad word: %s matches regexp: %s in song: %s by artist: %s from site: %s", match, r.String(), song, artist, site)
return true
}
}
return false
}
// Create proper regex type
func loadPatterns(r io.Reader) error {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r)
if err != nil {
return err
}
patterns = strings.Split(buf.String(), "\n")
expressions = make([]*regexp.Regexp, 0, len(patterns))
var temp *regexp.Regexp
for _, p := range patterns {
if p == "" {
continue
}
temp, err = regexp.Compile(p)
if err != nil {
fmt.Printf("%s pattern failed to compile\n", p)
continue
}
expressions = append(expressions, temp)
}
return nil
}