feat: initial commit
This commit is contained in:
44
wildcard/match.go
Normal file
44
wildcard/match.go
Normal file
@ -0,0 +1,44 @@
|
||||
package wildcard
|
||||
|
||||
const wildcard = '*'
|
||||
|
||||
func Match(str, pattern string) bool {
|
||||
if pattern == "" {
|
||||
return str == pattern
|
||||
}
|
||||
|
||||
if pattern == string(wildcard) {
|
||||
return true
|
||||
}
|
||||
|
||||
return deepMatchRune([]rune(str), []rune(pattern))
|
||||
}
|
||||
|
||||
func MatchAny(str string, patterns ...string) bool {
|
||||
for _, p := range patterns {
|
||||
if matches := Match(str, p); matches {
|
||||
return matches
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func deepMatchRune(str, pattern []rune) bool {
|
||||
for len(pattern) > 0 {
|
||||
switch pattern[0] {
|
||||
default:
|
||||
if len(str) == 0 || str[0] != pattern[0] {
|
||||
return false
|
||||
}
|
||||
case wildcard:
|
||||
return deepMatchRune(str, pattern[1:]) ||
|
||||
(len(str) > 0 && deepMatchRune(str[1:], pattern))
|
||||
}
|
||||
|
||||
str = str[1:]
|
||||
pattern = pattern[1:]
|
||||
}
|
||||
|
||||
return len(str) == 0 && len(pattern) == 0
|
||||
}
|
Reference in New Issue
Block a user