mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package yqlib
|
|
|
|
func matchKey(name string, pattern string) (matched bool) {
|
|
if pattern == "" {
|
|
return name == pattern
|
|
}
|
|
if pattern == "*" {
|
|
log.Debug("wild!")
|
|
return true
|
|
}
|
|
return deepMatch(name, pattern)
|
|
}
|
|
|
|
// deepMatch reports whether the name matches the pattern in linear time.
|
|
// Source https://research.swtch.com/glob
|
|
func deepMatch(name, pattern string) bool {
|
|
px := 0
|
|
nx := 0
|
|
nextPx := 0
|
|
nextNx := 0
|
|
for px < len(pattern) || nx < len(name) {
|
|
if px < len(pattern) {
|
|
c := pattern[px]
|
|
switch c {
|
|
default: // ordinary character
|
|
if nx < len(name) && name[nx] == c {
|
|
px++
|
|
nx++
|
|
continue
|
|
}
|
|
case '?': // single-character wildcard
|
|
if nx < len(name) {
|
|
px++
|
|
nx++
|
|
continue
|
|
}
|
|
case '*': // zero-or-more-character wildcard
|
|
// Try to match at nx.
|
|
// If that doesn't work out,
|
|
// restart at nx+1 next.
|
|
nextPx = px
|
|
nextNx = nx + 1
|
|
px++
|
|
continue
|
|
}
|
|
}
|
|
// Mismatch. Maybe restart.
|
|
if 0 < nextNx && nextNx <= len(name) {
|
|
px = nextPx
|
|
nx = nextNx
|
|
continue
|
|
}
|
|
return false
|
|
}
|
|
// Matched all of pattern to all of name. Success.
|
|
return true
|
|
}
|