Skip to content

Commit

Permalink
Simplify code.
Browse files Browse the repository at this point in the history
Co-authored-by: <[email protected]>
  • Loading branch information
serhiy-storchaka committed Nov 18, 2024
1 parent 169aed1 commit 273e2d1
Showing 1 changed file with 18 additions and 36 deletions.
54 changes: 18 additions & 36 deletions Modules/_sre/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1302,30 +1302,21 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", pattern,
ptr, pattern[1], pattern[2]));

/* Create repeat context.
* It is only needed to set state->repeat to non-NULL, so nested
* BRANCH will be able to restore marks. */
if (state->repeat == NULL) {
/* See comment in SRE_OP_REPEAT about potential memory leak. */
ctx->u.rep = (SRE_REPEAT*) PyObject_Malloc(sizeof(*ctx->u.rep));
if (!ctx->u.rep) {
PyErr_NoMemory();
RETURN_FAILURE;
}
ctx->u.rep->count = -1;
ctx->u.rep->pattern = NULL;
ctx->u.rep->prev = state->repeat;
ctx->u.rep->last_ptr = NULL;
state->repeat = ctx->u.rep;
}
else {
ctx->u.rep = NULL;
}

/* Set the global Input pointer to this context's Input
pointer */
state->ptr = ptr;

/* Set state->repeat to non-NULL */
ctx->u.rep = repeat_pool_malloc(state);
if (!ctx->u.rep) {
RETURN_ERROR(SRE_ERROR_MEMORY);
}
ctx->u.rep->count = -1;
ctx->u.rep->pattern = NULL;
ctx->u.rep->prev = state->repeat;
ctx->u.rep->last_ptr = NULL;
state->repeat = ctx->u.rep;

/* Initialize Count to 0 */
ctx->count = 0;

Expand All @@ -1334,18 +1325,15 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
/* not enough matches */
DO_JUMP0(JUMP_POSS_REPEAT_1, jump_poss_repeat_1,
&pattern[3]);
if (ret <= 0) { // error or failure
if (ctx->u.rep && !ctx->u.rep->pattern) {
state->repeat = ctx->u.rep->prev;
PyObject_Free(ctx->u.rep);
}
}
if (ret) {
RETURN_ON_ERROR(ret);
ctx->count++;
}
else {
state->ptr = ptr;
/* Restore state->repeat */
state->repeat = ctx->u.rep->prev;
repeat_pool_free(state, ctx->u.rep);
RETURN_FAILURE;
}
}
Expand Down Expand Up @@ -1390,12 +1378,6 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)

/* Check to see if the last attempted match
succeeded. */
if (ret < 0) { // error
if (ctx->u.rep && !ctx->u.rep->pattern) {
state->repeat = ctx->u.rep->prev;
PyObject_Free(ctx->u.rep);
}
}
if (ret) {
/* Drop the saved highest number Capture Group
marker saved above and use the newly updated
Expand Down Expand Up @@ -1423,10 +1405,10 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
break;
}
}
if (ctx->u.rep && !ctx->u.rep->pattern) {
state->repeat = ctx->u.rep->prev;
PyObject_Free(ctx->u.rep);
}

/* Restore state->repeat */
state->repeat = ctx->u.rep->prev;
repeat_pool_free(state, ctx->u.rep);

/* Evaluate Tail */
/* Jump to end of pattern indicated by skip, and then skip
Expand Down

0 comments on commit 273e2d1

Please sign in to comment.