Attempt to fix gsub with zero length matches #2564
Closed
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.
Problem: If a regex given to gsub can match a zero length string, it will never terminate and eventually crash. This is because after matching once it keeps checking for the same match at the same location and finding it.
Solution: We can't skip ahead because there might be a non-zero length match at the same location. Instead, keep track of whether the last match was zero length, and when it was, ignore any zero length match at the same offset.
Remaining problem: This fails one of the new tests I added. For some reason, match doesn't seem to correctly return (some) zero length matches at the end of the input string. I'm going to report that as a separate bug, I can't see why it behaves like that.
Ref: #2148