Skip to content

Commit 256dc44

Browse files
committed
Save an extra call to strings.HasPrefix
1 parent 1fea9be commit 256dc44

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

glob.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,25 @@ func Glob(pattern, subj string) bool {
3030
trailingGlob := strings.HasSuffix(pattern, GLOB)
3131
end := len(parts) - 1
3232

33-
// Check the first section. Requires special handling.
34-
if !leadingGlob {
35-
if strings.HasPrefix(subj, parts[0]) {
36-
// Strip prefix, to avoid matching it again
37-
subj = subj[len(parts[0]):]
38-
} else {
39-
return false
40-
}
41-
}
42-
43-
// Go over the middle parts and ensure they match.
44-
for i := 1; i < end; i++ {
45-
partStartIdx := strings.Index(subj, parts[i])
46-
if partStartIdx < 0 {
47-
return false
33+
// Go over the leading parts and ensure they match.
34+
for i := 0; i < end; i++ {
35+
idx := strings.Index(subj, parts[i])
36+
37+
switch i {
38+
case 0:
39+
// Check the first section. Requires special handling.
40+
if !leadingGlob && idx != 0 {
41+
return false
42+
}
43+
default:
44+
// Check that the middle parts match.
45+
if idx < 0 {
46+
return false
47+
}
4848
}
4949

5050
// Trim evaluated text from subj as we loop over the pattern.
51-
subj = subj[partStartIdx+len(parts[i]):]
51+
subj = subj[idx+len(parts[i]):]
5252
}
5353

5454
// Reached the last section. Requires special handling.

0 commit comments

Comments
 (0)