d2/d2ir/pattern.go
2023-07-29 16:57:25 -07:00

31 lines
495 B
Go

package d2ir
import (
"strings"
)
func matchPattern(s string, pattern []string) bool {
if len(pattern) == 0 {
return true
}
for i := 0; i < len(pattern); i++ {
if pattern[i] == "*" {
// * so match next.
if i != len(pattern)-1 {
j := strings.Index(s, pattern[i+1])
if j == -1 {
return false
}
s = s[j+len(pattern[i+1]):]
i++
}
} else {
if !strings.HasPrefix(s, pattern[i]) {
return false
}
s = s[len(pattern[i]):]
}
}
return true
}