Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vivekprm opened this issue Aug 29, 2022 · 3 comments
Open

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

vivekprm opened this issue Aug 29, 2022 · 3 comments

Comments

@vivekprm
Copy link

vivekprm commented Aug 29, 2022

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
Copy link
Contributor

calmh commented Aug 29, 2022

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
Copy link
Author

vivekprm commented Aug 29, 2022

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
Copy link
Contributor

calmh commented Aug 29, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants