Skip to content

pcre2_substitute: reset \Q literal mode for each replacement - #1065

Merged
NWilson merged 1 commit into
PCRE2Project:mainfrom
b84330808:substitute-literal-mode-reset
Nov 6, 2026
Merged

NWilson merged 1 commit into
PCRE2Project:mainfrom
b84330808:substitute-literal-mode-reset

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

In pcre2_substitute() with PCRE2_SUBSTITUTE_EXTENDED, a \Q not closed by \E stayed set for the next match in global mode, so /x/g,replace=\Qabc on xx gave abc\Qabc. The flag now lives inside the global loop, next to the case-forcing state. Tests are in testinput2 and testoutput2.

Verified: RunTest.py test 2 on Windows on all widths with JIT, and RunTest on Linux. Not available: the devcontainer presets, Valgrind, ManyConfigTests, MSVC, 32-bit, EBCDIC, autotools.

With PCRE2_SUBSTITUTE_EXTENDED, a \Q that is not closed by \E puts the rest
of the replacement in literal mode. The flag was declared outside the global
substitution loop, so it stayed set for the replacement of the next match, and
that replacement was copied as literal text, \Q included:

  /x/g,replace=\Qabc,substitute_extended
      xx    gave abc\Qabc, not abcabc

Declare the flag inside the loop, next to the case-forcing state, which is
already reset for each match.

Tests were added to testinput2 and testoutput2.
@NWilson

NWilson commented Nov 5, 2026

Copy link
Copy Markdown
Member

Thank you! I will review this soon. It certainly looks correct on the surface, and fixes a bug.

@NWilson NWilson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it! I think it's correct. Thank you.

@NWilson
NWilson merged commit 70277eb into PCRE2Project:main Nov 6, 2026
36 of 42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants