Conversation
…-char escapes
QRegexSearch::SubstituteByPosition() built the replacement by re-running the
find pattern against just the matched substring in isolation:
QString newString = match.captured();
newString.replace(match.regularExpression(), QByteArray(text, *length));
This broke in two ways. First, \r, \n, \t and \\ in the replacement text
were never translated to real characters, so e.g. replacing "Example" with
"\0\r\n" produced the literal text "\0\r\n" instead of the matched text
followed by a real line break. Second, re-matching the pattern against the
isolated substring silently gave wrong results for anything relying on
context outside the match itself, such as lookahead/lookbehind or anchors.
Rewrite it to build the replacement directly from the already-computed
match's captured groups (\0 for the whole match, \1-\9 for capture groups,
matching Notepad++'s own replace syntax) plus the same \n \r \t \\ escapes
the "Extended" search mode already supports, so regex replace has them too.
Add a QTest suite (tests/tst_QRegexSearch.cpp) covering whole-match
substitution, reordered capture groups, the literal-text case, and a
lookbehind pattern that the old implementation got wrong. Wire tests/ into
CMake and run them via ctest in CI on every push.
Closes dail8859#1100
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.
QRegexSearch::SubstituteByPosition() built the replacement by re-running the find pattern against just the matched substring in isolation:
This broke in two ways. First, \r, \n, \t and \ in the replacement text were never translated to real characters, so e.g. replacing "Example" with "\0\r\n" produced the literal text "\0\r\n" instead of the matched text followed by a real line break. Second, re-matching the pattern against the isolated substring silently gave wrong results for anything relying on context outside the match itself, such as lookahead/lookbehind or anchors.
Rewrite it to build the replacement directly from the already-computed match's captured groups (\0 for the whole match, \1-\9 for capture groups, matching Notepad++'s own replace syntax) plus the same \n \r \t \ escapes the "Extended" search mode already supports, so regex replace has them too.
Add a QTest suite (tests/tst_QRegexSearch.cpp) covering whole-match substitution, reordered capture groups, the literal-text case, and a lookbehind pattern that the old implementation got wrong. Wire tests/ into CMake and run them via ctest in CI on every push.
Closes #1100