Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
Add ability to compile a
Pattern
once and use it for subsequent matches.Note this is currently a naive implementation that just calls the existing
Match()
, I wanted to firstly gauge interest of the maintainers to see if this is a feature worth implementing, and also to discuss what the various exported identifiers should be.Why
ValidatePattern()
, allows the user to inspect whether there is an error before saving the pattern for later. This goes a step further, and removes theerror
from the analogousMatch()
function, since it cannot fail once compiled, making it a bit cleaner to use.Additionally, presumably a compiled pattern could be made to be more efficient, though I haven't dug into the implementation there yet. My naive implementation is very slightly better than Match(), since it makes use of the existing MatchUnvalidated, which skips some validation and therefore saves time.
I modeled the methods and signatures after regexp in go's standard library (quoted in part here):
https://pkg.go.dev/regexp
Usage
Testing
The compiled version is run through the same suite of tests as the normal version, which also serves to make sure they are in lock-step.
References
Closes: #85