-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathua.go
53 lines (42 loc) · 1010 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
47
48
49
50
51
52
53
package useragent
import (
"strings"
"sync"
"github.com/medama-io/go-useragent/internal"
)
var (
once sync.Once
parser *Parser
)
type Parser struct {
Trie *RuneTrie
}
type UserAgent struct {
version [32]rune
versionIndex int
browser internal.Match
os internal.Match
device internal.Match
// 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
}
// Create a new Trie and populate it with user agent data.
func NewParser() *Parser {
once.Do(func() {
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)
}