-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor simpler glob match with one wildcard only
- Loading branch information
1 parent
2459f1a
commit 282db65
Showing
4 changed files
with
141 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package policy | ||
|
||
// validateGlobPattern ensures the pattern conforms to the spec: only '*' and escaped '\*' are allowed. | ||
func validateGlobPattern(pattern string) bool { | ||
for i := 0; i < len(pattern); i++ { | ||
if pattern[i] == '*' { | ||
continue | ||
} | ||
if pattern[i] == '\\' && i+1 < len(pattern) && pattern[i+1] == '*' { | ||
i++ // skip the escaped '*' | ||
continue | ||
} | ||
if pattern[i] == '\\' && i+1 < len(pattern) { | ||
i++ // skip the escaped character | ||
continue | ||
} | ||
if pattern[i] == '\\' { | ||
return false // invalid escape sequence | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
// globMatch matches a string against a pattern with '*' wildcards, handling escaped '\*' literals. | ||
func globMatch(pattern, str string) bool { | ||
if !validateGlobPattern(pattern) { | ||
return false | ||
} | ||
|
||
var i, j int // i is the index for the pattern, j is the index for the string | ||
for i < len(pattern) && j < len(str) { | ||
switch pattern[i] { | ||
case '*': | ||
// Skip consecutive '*' characters | ||
for i < len(pattern) && pattern[i] == '*' { | ||
i++ | ||
} | ||
if i == len(pattern) { | ||
return true | ||
} | ||
// Match the rest of the pattern | ||
for j < len(str) { | ||
if globMatch(pattern[i:], str[j:]) { | ||
return true | ||
} | ||
j++ | ||
} | ||
return false | ||
case '\\': | ||
// Handle escaped '*' | ||
i++ | ||
if i < len(pattern) && pattern[i] == '*' { | ||
if str[j] != '*' { | ||
return false | ||
} | ||
i++ | ||
j++ | ||
} else { | ||
if i >= len(pattern) || pattern[i] != str[j] { | ||
return false | ||
} | ||
i++ | ||
j++ | ||
} | ||
default: | ||
if pattern[i] != str[j] { | ||
return false | ||
} | ||
i++ | ||
j++ | ||
} | ||
} | ||
// Check for remaining characters in pattern | ||
for i < len(pattern) && pattern[i] == '*' { | ||
i++ | ||
} | ||
return i == len(pattern) && j == len(str) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package policy | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSimpleGlobMatch(t *testing.T) { | ||
tests := []struct { | ||
pattern string | ||
str string | ||
matches bool | ||
}{ | ||
// Basic matching | ||
{"*", "anything", true}, | ||
{"a*", "abc", true}, | ||
{"*c", "abc", true}, | ||
{"a*c", "abc", true}, | ||
{"a*c", "abxc", true}, | ||
{"a*c", "ac", true}, | ||
{"a*c", "a", false}, | ||
{"a*c", "ab", false}, | ||
|
||
// Escaped characters | ||
{"a\\*c", "a*c", true}, | ||
{"a\\*c", "abc", false}, | ||
|
||
// Mixed wildcards and literals | ||
{"a*b*c", "abc", true}, | ||
{"a*b*c", "aXbYc", true}, | ||
{"a*b*c", "aXbY", false}, | ||
{"a*b*c", "abYc", true}, | ||
{"a*b*c", "aXbc", true}, | ||
{"a*b*c", "aXbYcZ", false}, | ||
|
||
// Edge cases | ||
{"", "", true}, | ||
{"", "a", false}, | ||
{"*", "", true}, | ||
{"*", "a", true}, | ||
{"\\*", "*", true}, | ||
{"\\*", "a", false}, | ||
|
||
// Specified test cases | ||
{"Alice\\*, Bob*, Carol.", "Alice*, Bob, Carol.", true}, | ||
{"Alice\\*, Bob*, Carol.", "Alice*, Bob, Dan, Erin, Carol.", true}, | ||
{"Alice\\*, Bob*, Carol.", "Alice*, Bob , Carol.", true}, | ||
{"Alice\\*, Bob*, Carol.", "Alice*, Bob*, Carol.", true}, | ||
{"Alice\\*, Bob*, Carol.", "Alice*, Bob, Carol", false}, | ||
{"Alice\\*, Bob*, Carol.", "Alice*, Bob*, Carol!", false}, | ||
{"Alice\\*, Bob*, Carol.", "Alice, Bob, Carol.", false}, | ||
{"Alice\\*, Bob*, Carol.", "Alice Cooper, Bob, Carol.", false}, | ||
{"Alice\\*, Bob*, Carol.", " Alice*, Bob, Carol. ", false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.pattern+"_"+tt.str, func(t *testing.T) { | ||
assert.Equal(t, tt.matches, globMatch(tt.pattern, tt.str)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters