File: pattern.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (84 lines) | stat: -rw-r--r-- 1,675 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package glob

// Pattern is a glob pattern.
type Pattern struct {
	Segments    []Segment
	DirOverride string
}

// Segment is the building block of Pattern.
type Segment interface {
	isSegment()
}

// Slash represents a slash "/".
type Slash struct{}

// Literal is a series of non-slash, non-wildcard characters, that is to be
// matched literally.
type Literal struct {
	Data string
}

// Wild is a wildcard.
type Wild struct {
	Type        WildType
	MatchHidden bool
	Matchers    []func(rune) bool
}

// WildType is the type of a Wild.
type WildType int

// Values for WildType.
const (
	Question = iota
	Star
	StarStar
)

// Match returns whether a rune is within the match set.
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
}

func (Literal) isSegment() {}
func (Slash) isSegment()   {}
func (Wild) isSegment()    {}

// IsSlash returns whether a Segment is a Slash.
func IsSlash(seg Segment) bool {
	_, ok := seg.(Slash)
	return ok
}

// IsLiteral returns whether a Segment is a Literal.
func IsLiteral(seg Segment) bool {
	_, ok := seg.(Literal)
	return ok
}

// IsWild returns whether a Segment is a Wild.
func IsWild(seg Segment) bool {
	_, ok := seg.(Wild)
	return ok
}

// IsWild1 returns whether a Segment is a Wild and has the specified type.
func IsWild1(seg Segment, t WildType) bool {
	return IsWild(seg) && seg.(Wild).Type == t
}

// IsWild2 returns whether a Segment is a Wild and has one of the two specified
// types.
func IsWild2(seg Segment, t1, t2 WildType) bool {
	return IsWild(seg) && (seg.(Wild).Type == t1 || seg.(Wild).Type == t2)
}