-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdata.go
77 lines (69 loc) · 1.93 KB
/
data.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
package sre2
import (
"unicode"
)
// RuneFilter is a unique method signature for matching true/false over a given
// unicode rune.
type RuneFilter func(r rune) bool
// Generate a RuneFilter matching a single rune.
func matchRune(to_match rune) RuneFilter {
return func(r rune) bool {
return r == to_match
}
}
// Generate a RuneFilter matching a range of runes, assumes from <= to.
func matchRuneRange(from rune, to rune) RuneFilter {
return func(r rune) bool {
return r >= from && r <= to
}
}
// Generate a RuneFilter matching a valid Unicode class. If no matching classes
// are found, then this method will return nil.
// Note that if just a single character is given, Categories will be searched
// for this as a prefix (so that 'N' will match 'Nd', 'Nl', 'No' etc).
func matchUnicodeClass(class string) RuneFilter {
found := false
match := make([]*unicode.RangeTable, 0)
if len(class) == 1 {
// A single character is a shorthand request for any category starting with this.
for key, r := range unicode.Categories {
if key[0] == class[0] {
found = true
match = append(match, r)
}
}
} else {
// Search for the unicode class name inside cats/props/scripts.
options := []map[string]*unicode.RangeTable{
unicode.Categories, unicode.Properties, unicode.Scripts}
for _, option := range options {
if r, ok := option[class]; ok {
found = true
match = append(match, r)
}
}
}
if found {
return func(r rune) bool {
for _, table := range match {
if unicode.Is(table, r) {
return true
}
}
return false
}
}
return nil
}
// Generate and return a new, inverse RuneFilter from the argument.
func (rf RuneFilter) not() RuneFilter {
return func(r rune) bool {
return !rf(r)
}
}
// Generate and return a new RuneFilter, which ignores case, from the argument.
func (rf RuneFilter) ignoreCase() RuneFilter {
return func(r rune) bool {
return rf(unicode.ToLower(r)) || rf(unicode.ToUpper(r))
}
}