-
Notifications
You must be signed in to change notification settings - Fork 29
/
string_list.go
55 lines (47 loc) · 1.11 KB
/
string_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
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package values
import (
"github.com/TeaOSLab/EdgeNode/internal/zero"
"strings"
)
type StringList struct {
ValueMap map[string]zero.Zero
CaseInsensitive bool
}
func NewStringList(caseInsensitive bool) *StringList {
return &StringList{
ValueMap: map[string]zero.Zero{},
CaseInsensitive: caseInsensitive,
}
}
func ParseStringList(v string, caseInsensitive bool) *StringList {
var list = NewStringList(caseInsensitive)
if len(v) == 0 {
return list
}
var lines = strings.Split(v, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
var values = strings.Split(line, ",")
for _, value := range values {
value = strings.TrimSpace(value)
if len(value) > 0 {
if caseInsensitive {
value = strings.ToLower(value)
}
list.ValueMap[value] = zero.Zero{}
}
}
}
return list
}
func (this *StringList) Contains(f string) bool {
if this.CaseInsensitive {
f = strings.ToLower(f)
}
_, ok := this.ValueMap[f]
return ok
}