Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions tsc/internal/glob/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ func (g group) String() string {
}

func (r charRange) String() string {
return "[" + string(r.low) + "-" + string(r.high) + "]"
prefix := "["
if r.negate {
prefix += "!"
}
return prefix + string(r.low) + "-" + string(r.high) + "]"
}

// Match reports whether the input string matches the glob pattern.
Expand All @@ -225,7 +229,7 @@ func match(elems []element, input string) (ok bool) {
if len(input) == 0 || input[0] != '/' {
return false
}
for input[0] == '/' {
for len(input) > 0 && input[0] == '/' {
input = input[1:]
}

Expand Down Expand Up @@ -262,8 +266,8 @@ func match(elems []element, input string) (ok bool) {
input = input[len(elem):]

case star:
var segInput string
segInput, input = split(input)
segInput, _, _ := strings.Cut(input, "/")
input = input[len(segInput):]

elemEnd := len(elems)
for i, e := range elems {
Expand Down Expand Up @@ -318,7 +322,7 @@ func match(elems []element, input string) (ok bool) {
return false
}
c, sz := utf8.DecodeRuneInString(input)
if c < elem.low || c > elem.high {
if (c >= elem.low && c <= elem.high) == elem.negate {
return false
}
input = input[sz:]
Expand Down
59 changes: 59 additions & 0 deletions tsc/internal/glob/glob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package glob

import "testing"

func TestString(t *testing.T) {
t.Parallel()
for _, pattern := range []string{"[0-9]", "[!0-9]", "example.[!α-ω]"} {
t.Run(pattern, func(t *testing.T) {
t.Parallel()
g, err := Parse(pattern)
if err != nil {
t.Fatal(err)
}
if got := g.String(); got != pattern {
t.Errorf("String() = %q, want %q", got, pattern)
}
})
}
}

func TestMatch(t *testing.T) {
t.Parallel()
for _, tt := range []struct {
pattern string
input string
want bool
}{
{"[!0-9]", "a", true},
{"[!0-9]", "0", false},
{"[!0-9]", "5", false},
{"[!0-9]", "9", false},
{"[!0-9]", "/", false},
{"[!0-9]", "", false},
{"[!α-ω]", "a", true},
{"[!α-ω]", "β", false},
{"a/*/c", "a/b/c", true},
{"*/c", "b/c", true},
{"a/*/c", "a/b//c", true},
{"a/*/c", "a/b/d/c", false},
{"*/c", "bc", false},
{"*b/c", "abbb/c", true},
{"*b/c", "abbb/d", false},
{"a/", "a", false},
{"a/", "a/", true},
{"a/", "a///", true},
{"*/", "b/", true},
} {
t.Run(tt.pattern+"_"+tt.input, func(t *testing.T) {
t.Parallel()
g, err := Parse(tt.pattern)
if err != nil {
t.Fatal(err)
}
if got := g.Match(tt.input); got != tt.want {
t.Errorf("Match(%q) = %v, want %v", tt.input, got, tt.want)
}
})
}
}
Loading