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
|
|
|
|
)
|
|
|
|
|
|
2019-06-24 21:17:57 +00:00
|
|
|
|
// Find compares ‘path’ against its (compiled) pattern template; if it matches it loads the
|
|
|
|
|
// matches into ‘args’, and then returns true.
|
|
|
|
|
//
|
|
|
|
|
// Find may set some, or all of the items in ‘args’ even if it returns false, and even if it
|
|
|
|
|
// returns an error.
|
2019-06-21 06:28:50 +00:00
|
|
|
|
func (pattern *Pattern) Find(path string, args ...interface{}) (bool, error) {
|
2019-06-21 20:37:53 +00:00
|
|
|
|
if nil == pattern {
|
|
|
|
|
return false, errNilReceiver
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pattern.mutex.RLock()
|
|
|
|
|
defer pattern.mutex.RUnlock()
|
2016-02-25 22:59:38 +00:00
|
|
|
|
|
|
|
|
|
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:
|
2019-06-24 22:04:02 +00:00
|
|
|
|
if "" == s {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 22:59:38 +00:00
|
|
|
|
index := strings.IndexRune(s, '/')
|
2019-06-24 21:17:57 +00:00
|
|
|
|
|
|
|
|
|
var value string
|
|
|
|
|
switch {
|
|
|
|
|
default:
|
2016-02-25 22:59:38 +00:00
|
|
|
|
return doesNotMatter, errThisShouldNeverHappen
|
2019-06-24 21:17:57 +00:00
|
|
|
|
case -1 == index:
|
|
|
|
|
value = s
|
|
|
|
|
case 0 <= index:
|
|
|
|
|
value = s[:index]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := set(value, argsIndex, args...); nil != err {
|
|
|
|
|
return doesNotMatter, err
|
2016-02-25 22:59:38 +00:00
|
|
|
|
}
|
2019-06-24 21:17:57 +00:00
|
|
|
|
argsIndex++
|
|
|
|
|
s = s[len(value):]
|
2016-02-25 22:59:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-24 21:17:57 +00:00
|
|
|
|
if "" != s {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 22:59:38 +00:00
|
|
|
|
return true, nil
|
|
|
|
|
}
|