-
Notifications
You must be signed in to change notification settings - Fork 29
/
search.go
60 lines (49 loc) · 975 Bytes
/
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
package main
import (
"regexp"
"strings"
)
func parseSearchQuery(targets []string) (words []string, pattern string) {
var (
search bool
query []string
)
for _, target := range targets {
if strings.HasPrefix(target, "/") {
query = append(
query,
strings.TrimPrefix(target, "/"),
)
search = true
continue
}
if search {
query = append(query, target)
} else {
words = append(words, target)
}
}
return words, getSearchPattern(query)
}
func getSearchPattern(query []string) string {
letters := strings.Split(
strings.Replace(
strings.Join(query, ""),
" ", "", -1,
),
"",
)
for i, letter := range letters {
letters[i] = regexp.QuoteMeta(letter)
}
pattern := strings.Join(letters, ".*")
return pattern
}
func matchPattern(pattern, target string) bool {
match, err := regexp.MatchString(strings.ToLower(pattern),
strings.ToLower(target))
if err != nil {
debugf("Error: %+v", err)
}
return match
}