onetrueawk / awk

One true awk
Other
1.96k stars 156 forks source link

Fix fnematch. #213

Closed arnoldrobbins closed 8 months ago

arnoldrobbins commented 8 months ago

Use sane code after finding the end of the match to put back the last rune read and terminate the buffer. Fixes https://github.com/onetrueawk/awk/issues/212.

millert commented 8 months ago

Thanks @arnoldrobbins for this. BTW, you don't really need a new loop variable for this, you could just operate directly on r.len since you are headed for return regardless.

mpinjr commented 8 months ago

It's incorrect to assume that there will be only one rune's worth of bytes to put back. If the automaton went looking for a longer match, failed to find it, and settled for a shorter one, there could be much more after the match.

$ printf aaaZZZbbb | ./correct.out '{print} END {print NR}' RS='ZZZ(bbbb)?'
aaa
bbb
2
$ printf aaaZZZbbb | ./broken.out '{print} END {print NR}' RS='ZZZ(bbbb)?'
aaa
1
arnoldrobbins commented 8 months ago

@mpinjr I see. If your PR handles this then I'm OK with going with it. @plan9 You should probably take his PR and close mine.