-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevice_list.go
84 lines (72 loc) · 1.94 KB
/
device_list.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
package devicedetector
import (
"github.com/gijsbers/go-pcre"
"github.com/robicode/device-detector/util"
)
// A DeviceList holds a list of devices loaded from YAML files.
type CachedDeviceList struct {
list map[string]CachedDevice
}
// NewDeviceList creates a new list of devices.
func NewDeviceList() *CachedDeviceList {
devices := make(map[string]CachedDevice)
return &CachedDeviceList{
list: devices,
}
}
// Append returns a copy of the *CachedDeviceList with the given entry appended
// to the end of the list. If the new item already appears in the list, Append
// returns the original list unmodified.
func (d *CachedDeviceList) Append(name string, entry CachedDevice) *CachedDeviceList {
for _, item := range d.list {
if item.Regex == entry.Regex {
return d
}
}
dl := NewDeviceList()
for key, value := range d.list {
dl.list[key] = value
}
dl.list[name] = entry
return dl
}
// Delete deletes an item from the *CachedDeviceList. If the item is not
// found, the original list is returned.
func (d *CachedDeviceList) Delete(item string) *CachedDeviceList {
if _, ok := d.list[item]; ok {
devices := NewDeviceList()
for name, device := range d.list {
if name == item {
continue
}
devices.Append(name, device)
}
return devices
}
return d
}
// findModel attempts to locate a matching model, if any.
func (e *CachedDevice) FindModel(userAgent string) *CachedModel {
if len(e.Models) == 0 {
return nil
}
for _, model := range e.Models {
if !model.compiled && model.compileError == nil {
re, err := pcre.Compile(util.FixupRegex(model.Regex), pcre.CASELESS)
if err != nil {
model.compileError = err
continue
}
model.compiled = true
model.compiledRegex = re
}
// pcre uses Regexp, not *Regexp, so we can't check it for nil.
if model.compileError == nil {
modelMatcher := model.compiledRegex.MatcherString(userAgent, 0)
if modelMatcher.Matches() {
return &model
}
}
}
return nil
}