-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringCompare.go
More file actions
119 lines (103 loc) · 2.84 KB
/
stringCompare.go
File metadata and controls
119 lines (103 loc) · 2.84 KB
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
114
115
116
117
118
119
package jsonpath
import (
"strings"
"golang.org/x/text/collate"
"golang.org/x/text/language"
)
// stringCompareOptions 定义字符串比较选项
type stringCompareOptions struct {
caseSensitive bool // 是否大小写敏感
locale language.Tag // 用于排序的语言环境
}
// defaultStringCompareOptions 返回默认的字符串比较选项
func defaultStringCompareOptions() stringCompareOptions {
return stringCompareOptions{
caseSensitive: true,
locale: language.English, // 默认使用英语排序规则
}
}
// stringComparer 提供标准化的字符串比较功能
type stringComparer struct {
options stringCompareOptions
collator *collate.Collator
}
// newStringComparer 创建一个新的字符串比较器
func newStringComparer(opts ...stringCompareOptions) *stringComparer {
options := defaultStringCompareOptions()
if len(opts) > 0 {
options = opts[0]
}
return &stringComparer{
options: options,
collator: collate.New(options.locale),
}
}
// compare 比较两个字符串
// 返回值:
// -1: a < b
//
// 0: a == b
// 1: a > b
func (sc *stringComparer) compare(a, b string) int {
if sc.options.caseSensitive {
// 当大小写敏感且使用默认英语环境时,使用 strings.Compare
if sc.options.locale == language.English {
return strings.Compare(a, b)
}
// 当大小写敏感且使用其他语言环境时,使用 collator
result := sc.collator.CompareString(a, b)
if result < 0 {
return -1
} else if result > 0 {
return 1
}
return 0
}
// 当大小写不敏感时,使用 collator 并转换为小写
result := sc.collator.CompareString(strings.ToLower(a), strings.ToLower(b))
if result < 0 {
return -1
} else if result > 0 {
return 1
}
return 0
}
// equals 检查两个字符串是否相等
func (sc *stringComparer) equals(a, b string) bool {
return sc.compare(a, b) == 0
}
// lessThan 检查 a 是否小于 b
func (sc *stringComparer) lessThan(a, b string) bool {
return sc.compare(a, b) < 0
}
// lessThanOrEqual 检查 a 是否小于等于 b
func (sc *stringComparer) lessThanOrEqual(a, b string) bool {
return sc.compare(a, b) <= 0
}
// greaterThan 检查 a 是否大于 b
func (sc *stringComparer) greaterThan(a, b string) bool {
return sc.compare(a, b) > 0
}
// greaterThanOrEqual 检查 a 是否大于等于 b
func (sc *stringComparer) greaterThanOrEqual(a, b string) bool {
return sc.compare(a, b) >= 0
}
// standardCompareStrings 使用指定的操作符比较两个字符串
func standardCompareStrings(a string, operator string, b string) bool {
sc := newStringComparer()
switch operator {
case "==":
return sc.equals(a, b)
case "!=":
return !sc.equals(a, b)
case "<":
return sc.lessThan(a, b)
case "<=":
return sc.lessThanOrEqual(a, b)
case ">":
return sc.greaterThan(a, b)
case ">=":
return sc.greaterThanOrEqual(a, b)
}
return false
}