use lazyregexp to compile regexes on first use#48166
Conversation
|
Ah, need a rebase for |
1ab6ddf to
d20a37c
Compare
|
Discussing in the maintainers call; one downside of this is that mistakes in regular expressions may now result in a panic at runtime. Such mistakes should of course be revealed as part of tests (i.e. if it doesn't trigger in tests, there's no coverage of the regular suppression 😇), so we were discussing approaches for having a Linter verify if any While discussing that @corhere round an interesting package in golang.org/x/mod that seems to handle most of this, but it's internal, so we'd have to copy it. We could rewrite parts of it to us it has a special case when running under test |
|
The special case when running under test is much simpler to achieve with Go 1.21: https://pkg.go.dev/testing#Testing |
|
Oh! Just after I mentioned "I wish stdlib has something for this" 😂 |
|
Yet another alternative to this is https://github.com/containers/storage/tree/main/pkg/regexp (godoc: https://pkg.go.dev/github.com/containers/storage/pkg/regexp). In particular, it solves the issue of checking all regexes by using a build tag ( |
Ah, yes, that's an option. I think I want to avoid special build-tags though, and it looks like the Looking at the Lazyregexp approach, the only thing I'm not super fond of is that it's also wrapping the Regexp; I gave it a try to continue using a |
d20a37c to
474ecba
Compare
laxyregexp wrapping the Regexp is one of the things I liked the most about it! Wrapping makes it a near drop-in replacement for Thoughts on adding a forbidigo lint rule to block direct uses of |
Yeah, that's a good point, and I was going back-and-forth a bit on that. It's nice for it to be a drop-in replacement, but I was also somewhat concerned on that requiring us to keep it in sync with any changes / new features that may be introduced in Implementing all of those would not be too complicated, so if we want it to be a full drop-in replacement, I can have a look of that, but input welcome. |
|
BTW it probably doesn't make any sense to touch any One other thing is, some uses of regular expressions (in non-test code) may and should be eliminated, as NOT using regexp usually results in faster code and smaller binaries. Yet the task is somewhat hard; for a few examples, see opencontainers/runc#3460 and the linked PRs/CLs. |
Implementing only a subset of
It's in an internal package. We could trivially add wrapper methods on demand, as needed, just like golang.org/x/mod's lazyregexp package. Though I doubt it would take much time to whip up a full (minus methods which mutate the compiled regexp) wrapper with a bit of editor-fu. |
474ecba to
924a6d1
Compare
f291414 to
c25d705
Compare
c25d705 to
9d79563
Compare
| ) | ||
|
|
||
| var stripOSC = regexp.MustCompile(`\x1b\][^\x1b\a]*(\x1b\\|\a)`) | ||
| var stripOSC = lazyregexp.New(`\x1b\][^\x1b\a]*(\x1b\\|\a)`) |
There was a problem hiding this comment.
- This one won't be needed if the workaround is removed in vendor: github.com/Azure/go-ansiterm faa5f7b0171c, remove workaround for OSC string terminator parsing #49195
| func (r *Regexp) ReplaceAllLiteralString(src, repl string) string { | ||
| return r.re().ReplaceAllLiteralString(src, repl) | ||
| } |
There was a problem hiding this comment.
This one was added for that fix as well, so we could drop it if that one's no longer needed (it's the only place this one was used currently).
|
@corhere @kolyshkin I updated this PR to use a fork of the lazyregexp package from golang.org/x/mod, and added a Linter rule to check for |
9d79563 to
6e362d4
Compare
Based on the "lazyregexp" package in golang.org/x/mod; https://cs.opensource.google/go/x/mod/+/refs/tags/v0.19.0:internal/lazyregexp/lazyre.go;l=66-78 This package allows defining regular expressions that should not be compiled until used, but still providing validation to prevent invalid regular expressions from producing a panic at runtime. This is largely a copy of the package from golang.org/x/mod, with FindAllStringSubmatch and ReplaceAllStringFunc added Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
6e362d4 to
e451b69
Compare
|
Rebased after #49195 was merged (dropped one commit, and removed the now unused |
implement lazyregexp package based on the "lazyregexp" package in golang.org/x/mod;
https://cs.opensource.google/go/x/mod/+/refs/tags/v0.19.0:internal/lazyregexp/lazyre.go;l=66-78
This package allows defining regular expressions that should not be
compiled until used, but still providing validation to prevent
invalid regular expressions from producing a panic at runtime.
This is largely a copy of the package from golang.org/x/mod,
with FindAllStringSubmatch and ReplaceAllLiteralString,
and ReplaceAllStringFunc added
- A picture of a cute animal (not mandatory but encouraged)