gobwas / glob

Go glob
MIT License
954 stars 63 forks source link

Is there a way to check whether a url is valid glob pattern? #57

Open vivekprm opened 2 years ago

vivekprm commented 2 years ago

I am trying to test whether a pattern is valid glob. How can we do that?

E.g. "^register/?$" this should return false.

I was trying to test like this but getting true.

package glob

import (
    "fmt"

    "github.com/gobwas/glob/compiler"
    "github.com/gobwas/glob/syntax"
)

func isGlob (str string) bool {
    delim := []rune{','}
    if str == "" {
        return false;
    }
    ast, err := syntax.Parse(str); 
    if err != nil {
        return false
    }
    _, err := compiler.Compile(ast, delim)

    if err == nil {
        return true
    }
    return false
}
calmh commented 2 years ago

E.g. "^register/?$" this should return false.

But ^register/?$ is a valid glob pattern, it matches ^register/x$ for example... It's also a valid regexp, and as a human I can see that it's likely intended as that and not a glob pattern, but I don't think this package can know that?

vivekprm commented 2 years ago

Thanks for your response!

I was looking at performance benchmark and it seems ^ and $ are not allowed in glob patterns?

Screenshot 2022-08-29 at 8 50 43 PM

So, my question is how glob.Compile is different from regexp.Compile ?

calmh commented 2 years ago

Regexps and glob patterns are different. I'm not sure why you think ^ and $ are not allowed - they are just normal characters as far as a glob pattern is concerned.

I'm not the author of this package, by the way, just a bystander.