valpackett / pcre-heavy

A Haskell regular expressions library that doesn't suck | now on https://codeberg.org/valpackett/pcre-heavy
https://codeberg.org/valpackett/pcre-heavy
The Unlicense
51 stars 6 forks source link

Loop in gsub #2

Closed rjmac closed 9 years ago

rjmac commented 9 years ago

gsub, when replacing, offsets after each replacement by the length of the match, not the length of the replacement, which means that if the replacement contains something that will re-match after that limit, gsub will loop forever:

λ> gsub [re|bad|] "xxbad" "this is bad, right?" -- length "xx" < length "bad", so ok
"this is xxbad, right?"
λ> gsub [re|bad|] "xxxbad" "this is bad, right?" -- length "xxx" >= length "bad", so loop
"^CInterrupted.

Similarly, it can miss targets that occur soon after a shortening replacement:

λ> gsub [re|good|] "bad" "goodgoodgood"
"badgoodbad"

The ultimate bug's in rawSub, where it returns end instead of offset + length replacement.

valpackett commented 9 years ago

Oh fuck. Thanks for noticing! Going to fix right now

valpackett commented 9 years ago

Released 0.2.3!

P.S. it was begin + length replacement, not offset + length replacement :-)