gh-91524: Speed up the regular expression substitution (#91525)

Functions re.sub() and re.subn() and corresponding re.Pattern methods
are now 2-3 times faster for replacement strings containing group references.

Closes #91524

Primarily authored by serhiy-storchaka Serhiy Storchaka
Minor-cleanups-by: Gregory P. Smith [Google] <greg@krypto.org>
This commit is contained in:
Serhiy Storchaka 2022-10-24 01:57:30 +03:00 committed by GitHub
parent 176b6c57be
commit 75a6fadf36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 358 additions and 91 deletions

View file

@ -52,6 +52,17 @@ typedef struct {
Py_ssize_t mark[1];
} MatchObject;
typedef struct {
PyObject_VAR_HEAD
Py_ssize_t chunks; /* the number of group references and non-NULL literals
* self->chunks <= 2*Py_SIZE(self) + 1 */
PyObject *literal;
struct {
Py_ssize_t index;
PyObject *literal; /* NULL if empty */
} items[0];
} TemplateObject;
typedef struct SRE_REPEAT_T {
Py_ssize_t count;
const SRE_CODE* pattern; /* points to REPEAT operator arguments */