Playground
glob.gno
// Package glob matches shell-style patterns against strings, in two modes:
// plain, where `*` crosses everything, and path, where `/` is a boundary that
// only `**` crosses.
//
// The reason to have it on chain is the allowlist. A realm that decides what a
// caller may touch writes the rule as a pattern (gno.land/r/moul/**), and the
// obvious alternative, strings.HasPrefix, is wrong in a way that only shows up
// in production: the prefix gno.land/r/moul/home also matches
// gno.land/r/moul/homeless, and the prefix a caller gets by trimming /** off
// the rule above, gno.land/r/moul, also matches gno.land/r/moulier/drain, a
// different namespace owned by a different person. A separator-aware matcher
// cannot make either mistake, because a boundary is part of the grammar rather
// than something the caller remembered to append. Remembering to append it is
// no answer either: gno.land/r/moul/home with a slash on the end stops being a
// rule that matches the path it names.
//
// The matcher is iterative with a single backtrack point, so it runs in
// O(len(pattern) * len(name)) time and constant space. That is not a style
// preference. The recursive one-liner every glob tutorial shows is exponential
// on a pattern like a*a*a*a*a*a*b, and a realm that accepts a pattern from a
// caller and runs an exponential matcher on it has handed out a gas bomb.
//
// Matching is byte-oriented, deliberately: `?` matches one byte and a class
// range compares bytes, so this package is for ASCII subjects (package paths,
// bech32 addresses, handles, file names) and not for matching prose. See the
// package README for what that rules out.
//
// A live demo of this package is at
// [r/moul/x/daily/globdemo](/r/moul/x/daily/globdemo/v0).
package glob
import "strings"
// Separator is the byte [MatchPath] treats as a path boundary.
const Separator = '/'
// Match reports whether name matches pattern.
//
// ? any single byte
// * any sequence of bytes, including none
// [abc] any one of the listed bytes
// [a-z] any byte in the range, ends inclusive
// [!abc] any byte NOT listed; [^abc] is the same thing
// \x the literal byte x, for any x
// anything else itself
//
// `*` crosses every byte, including [Separator]. Use [MatchPath] when the
// subject is a path and a wildcard must stay inside one segment.
//
// An invalid pattern matches nothing. There is no error return, so the choice
// is between inert and permissive, and a bad allowlist entry that matches
// everything is the worse of the two. Ask [Valid] when it matters.
func Match(pattern, name string) bool {
if !Valid(pattern) {
return false
}
return matchBytes(pattern, name)
}
// MatchPath reports whether name matches pattern, with [Separator] as a
// boundary neither side may cross by accident.
//
// Both are split into segments, and then:
//
// - a segment that is exactly `**` matches zero or more whole segments
// - every other segment is matched by [Match] against one segment, so `*`,
// `?` and a class can never reach past the separator
//
// `**` only crosses as a whole segment. Glued to anything else, as in `a**b`,
// it is an ordinary star confined to its segment.
//
// A [Separator] that is escaped (`a\/b`) or inside a class (`[/]`) belongs to
// its segment and does not split the pattern. Such a pattern matches nothing,
// because a name segment can never contain a separator, and it says so by
// returning false rather than by aborting.
//
// An invalid pattern matches nothing, as in [Match].
func MatchPath(pattern, name string) bool {
if !Valid(pattern) {
return false
}
return matchSegments(splitPattern(pattern), splitName(name))
}
// Valid reports whether pattern is well formed.
//
// Three things make it malformed, and each of them is a typo a caller makes
// rather than a construct anyone writes on purpose: an unterminated class
// (`[a-z`), an empty one (`[]`, `[!]`), and a trailing backslash with nothing
// to escape (`abc\`).
func Valid(pattern string) bool {
for i := 0; i < len(pattern); {
switch pattern[i] {
case '\\':
if i+1 >= len(pattern) {
return false
}
i += 2
case '[':
j := i + 1
if j < len(pattern) && (pattern[j] == '!' || pattern[j] == '^') {
j++
}
start := j
for j < len(pattern) && pattern[j] != ']' {
if pattern[j] == '\\' {
j++
if j >= len(pattern) {
return false
}
}
j++
}
// j == start is an empty class, which is a typo rather than a
// class that matches nothing: nobody writes one deliberately.
if j >= len(pattern) || j == start {
return false
}
i = j + 1
default:
i++
}
}
return true
}
// Filter returns the elements of names matching pattern, in their original
// order, using [Match].
//
// The result is never nil, so a caller can range and len it with no guard.
func Filter(pattern string, names []string) []string {
out := []string{}
if !Valid(pattern) {
return out
}
for _, n := range names {
if matchBytes(pattern, n) {
out = append(out, n)
}
}
return out
}
// FilterPath is [Filter] with [MatchPath] semantics.
func FilterPath(pattern string, names []string) []string {
out := []string{}
if !Valid(pattern) {
return out
}
psegs := splitPattern(pattern)
for _, n := range names {
if matchSegments(psegs, splitName(n)) {
out = append(out, n)
}
}
return out
}
// Any reports whether name matches at least one of patterns, using [Match].
//
// An invalid entry is skipped rather than failing the whole list: one typo in
// an allowlist should cost that rule and no other.
func Any(patterns []string, name string) bool {
for _, p := range patterns {
if Match(p, name) {
return true
}
}
return false
}
// AnyPath is [Any] with [MatchPath] semantics. This is the allowlist
// primitive: a list of rules, a subject, one boolean.
func AnyPath(patterns []string, name string) bool {
for _, p := range patterns {
if MatchPath(p, name) {
return true
}
}
return false
}
// matchBytes is the matcher proper, over a pattern already known valid.
//
// It walks both strings forward and remembers exactly one position: the most
// recent `*` and how much of name it had consumed when it was reached. On a
// mismatch it returns there and hands that star one more byte. That single
// backtrack point is what keeps the whole thing O(len(pattern)*len(name)) in
// time and O(1) in space, where the recursive formulation branches at every
// star and goes exponential.
//
// It is correct precisely because every other pattern element consumes exactly
// one byte: a later star can always absorb whatever an earlier one gives back,
// so there is never a reason to reconsider any star but the last.
func matchBytes(pattern, name string) bool {
px, nx := 0, 0
starPx, starNx := -1, -1
for nx < len(name) {
if px < len(pattern) {
if pattern[px] == '*' {
// Record the star and try to match the rest with it
// consuming nothing. Consecutive stars collapse naturally:
// the last one simply overwrites the mark.
starPx, starNx = px, nx
px++
continue
}
if ok, next := matchOne(pattern, px, name[nx]); ok {
px = next
nx++
continue
}
}
if starPx < 0 {
return false
}
starNx++
px, nx = starPx+1, starNx
}
// name is exhausted; the pattern may only have stars left.
for px < len(pattern) && pattern[px] == '*' {
px++
}
return px == len(pattern)
}
// matchOne reports whether the single-byte pattern element at px matches b,
// and returns the index just past that element.
//
// The pattern is already known valid, which is why this can index past px
// without checking: Valid has established that an escape has a byte after it
// and a class has its closing bracket.
func matchOne(pattern string, px int, b byte) (bool, int) {
switch pattern[px] {
case '?':
return true, px + 1
case '\\':
return pattern[px+1] == b, px + 2
case '[':
return matchClass(pattern, px, b)
default:
return pattern[px] == b, px + 1
}
}
// matchClass evaluates the bracket expression starting at px against b and
// returns the index just past its closing bracket.
//
// There is no POSIX leading-`]` rule: a `]` inside a class is written `\]`.
// One way to say it beats two, and the one that survives is the one that also
// works for `-` and `\`.
func matchClass(pattern string, px int, b byte) (bool, int) {
i := px + 1
negated := false
if pattern[i] == '!' || pattern[i] == '^' {
negated = true
i++
}
found := false
for pattern[i] != ']' {
lo := pattern[i]
if lo == '\\' {
i++
lo = pattern[i]
}
i++
// A '-' is a range only with something other than ']' after it, which
// is what makes [a-] and [-a] literal dashes rather than errors.
hi := lo
if pattern[i] == '-' && pattern[i+1] != ']' {
i++
hi = pattern[i]
if hi == '\\' {
i++
hi = pattern[i]
}
i++
}
if lo <= b && b <= hi {
found = true
}
}
return found != negated, i + 1
}
// splitName splits a subject on [Separator]. A name carries no pattern syntax,
// so there is nothing to protect and a plain split is right.
func splitName(name string) []string {
return strings.Split(name, string(Separator))
}
// splitPattern splits a pattern on every [Separator] that is not escaped and
// not inside a character class.
//
// A plain strings.Split is wrong here, and wrong by aborting rather than by
// answering: it cuts `a\/b` into `a\` and `b`, handing matchBytes a segment
// whose trailing backslash has nothing after it, and `[/]` into `[` and `]`,
// handing matchClass a bracket with no closing one. Both then index past the
// end of the piece they were given, out of a Render that cannot be redeployed.
// Splitting with the escape and the class in mind keeps every pattern element
// whole, so each segment is itself a valid pattern.
//
// The resulting segment can still never match, since a name segment cannot
// contain a separator. That is the correct answer and this returns it.
func splitPattern(pattern string) []string {
segs := []string{}
start := 0
inClass := false
for i := 0; i < len(pattern); i++ {
switch pattern[i] {
case '\\':
// Valid has established there is a byte after it.
i++
case '[':
inClass = true
case ']':
inClass = false
case Separator:
if inClass {
continue
}
segs = append(segs, pattern[start:i])
start = i + 1
}
}
return append(segs, pattern[start:])
}
// matchSegments is matchBytes one level up: the same single-backtrack walk,
// with `**` for the star and a whole segment for the byte.
//
// Writing it twice rather than generalising is the cheaper of the two: the
// element test differs (a byte equality against a glob call), the alphabet
// differs, and the shared version would take a comparison function and be
// longer than both. What the duplication buys is that the zero-segment case,
// `a/**/b` matching `a/b`, falls out of the algorithm instead of needing the
// special case a segment-by-segment walk requires.
func matchSegments(psegs, nsegs []string) bool {
pi, ni := 0, 0
starPi, starNi := -1, -1
for ni < len(nsegs) {
if pi < len(psegs) {
if psegs[pi] == "**" {
starPi, starNi = pi, ni
pi++
continue
}
if matchBytes(psegs[pi], nsegs[ni]) {
pi++
ni++
continue
}
}
if starPi < 0 {
return false
}
starNi++
pi, ni = starPi+1, starNi
}
for pi < len(psegs) && psegs[pi] == "**" {
pi++
}
return pi == len(psegs)
}
glob_test.gno
package glob
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
func TestMatchLiteralsAndSingles(t *testing.T) {
cases := []struct {
pattern string
name string
want bool
}{
{"", "", true},
{"", "a", false},
{"a", "", false},
{"abc", "abc", true},
{"abc", "abd", false},
{"abc", "abcd", false},
{"?", "a", true},
{"?", "", false},
{"a?c", "abc", true},
{"a?c", "ac", false},
{"???", "abc", true},
{"???", "ab", false},
// '?' is one BYTE, not one rune. Documented, and pinned here so the
// claim in the README cannot drift away from the code.
{"?", "é", false},
{"??", "é", true},
}
for _, tc := range cases {
got := Match(tc.pattern, tc.name)
uassert.Equal(t, tc.want, got, "Match("+tc.pattern+", "+tc.name+")")
}
}
func TestMatchStar(t *testing.T) {
cases := []struct {
pattern string
name string
want bool
}{
{"*", "", true},
{"*", "anything", true},
{"*", "with/slashes/inside", true},
{"a*", "a", true},
{"a*", "abc", true},
{"a*", "b", false},
{"*c", "abc", true},
{"*c", "abd", false},
{"a*c", "ac", true},
{"a*c", "abc", true},
{"a*c", "abbbbc", true},
{"a*c", "abbbbd", false},
{"**", "abc", true},
{"a**c", "abc", true},
// The pattern a naive recursive matcher takes exponential time on.
// Here it is a correctness case; the cost claim is in the README.
{"a*a*a*a*a*a*b", "aaaaaaaaaaaaaaaaaaaaaaaa", false},
{"a*a*a*a*a*a*b", "aaaaaaaaaaaaaaaaaaaaaaab", true},
}
for _, tc := range cases {
got := Match(tc.pattern, tc.name)
uassert.Equal(t, tc.want, got, "Match("+tc.pattern+", "+tc.name+")")
}
}
func TestMatchClass(t *testing.T) {
cases := []struct {
pattern string
name string
want bool
}{
{"[abc]", "b", true},
{"[abc]", "d", false},
{"[a-z]", "q", true},
{"[a-z]", "Q", false},
{"[a-zA-Z0-9]", "7", true},
{"[!a-z]", "Q", true},
{"[!a-z]", "q", false},
{"[^a-z]", "Q", true},
{"g1[a-z0-9]*", "g1manfred", true},
{"g1[a-z0-9]*", "g1MANFRED", false},
// A '-' at either end of a class is a literal, not a range.
{"[-a]", "-", true},
{"[a-]", "-", true},
{"[a-]", "a", true},
{"[a-]", "b", false},
// A ']' inside a class has to be escaped; there is no POSIX
// leading-']' rule here.
{`[\]]`, "]", true},
{`[\]]`, "x", false},
// A class never matches an empty name: it consumes exactly one byte.
{"[abc]", "", false},
}
for _, tc := range cases {
got := Match(tc.pattern, tc.name)
uassert.Equal(t, tc.want, got, "Match("+tc.pattern+", "+tc.name+")")
}
}
func TestMatchEscape(t *testing.T) {
cases := []struct {
pattern string
name string
want bool
}{
{`\*`, "*", true},
{`\*`, "abc", false},
{`a\*b`, "a*b", true},
{`a\*b`, "axb", false},
{`\?`, "?", true},
{`\?`, "a", false},
{`\[a]`, "[a]", true},
{`\\`, `\`, true},
}
for _, tc := range cases {
got := Match(tc.pattern, tc.name)
uassert.Equal(t, tc.want, got, "Match("+tc.pattern+", "+tc.name+")")
}
}
func TestValid(t *testing.T) {
cases := []struct {
pattern string
want bool
}{
{"", true},
{"abc", true},
{"a*b?c", true},
{"[a-z]", true},
{"[!a-z]", true},
{`[\]]`, true},
{`a\*b`, true},
{"**/*.gno", true},
// Unterminated class.
{"[a-z", false},
{"[", false},
// Empty class: '[]' is not a class, and '[!]' negates nothing.
{"[]", false},
{"[!]", false},
// Dangling escape, including one that eats the closing bracket.
{`abc\`, false},
{`[a\`, false},
}
for _, tc := range cases {
uassert.Equal(t, tc.want, Valid(tc.pattern), "Valid("+tc.pattern+")")
}
}
// TestInvalidPatternMatchesNothing pins the decision that replaces an error
// return: Match has no way to report a bad pattern, so a bad pattern is inert
// rather than permissive. The failure mode of the alternative is an allowlist
// entry with a typo that matches everything.
func TestInvalidPatternMatchesNothing(t *testing.T) {
for _, pattern := range []string{"[a-z", "[]", `abc\`} {
uassert.False(t, Match(pattern, "abc"), "Match("+pattern+", abc)")
uassert.False(t, Match(pattern, ""), "Match("+pattern+", empty)")
uassert.False(t, MatchPath(pattern, "abc"), "MatchPath("+pattern+", abc)")
}
}
func TestMatchPathSingleStarStopsAtSeparator(t *testing.T) {
cases := []struct {
pattern string
name string
want bool
}{
{"*", "abc", true},
{"*", "a/b", false},
{"?", "/", false},
{"a/*", "a/b", true},
{"a/*", "a/b/c", false},
{"a/*/c", "a/b/c", true},
{"a/*/c", "a/c", false},
{"[a-z]/x", "q/x", true},
{"[a-z]/x", "q/y", false},
// The allowlist case this package exists for.
{"gno.land/r/moul/*", "gno.land/r/moul/home", true},
{"gno.land/r/moul/*", "gno.land/r/moul/x/daily", false},
{"gno.land/r/moul/*", "gno.land/r/other/home", false},
}
for _, tc := range cases {
got := MatchPath(tc.pattern, tc.name)
uassert.Equal(t, tc.want, got, "MatchPath("+tc.pattern+", "+tc.name+")")
}
}
func TestMatchPathDoubleStarCrossesSeparator(t *testing.T) {
cases := []struct {
pattern string
name string
want bool
}{
{"**", "", true},
{"**", "a", true},
{"**", "a/b/c", true},
{"a/**", "a/b", true},
{"a/**", "a/b/c/d", true},
{"a/**", "b/c", false},
// A '**' segment matches zero segments too, which is the case a
// segment-by-segment walk gets wrong.
{"a/**/b", "a/b", true},
{"a/**/b", "a/x/b", true},
{"a/**/b", "a/x/y/b", true},
{"a/**/b", "a/x/y/c", false},
{"**/c", "a/b/c", true},
{"**/c", "c", true},
{"**/c", "a/b/d", false},
{"gno.land/r/moul/**", "gno.land/r/moul/x/daily/globdemo", true},
// '**' only crosses as a whole segment. Glued to anything else it is
// an ordinary star and stays inside its own segment.
{"a/**x/b", "a/y/x/b", false},
{"a/**x/b", "a/yx/b", true},
}
for _, tc := range cases {
got := MatchPath(tc.pattern, tc.name)
uassert.Equal(t, tc.want, got, "MatchPath("+tc.pattern+", "+tc.name+")")
}
}
// TestMatchPathIsNotMatch pins the one difference a caller is most likely to
// assume away: the same pattern and name answer differently in the two modes.
func TestMatchPathIsNotMatch(t *testing.T) {
uassert.True(t, Match("gno.land/*", "gno.land/r/moul/home"), "Match crosses")
uassert.False(t, MatchPath("gno.land/*", "gno.land/r/moul/home"), "MatchPath does not")
}
func TestFilter(t *testing.T) {
names := []string{"alpha.gno", "beta.gno", "alpha_test.gno", "README.md"}
got := Filter("*.gno", names)
uassert.Equal(t, 3, len(got))
uassert.Equal(t, "alpha.gno", got[0])
uassert.Equal(t, "beta.gno", got[1])
uassert.Equal(t, "alpha_test.gno", got[2])
got = Filter("*_test.gno", names)
uassert.Equal(t, 1, len(got))
uassert.Equal(t, "alpha_test.gno", got[0])
// No match returns an empty slice, never nil, so a caller can range and
// len it without a guard.
got = Filter("*.txt", names)
uassert.Equal(t, 0, len(got))
uassert.True(t, got != nil, "Filter returns a non-nil empty slice")
// An invalid pattern filters everything out rather than letting it through.
uassert.Equal(t, 0, len(Filter("[a-z", names)))
}
func TestFilterPath(t *testing.T) {
paths := []string{
"gno.land/r/moul/home",
"gno.land/r/moul/x/daily/globdemo",
"gno.land/p/moul/x/daily/glob",
"gno.land/r/other/home",
}
got := FilterPath("gno.land/r/moul/*", paths)
uassert.Equal(t, 1, len(got))
uassert.Equal(t, "gno.land/r/moul/home", got[0])
got = FilterPath("gno.land/r/moul/**", paths)
uassert.Equal(t, 2, len(got))
uassert.Equal(t, "gno.land/r/moul/home", got[0])
uassert.Equal(t, "gno.land/r/moul/x/daily/globdemo", got[1])
got = FilterPath("gno.land/*/moul/**", paths)
uassert.Equal(t, 3, len(got))
}
func TestAny(t *testing.T) {
patterns := []string{"*.gno", "*.md"}
uassert.True(t, Any(patterns, "a.gno"))
uassert.True(t, Any(patterns, "README.md"))
uassert.False(t, Any(patterns, "a.txt"))
uassert.False(t, Any(nil, "a.gno"), "no pattern matches nothing")
// One bad entry does not make the whole list permissive, and does not
// disable the good entries either.
uassert.True(t, Any([]string{"[a-z", "*.gno"}, "a.gno"))
uassert.False(t, Any([]string{"[a-z"}, "a.gno"))
}
func TestAnyPath(t *testing.T) {
allow := []string{
"gno.land/r/moul/home",
"gno.land/r/moul/x/daily/**",
}
uassert.True(t, AnyPath(allow, "gno.land/r/moul/home"))
uassert.True(t, AnyPath(allow, "gno.land/r/moul/x/daily/globdemo"))
uassert.False(t, AnyPath(allow, "gno.land/r/moul/blog"))
// The prefix trap a hand-rolled HasPrefix allowlist walks into: a longer
// sibling name is not a child path.
uassert.False(t, AnyPath(allow, "gno.land/r/moul/homeless"))
uassert.False(t, AnyPath(allow, "gno.land/r/moul/x/dailyevil/thing"))
}
// TestStarIsGreedyButBacktracks is the property a single-backtrack-point
// matcher has to get right: the first star takes everything it can, then gives
// bytes back one at a time until the tail fits.
func TestStarIsGreedyButBacktracks(t *testing.T) {
uassert.True(t, Match("*.gno", "a.b.c.gno"))
uassert.True(t, Match("*ab*", "xxabyy"))
uassert.True(t, Match("*ab*cd", "xxabyycd"))
uassert.False(t, Match("*ab*cd", "xxabyycde"))
uassert.True(t, Match("a*b*c*d", "abcd"))
uassert.True(t, Match("a*b*c*d", "a1b2c3d"))
uassert.False(t, Match("a*b*c*d", "a1b2c3"))
}
// TestMatchPathSeparatorInsidePatternSyntax pins the one place the two layers
// of this package can disagree: a '/' that is escaped, or that sits inside a
// character class, belongs to its segment and must not split the pattern.
// Splitting there cuts an escape pair or a bracket expression in half, and the
// matcher then reads past the end of the piece it was handed.
//
// No name segment can contain a separator, so such a pattern matches nothing.
// The requirement is that it answers rather than aborts: a Render that panics
// on a stored pattern is a page that is broken for good.
func TestMatchPathSeparatorInsidePatternSyntax(t *testing.T) {
cases := []struct {
pattern string
name string
}{
{`a\/b`, "ax/b"},
{`a\/b`, "a/b"},
{`a\/b`, "ab"},
{`a\/b`, ""},
{"[/]", "x/y"},
{"[/]", "x"},
{"[a-/]", "x"},
{`**/a\/b`, "x/y"},
{`a\/b/c`, "a/b/c"},
}
for _, tc := range cases {
uassert.False(t, MatchPath(tc.pattern, tc.name),
"MatchPath("+tc.pattern+", "+tc.name+")")
}
// FilterPath and AnyPath split the same way and must be total too.
uassert.Equal(t, 0, len(FilterPath(`a\/b`, []string{"ax/b", "a/b"})))
uassert.Equal(t, 0, len(FilterPath("[/]", []string{"x/y", "x"})))
uassert.False(t, AnyPath([]string{"[/]", `a\/b`}, "x/y"))
// A separator that is neither escaped nor in a class still splits, and a
// class holding one still works on the segment it belongs to.
uassert.True(t, MatchPath(`a\*b/c`, "a*b/c"), `MatchPath(a\*b/c, a*b/c)`)
uassert.True(t, MatchPath("[abc]/x", "b/x"), "MatchPath([abc]/x, b/x)")
// A negated class holding the separator is the one shape here that does
// match: it is a single segment meaning "any byte but a separator", which
// is what every name segment is.
uassert.True(t, MatchPath("[!/]", "x"), "MatchPath([!/], x)")
}
gnomod.toml
module = "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/glob/v0" gno = "0.9" [addpkg] creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv" height = 751709 max_deposit = "12000000ugnot"