-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_wildcard.go
46 lines (40 loc) · 1.23 KB
/
pattern_wildcard.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
package hypermatch
import (
"fmt"
"strings"
)
func validatePatternWildcard(pattern *Pattern) error {
if len(pattern.Value) == 0 {
return fmt.Errorf("[%s] must contain a value", PatternWildcard.String())
}
if len(pattern.Sub) > 0 {
return fmt.Errorf("[%s] must not contain sub-patterns", PatternWildcard.String())
}
if strings.Contains(pattern.Value, "**") {
return fmt.Errorf("[%s] must not contain two consecutive wildcards", PatternWildcard.String())
}
return nil
}
func compilePatternWildcard(start *nfaStep, value []byte, exitFm *fieldMatcher) *fieldMatcher {
step := start
var lastWildcardStep *nfaStep
for i, char := range value {
if char == byteWildcard {
if (i == len(value)-2 && value[len(value)-1] == byteValueTerminator) || i == len(value)-1 {
// wildcard is the last character before value terminator
return step.addOrReuseOrCreateFieldTransition(exitFm)
} else {
lastWildcardStep = step
step = step.MakeStep(byteWildcard)
step.ValueTransitions[byteWildcard] = step
}
} else {
step = step.MakeStep(char)
if lastWildcardStep != nil {
lastWildcardStep.ValueTransitions[char] = step
lastWildcardStep = nil
}
}
}
return step.addOrReuseOrCreateFieldTransition(exitFm)
}