Skip to content

Commit

Permalink
simplify matchplus and matchstar
Browse files Browse the repository at this point in the history
  • Loading branch information
matyalatte committed Jul 4, 2024
1 parent ba559b4 commit d95b944
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/re.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,20 +343,7 @@ static int matchone(regex_t p, char c) {
}

static int matchstar(regex_t p, regex_t* pattern, const char* text, int* matchlength) {
int prelen = *matchlength;
const char* prepoint = text;
while ((text[0] != '\0') && matchone(p, *text)) {
text++;
(*matchlength)++;
}
while (text >= prepoint) {
if (matchpattern(pattern, text--, matchlength))
return 1;
(*matchlength)--;
}

*matchlength = prelen;
return 0;
return matchplus(p, pattern, text, matchlength) || matchpattern(pattern, text, matchlength);
}

static int matchplus(regex_t p, regex_t* pattern, const char* text, int* matchlength) {
Expand All @@ -365,10 +352,11 @@ static int matchplus(regex_t p, regex_t* pattern, const char* text, int* matchle
text++;
(*matchlength)++;
}
while (text > prepoint) {
if (matchpattern(pattern, text--, matchlength))
for (; text > prepoint; text--) {
if (matchpattern(pattern, text, matchlength)) {
*matchlength += text - prepoint;
return 1;
(*matchlength)--;
}
}

return 0;
Expand Down

0 comments on commit d95b944

Please sign in to comment.