-
-
Notifications
You must be signed in to change notification settings - Fork 53
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
Implementing replaceAll with generated matchers? #357
Comments
Here's a demo of what I'm trying to do. Not quite happy yet...
Save that as main.go, then create match.go with something like
and run with
Currently, it outputs
Examining the generated code, fsm expands the quantifier a{2,3} to return immediately after the 2nd a, without even trying to consume a 3rd one. This is a fine optimization for detecting a match, so evidently my hack to return the end offset of the match is breaking an assumption libfsm made, and/or I'm misusing libfsm in some other way. |
I think you need to call Match again after you replace to find the next
index. Also a good idea to use a slice instead of string here so you avoid
a bunch of allocations.
Looks like you're at Fastly, so may be worth looking at the Varnish
implementation if that still exists. In particular you need to decide what
to do if the replacement results in another match.
…On Tue, Jun 1, 2021 at 20:24 dkegel-fastly ***@***.***> wrote:
I'm flailing around a bit trying to figure out how to port that regexp to
libfsm. Using
re -k str -l go -r pcre 'a{2,}[^a]' | sed 's/fsm_fsm/main/' > match.go
outputs
regexp: r.FindStringIndex returns 0, 3
fsm: Match returns 0, 3
regexp: ReplaceAllString returns b b
fsm: myReplaceAll returns b aaa
Using
re -k str -l go -r pcre 'a{2,}' | sed 's/fsm_fsm/main/' > match.go
yields
regexp: r.FindStringIndex returns 0, 3
fsm: Match returns 0, 1
regexp: ReplaceAllString returns b b
fsm: myReplaceAll returns bba bba
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#357 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABJFR36WPKJXSDQ2JZDHW3TQWB6JANCNFSM455AH5UA>
.
|
Thanks. I updated the problem description to address your last suggestion about induced matches. I am very interested in suggestions for coaxing libfsm into actually finishing the match of a{2,3} instead |
I'm trying to port a use of golang's https://golang.org/pkg/regexp/#Regexp.ReplaceAllString to tinygo, where
regexp doesn't work just yet. libfsm works nicely, i.e. I can use
to generate a nice matcher. But it isn't especially helpful for implementing ReplaceAllString, as it returns
neither the starting nor the ending index.
Returning starting and ending indices seems easy, e.g.
and would let me implement a ReplaceAllString.
Is there an easier way? I feel like I haven't yet found the appropriate doc...
The text was updated successfully, but these errors were encountered: