Skip to content

Commit d0c52ea

Browse files
committed
simplify matchplus and matchstar
`matchplus` can be simplified by only modifying `matchlength` once the complete match is successful. This means it doesn't have to rewind `matchlength` as it iterates through each possible `matchpattern`. This also means it keeps `matchlength` unmodified if it doesn't return a match. Because of this last part, this also means that `matchstar` can leverage `matchplus` which reduces it to single line of code `return matchplus(...) || matchpattern(..)`.
1 parent 711981b commit d0c52ea

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

re.c

+5-21
Original file line numberDiff line numberDiff line change
@@ -399,22 +399,7 @@ static int matchone(regex_t p, char c)
399399

400400
static int matchstar(regex_t p, regex_t* pattern, const char* text, int* matchlength)
401401
{
402-
int prelen = *matchlength;
403-
const char* prepoint = text;
404-
while ((text[0] != '\0') && matchone(p, *text))
405-
{
406-
text++;
407-
(*matchlength)++;
408-
}
409-
while (text >= prepoint)
410-
{
411-
if (matchpattern(pattern, text--, matchlength))
412-
return 1;
413-
(*matchlength)--;
414-
}
415-
416-
*matchlength = prelen;
417-
return 0;
402+
return matchplus(p, pattern, text, matchlength) || matchpattern(pattern, text, matchlength);
418403
}
419404

420405
static int matchplus(regex_t p, regex_t* pattern, const char* text, int* matchlength)
@@ -423,15 +408,14 @@ static int matchplus(regex_t p, regex_t* pattern, const char* text, int* matchle
423408
while ((text[0] != '\0') && matchone(p, *text))
424409
{
425410
text++;
426-
(*matchlength)++;
427411
}
428-
while (text > prepoint)
412+
for (; text > prepoint; text--)
429413
{
430-
if (matchpattern(pattern, text--, matchlength))
414+
if (matchpattern(pattern, text, matchlength)) {
415+
*matchlength += text - prepoint;
431416
return 1;
432-
(*matchlength)--;
417+
}
433418
}
434-
435419
return 0;
436420
}
437421

0 commit comments

Comments
 (0)