gh-156513: Match a backreference case-insensitively as a literal - #156514
Open
serhiy-storchaka wants to merge 1 commit into
Open
gh-156513: Match a backreference case-insensitively as a literal#156514serhiy-storchaka wants to merge 1 commit into
serhiy-storchaka wants to merge 1 commit into
Conversation
sre_lower_unicode() now returns the lowercase of the simple case folding of the character, computed by the new _PyUnicode_ToFoldedLowercase(), with the four pairs which no case mapping unifies hardcoded. The key is the same for all characters matched case-insensitively, so a backreference matches what a literal matches: (?i)(.)\1 matches 'ςσ' as (?i)ς already matched 'σ'. _casefix._EXTRA_CASES is no longer needed and is removed, together with the alternatives which the compiler expanded a literal into. Its generator now emits no table, but keeps computing the groups of characters which have to be matched case-insensitively, and fails if sre_lower_unicode() does not fold such a group to a single code. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
serhiy-storchaka
force-pushed
the
re-foldcase
branch
from
August 28, 2026 12:59
64ca418 to
3995544
Compare
serhiy-storchaka
marked this pull request as ready for review
August 28, 2026 13:39
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.
Implements the three steps proposed in the issue.
The four pairs which no case mapping unifies are hardcoded as special cases in
sre_lower_unicode()._PyUnicode_ToFoldedLowercase()combines the simple case folding and the lowercase in one function. It is added for performance: it reads the type record once, except for the few characters having a special single-character folding._casefix._EXTRA_CASESis removed, together with the alternatives which the compiler expanded a literal into. Its generator keeps computing the groups of characters which have to be matched case-insensitively, and now fails ifsre_lower_unicode()does not fold such a group to a single code.The set of characters matched case-insensitively does not change: the equivalence classes computed from
_sre.unicode_tolower()are the same as before for every code point.