Repository navigation
fix: apply text replacements that overlap a match inside a word - #140
Open
RaphaelFakhri wants to merge 1 commit into
Open
RaphaelFakhri wants to merge 1 commit into
RaphaelFakhri wants to merge 1 commit into
Conversation
A literal phrase match that fails the word-boundary check consumed its span, so an overlapping whole-phrase match starting later in that span was never considered. With a rule "I I" -> "I", "Hi I I think" stayed unchanged. Resume the search one character after a rejected match.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
ReplacementSet::replacecollected candidates withRegex::find_iterand then dropped matches that fail the word-boundary check.find_iternever revisits a span it has already returned, so a rejected match inside a word hid any overlapping whole-phrase match that starts later in that span.This affects phrases whose start repeats their end, such as stutter fixes:
I I->I:Hi I I think so.stays unchanged, becausei IinsideHi Iis found first and rejected.the the->the:Breathe the the air.stays unchanged, becausethe thestarting insideBreatheis found first and rejected.The fix searches with
Regex::find_atand, after a rejected match, resumes one character after its start. Accepted matches still advance past their end, so selection, ordering, and deletion behavior are unchanged.Tests
a_match_inside_a_word_does_not_hide_an_overlapping_whole_phraseanda_rejected_match_ending_in_a_multibyte_character_resumes_on_a_character_boundaryinsrc/text_replacements.rs.left: "Hi I I think so."andleft: "Café é é.") and pass with it. All 7text_replacementstests pass.cargo fmt --checkandgit diff --checkare clean.text_replacementsis compiled only on macOS. The tests ran on Linux by compiling this exact module file in a scratch crate with a stubTextReplacementandregex = "=1.13.1", pluscargo clippy --all-targets -- -D warningson that crate. The full macOScargo testand Clippy run were not performed.Fixes #139