github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/glob/pattern.go (about) 1 package glob 2 3 // Pattern is a glob pattern. 4 type Pattern struct { 5 Segments []Segment 6 DirOverride string 7 } 8 9 // Segment is the building block of Pattern. 10 type Segment interface { 11 isSegment() 12 } 13 14 // Slash represents a slash "/". 15 type Slash struct{} 16 17 // Literal is a series of non-slash, non-wildcard characters, that is to be 18 // matched literally. 19 type Literal struct { 20 Data string 21 } 22 23 // Wild is a wildcard. 24 type Wild struct { 25 Type WildType 26 MatchHidden bool 27 Matchers []func(rune) bool 28 } 29 30 // WildType is the type of a Wild. 31 type WildType int 32 33 // Values for WildType. 34 const ( 35 Question = iota 36 Star 37 StarStar 38 ) 39 40 // Match returns whether a rune is within the match set. 41 func (w Wild) Match(r rune) bool { 42 if len(w.Matchers) == 0 { 43 return true 44 } 45 for _, m := range w.Matchers { 46 if m(r) { 47 return true 48 } 49 } 50 return false 51 } 52 53 func (Literal) isSegment() {} 54 func (Slash) isSegment() {} 55 func (Wild) isSegment() {} 56 57 // IsSlash returns whether a Segment is a Slash. 58 func IsSlash(seg Segment) bool { 59 _, ok := seg.(Slash) 60 return ok 61 } 62 63 // IsLiteral returns whether a Segment is a Literal. 64 func IsLiteral(seg Segment) bool { 65 _, ok := seg.(Literal) 66 return ok 67 } 68 69 // IsWild returns whether a Segment is a Wild. 70 func IsWild(seg Segment) bool { 71 _, ok := seg.(Wild) 72 return ok 73 } 74 75 // IsWild1 returns whether a Segment is a Wild and has the specified type. 76 func IsWild1(seg Segment, t WildType) bool { 77 return IsWild(seg) && seg.(Wild).Type == t 78 } 79 80 // IsWild2 returns whether a Segment is a Wild and has one of the two specified 81 // types. 82 func IsWild2(seg Segment, t1, t2 WildType) bool { 83 return IsWild(seg) && (seg.(Wild).Type == t1 || seg.(Wild).Type == t2) 84 }