Skip to content

gh-156513: Match a backreference case-insensitively as a literal - #156514

Open
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:re-foldcase
Open

gh-156513: Match a backreference case-insensitively as a literal#156514
serhiy-storchaka wants to merge 1 commit into
python:mainfrom
serhiy-storchaka:re-foldcase

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

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_CASES is 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 if sre_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.

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
serhiy-storchaka marked this pull request as ready for review August 28, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant