-
Notifications
You must be signed in to change notification settings - Fork 218
/
like_expression.go
87 lines (74 loc) · 2.05 KB
/
like_expression.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
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package expression
import (
cesql "github.com/cloudevents/sdk-go/sql/v2"
"github.com/cloudevents/sdk-go/sql/v2/utils"
cloudevents "github.com/cloudevents/sdk-go/v2"
)
type likeExpression struct {
baseUnaryExpression
pattern string
}
func (l likeExpression) Evaluate(event cloudevents.Event) (interface{}, error) {
val, err := l.child.Evaluate(event)
if err != nil {
return nil, err
}
val, err = utils.Cast(val, cesql.StringType)
if err != nil {
return nil, err
}
return matchString(val.(string), l.pattern), nil
}
func NewLikeExpression(child cesql.Expression, pattern string) (cesql.Expression, error) {
return likeExpression{
baseUnaryExpression: baseUnaryExpression{
child: child,
},
pattern: pattern,
}, nil
}
func matchString(text, pattern string) bool {
textLen := len(text)
patternLen := len(pattern)
textIdx := 0
patternIdx := 0
lastWildcardIdx := -1
lastMatchIdx := 0
for textIdx < textLen {
// handle escaped characters -> pattern needs to increment two places here
if patternIdx < patternLen-1 && pattern[patternIdx] == '\\' &&
((pattern[patternIdx+1] == '_' || pattern[patternIdx+1] == '%') &&
pattern[patternIdx+1] == text[textIdx]) {
patternIdx += 2
textIdx += 1
// handle non escaped characters
} else if patternIdx < patternLen && (pattern[patternIdx] == '_' || pattern[patternIdx] == text[textIdx]) {
textIdx += 1
patternIdx += 1
// handle wildcard characters
} else if patternIdx < patternLen && pattern[patternIdx] == '%' {
lastWildcardIdx = patternIdx
lastMatchIdx = textIdx
patternIdx += 1
// greedy match didn't work, try again from the last known match
} else if lastWildcardIdx != -1 {
patternIdx = lastWildcardIdx + 1
lastMatchIdx += 1
textIdx = lastMatchIdx
} else {
return false
}
}
// consume remaining pattern characters as long as they are wildcards
for patternIdx < patternLen {
if pattern[patternIdx] != '%' {
return false
}
patternIdx += 1
}
return true
}