-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformula_string.go
More file actions
77 lines (68 loc) · 1.79 KB
/
formula_string.go
File metadata and controls
77 lines (68 loc) · 1.79 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
package formula_engine
import (
"strings"
)
const (
commonSign = '%'
commonSignStr = "%"
)
type fLike struct{}
func (f fLike) Invoke(env *Wrapper, args ...*Token) (*Token, error) {
pattern := args[0].getStringValue()
origin := args[1].getStringValue()
if pattern == "" {
if origin == "" {
return newBoolToken(true), nil
} else {
return newBoolToken(false), nil
}
}
realPattern := strings.ReplaceAll(pattern, commonSignStr, "")
startPos := 0
endPos := len(pattern) - 1
result := true
if pattern[startPos] == commonSign && pattern[endPos] == commonSign {
result = strings.Contains(origin, realPattern)
} else if pattern[startPos] == commonSign {
result = strings.HasSuffix(origin, realPattern)
} else if pattern[endPos] == commonSign {
// 正序匹配字符串
for index, c := range realPattern {
if index >= len(origin) || c != rune(origin[index]) {
result = false
}
}
} else {
result = pattern == origin
}
return newBoolToken(result), nil
}
type fHasSubStr struct{}
func (f fHasSubStr) Invoke(env *Wrapper, args ...*Token) (*Token, error) {
bigStr := args[0].getStringValue()
smallStr := args[1].getStringValue()
return newBoolToken(strings.Contains(bigStr, smallStr)), nil
}
/*
realPattern := strings.ReplaceAll(pattern, CommonSignStr, "")
startPos := 0
endPos := len(pattern) - 1
if pattern[startPos] == CommonSign && pattern[endPos] == CommonSign {
return strings.Contains(origin, realPattern)
}
if pattern[startPos] == CommonSign {
index := strings.LastIndex(origin, realPattern)
return index == len(origin)-len(realPattern)
}
if pattern[endPos] == CommonSign {
// 正序匹配字符串
for index, c := range realPattern {
if c != rune(origin[index]) {
return false
}
}
return true
}
// 无通配符
return origin == pattern
*/