-
Notifications
You must be signed in to change notification settings - Fork 1
/
like.go
64 lines (58 loc) · 1.21 KB
/
like.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
package like
import (
"fmt"
"regexp"
"strings"
"unicode/utf8"
)
func Compile(expr string) (*like, error) {
switch {
case len(expr) == 0:
return &like{fn: func(s string) bool { return s == "" }}, nil
case len(expr) == 1 && expr[0] == '%':
return &like{fn: func(s string) bool { return true }}, nil
case len(expr) == 1 && expr[0] == '_':
return &like{fn: func(s string) bool { return len(s) != 0 }}, nil
}
reg, err := regexp.Compile(convert(expr))
if err != nil {
return nil, err
}
return &like{reg: reg}, nil
}
func (l *like) MatchString(s string) bool {
if l.fn != nil {
return l.fn(s)
}
return l.reg.MatchString(s)
}
func convert(expr string) string {
return fmt.Sprintf("^(?s:%s)$", replace(expr))
}
func replace(s string) string {
var oc rune
r := make([]byte, len(s)+strings.Count(s, `%`))
w := 0
start := 0
for len(s) > start {
c, wid := utf8.DecodeRuneInString(s[start:])
if oc == '\\' {
w += copy(r[w:], s[start:start+wid])
start += wid
oc = 0
continue
}
switch c {
case '_':
w += copy(r[w:], []byte{'*'})
case '%':
w += copy(r[w:], []byte{'.', '*'})
case '\\':
default:
w += copy(r[w:], s[start:start+wid])
}
start += wid
oc = c
}
return string(r)
}