-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcachefile_list.go
113 lines (96 loc) · 2.64 KB
/
cachefile_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
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
package devicedetector
import (
"log"
"path/filepath"
"strings"
"github.com/robicode/device-detector/util"
)
// CacheFileList is a helper struct used to pass a list of caches to search into
// the cache's findRegex function.
type CacheFileList struct {
originalList []string
filenames []string
}
// NewCacheFileList creates a new *CacheFileList, given the base list as an optional parameter.
// If no base is provided, uses the entires cacheFilenames array as a starting point.
func NewCacheFileList(baseList ...string) *CacheFileList {
var files []string
var originalFiles []string
if len(baseList) == 0 {
for _, file := range cacheFiles {
if strings.HasPrefix(file, "regexes/") {
files = append(files, file)
} else {
files = append(files, filepath.Join("regexes", file))
}
}
originalFiles = files
} else {
for _, file := range baseList {
if strings.HasPrefix(file, "regexes/") {
files = append(files, file)
originalFiles = append(originalFiles, file)
} else {
files = append(files, filepath.Join("regexes", file))
originalFiles = append(originalFiles, filepath.Join("regexes", file))
}
}
}
return &CacheFileList{
filenames: files,
originalList: originalFiles,
}
}
// Exclude returns a new *CacheFileList with the specified files removed.
func (c *CacheFileList) Exclude(excludes ...string) *CacheFileList {
if len(excludes) == 0 {
log.Println("Nothing to exclude.")
return c
}
for _, exclude := range excludes {
if !util.InStrArray(exclude, c.originalList) {
return c
}
}
newList := []string{}
for _, file := range c.originalList {
if !util.InStrArray(file, excludes) {
newList = append(newList, file)
}
}
newCacheFiles := NewCacheFileList()
newCacheFiles.filenames = newList
return newCacheFiles
}
// Includes returns true if the given filename is in the *CacheFileList.
func (c *CacheFileList) Includes(filename string) bool {
for _, file := range c.filenames {
if file == filename {
return true
}
}
return false
}
// Get returns the filename at the given index.
func (c *CacheFileList) Get(index int) string {
if index > len(c.filenames)-1 {
return ""
}
return c.filenames[index]
}
// Exclusive returns a copy of the *CacheFileList containing only the given filenames.
func (c *CacheFileList) Exclusive(filenames ...string) *CacheFileList {
if len(filenames) == 0 || len(filenames) == len(deviceFilenames) {
return c
}
newArr := []string{}
for _, file := range filenames {
if !util.InStrArray(file, c.originalList) {
return c
}
newArr = append(newArr, file)
}
newFileList := NewCacheFileList()
newFileList.filenames = newArr
return newFileList
}