-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ua.go
46 lines (37 loc) · 890 Bytes
/
ua.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
package useragent
import (
"strings"
)
type Parser struct {
Trie *RuneTrie
}
type UserAgent struct {
// Precedence is the order in which the user agent matched the
// browser, device, and OS. The lower the number, the higher the
// precedence.
browserPrecedence uint8
osPrecedence uint8
typePrecedence uint8
browser string
os string
version string
desktop bool
mobile bool
tablet bool
tv bool
bot bool
}
// Create a new Trie and populate it with user agent data.
func NewParser() *Parser {
trie := NewRuneTrie()
parser := &Parser{Trie: trie}
// For each newline in the file, add the user agent to the trie.
for _, ua := range strings.Split(userAgentsFile, "\n") {
parser.Trie.Put(ua)
}
return parser
}
// Parse a user agent string and return a UserAgent struct.
func (p *Parser) Parse(ua string) UserAgent {
return p.Trie.Get(ua)
}