2017-02-03 03:15:42 +08:00
|
|
|
package glob
|
|
|
|
|
|
|
|
// Pattern is a glob pattern.
|
|
|
|
type Pattern struct {
|
|
|
|
Segments []Segment
|
|
|
|
DirOverride string
|
|
|
|
}
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// Segment is the building block of Pattern.
|
2017-02-03 03:15:42 +08:00
|
|
|
type Segment interface {
|
|
|
|
isSegment()
|
|
|
|
}
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// Slash represents a slash "/".
|
2017-02-03 03:15:42 +08:00
|
|
|
type Slash struct{}
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// Literal is a series of non-slash, non-wildcard characters, that is to be
|
|
|
|
// matched literally.
|
2017-02-03 03:15:42 +08:00
|
|
|
type Literal struct {
|
|
|
|
Data string
|
|
|
|
}
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// Wild is a wildcard.
|
2017-02-03 03:15:42 +08:00
|
|
|
type Wild struct {
|
2017-02-03 05:54:22 +08:00
|
|
|
Type WildType
|
|
|
|
MatchHidden bool
|
2017-02-03 07:24:08 +08:00
|
|
|
Matchers []func(rune) bool
|
2017-02-03 03:15:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// WildType is the type of a Wild.
|
|
|
|
type WildType int
|
|
|
|
|
|
|
|
// Values for WildType.
|
|
|
|
const (
|
|
|
|
Question = iota
|
|
|
|
Star
|
|
|
|
StarStar
|
|
|
|
)
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// Match returns whether a rune is within the match set.
|
2017-02-03 07:24:08 +08:00
|
|
|
func (w Wild) Match(r rune) bool {
|
|
|
|
if len(w.Matchers) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, m := range w.Matchers {
|
|
|
|
if m(r) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-02-03 03:15:42 +08:00
|
|
|
func (Literal) isSegment() {}
|
|
|
|
func (Slash) isSegment() {}
|
|
|
|
func (Wild) isSegment() {}
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// IsSlash returns whether a Segment is a Slash.
|
2017-02-03 03:15:42 +08:00
|
|
|
func IsSlash(seg Segment) bool {
|
|
|
|
_, ok := seg.(Slash)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// IsLiteral returns whether a Segment is a Literal.
|
2017-02-03 03:15:42 +08:00
|
|
|
func IsLiteral(seg Segment) bool {
|
|
|
|
_, ok := seg.(Literal)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// IsWild returns whether a Segment is a Wild.
|
2017-02-03 03:15:42 +08:00
|
|
|
func IsWild(seg Segment) bool {
|
|
|
|
_, ok := seg.(Wild)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// IsWild1 returns whether a Segment is a Wild and has the specified type.
|
2017-02-03 03:15:42 +08:00
|
|
|
func IsWild1(seg Segment, t WildType) bool {
|
|
|
|
return IsWild(seg) && seg.(Wild).Type == t
|
|
|
|
}
|
|
|
|
|
2017-05-22 06:54:04 +08:00
|
|
|
// IsWild2 returns whether a Segment is a Wild and has one of the two specified
|
|
|
|
// types.
|
2017-02-03 03:15:42 +08:00
|
|
|
func IsWild2(seg Segment, t1, t2 WildType) bool {
|
|
|
|
return IsWild(seg) && (seg.(Wild).Type == t1 || seg.(Wild).Type == t2)
|
|
|
|
}
|