2016-02-25 22:59:38 +00:00
|
|
|
package pathmatch
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
doesNotMatter = false
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
2019-06-21 04:35:33 +00:00
|
|
|
errThisShouldNeverHappen = newInternalError("This should never happen.")
|
2016-02-25 22:59:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (pattern *internalPattern) Match(path string, args ...interface{}) (bool, error) {
|
|
|
|
|
|
|
|
s := path
|
|
|
|
|
|
|
|
argsIndex := 0
|
|
|
|
for _, bit := range pattern.bits {
|
|
|
|
|
|
|
|
switch bit {
|
|
|
|
default:
|
|
|
|
if !strings.HasPrefix(s, bit) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s = s[len(bit):]
|
|
|
|
case wildcardBit:
|
|
|
|
index := strings.IndexRune(s, '/')
|
|
|
|
if -1 == index {
|
|
|
|
if err := set(s, argsIndex, args...); nil != err {
|
|
|
|
return doesNotMatter, err
|
|
|
|
}
|
|
|
|
argsIndex++
|
|
|
|
} else if 0 <= index {
|
|
|
|
value := s[:index]
|
|
|
|
if err := set(value, argsIndex, args...); nil != err {
|
|
|
|
return doesNotMatter, err
|
|
|
|
}
|
|
|
|
argsIndex++
|
|
|
|
s = s[index:]
|
|
|
|
} else {
|
|
|
|
return doesNotMatter, errThisShouldNeverHappen
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|