-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathglob.go
149 lines (137 loc) · 3.79 KB
/
glob.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package mimemagic
import (
"sort"
"strings"
)
type byteMatcher interface {
matchByte(byte) bool
}
type matcher interface {
len() int
match(string) bool
}
type glob interface {
matcher
isCaseSensitive() bool
mediaType() simpleGlob
}
type value string
type list string
type byteRange struct{ min, max byte }
type any []byteMatcher
type pattern struct {
matchers []matcher
length int
}
type textPattern struct {
pattern
caseSensitive bool
mimeType, weight int
}
type suffixPattern struct {
pattern
caseSensitive bool
mimeType, weight int
}
type prefixPattern struct {
pattern
caseSensitive bool
mimeType, weight int
}
func (v value) len() int { return len(v) }
func (list) len() int { return 1 }
func (byteRange) len() int { return 1 }
func (any) len() int { return 1 }
func (p pattern) len() int { return p.length }
func (l list) matchByte(b byte) bool { return strings.IndexByte(string(l), b) >= 0 }
func (r byteRange) matchByte(b byte) bool { return r.min <= b && b <= r.max }
func (a any) matchByte(b byte) bool {
for _, m := range a {
if m.matchByte(b) {
return true
}
}
return false
}
func (v value) match(s string) bool {
return s == string(v)
}
func (l list) match(s string) bool {
return l.matchByte(s[0])
}
func (r byteRange) match(s string) bool {
return r.matchByte(s[0])
}
func (a any) match(s string) bool {
return a.matchByte(s[0])
}
func (p pattern) match(s string) bool {
for _, ml := range p.matchers {
if !ml.match(s[:ml.len()]) {
return false
}
s = s[ml.len():]
}
return true
}
func (t textPattern) match(s string) bool { return len(s) == t.len() && t.pattern.match(s) }
func (t suffixPattern) match(s string) bool {
return len(s) >= t.len() && t.pattern.match(s[len(s)-t.len():])
}
func (t prefixPattern) match(s string) bool { return len(s) >= t.len() && t.pattern.match(s[:t.len()]) }
func (t textPattern) isCaseSensitive() bool { return t.caseSensitive }
func (t suffixPattern) isCaseSensitive() bool { return t.caseSensitive }
func (t prefixPattern) isCaseSensitive() bool { return t.caseSensitive }
func (t textPattern) mediaType() simpleGlob { return simpleGlob{t.weight, t.mimeType} }
func (t suffixPattern) mediaType() simpleGlob { return simpleGlob{t.weight, t.mimeType} }
func (t prefixPattern) mediaType() simpleGlob { return simpleGlob{t.weight, t.mimeType} }
type simpleGlob struct {
weight, mimeType int
}
// MatchGlob determines the MIME type of the file using
// exclusively its filename.
func MatchGlob(filename string) MediaType {
return mediaTypes[matchGlob(filename)]
}
func matchGlob(filename string) int {
return matchGlobAll(filename)[0]
}
func matchGlobAll(filename string) []int {
var globResults []simpleGlob
lowerCase := strings.ToLower(filename)
if t, ok := textCS[filename]; ok {
globResults = append(globResults, t...)
}
if t, ok := text[lowerCase]; ok {
globResults = append(globResults, t...)
}
fnLen := len(filename)
for l := min(len(filename), globMaxLen); l > 0; l-- {
if t, ok := suffixesCS[filename[fnLen-l:]]; ok {
globResults = append(globResults, t...)
}
if t, ok := prefixesCS[filename[:l]]; ok {
globResults = append(globResults, t...)
}
if t, ok := suffixes[lowerCase[fnLen-l:]]; ok {
globResults = append(globResults, t...)
}
if t, ok := prefixes[lowerCase[:l]]; ok {
globResults = append(globResults, t...)
}
}
for _, g := range globs {
if (g.isCaseSensitive() || g.match(lowerCase)) && (!g.isCaseSensitive() || g.match(filename)) {
globResults = append(globResults, g.mediaType())
}
}
if globResults == nil {
return []int{unknownType}
}
sort.Slice(globResults, func(i, j int) bool { return globResults[i].weight > globResults[j].weight })
results := make([]int, len(globResults))
for i := range globResults {
results[i] = globResults[i].mimeType
}
return results
}