Skip to content

Commit

Permalink
fix a bug when matching with empty strings
Browse files Browse the repository at this point in the history
  • Loading branch information
matyalatte committed Jul 5, 2024
1 parent 61a0d57 commit 2e64692
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/re.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ int re_matchp(re_t pattern, const char* text, int* matchlength) {

do {
if (matchpattern(pattern, text, rune_size, matchlength)) {
if (text[0] == '\0')
return -1;
// Maybe we don't need this
// if (text[0] == '\0')
// return -1;

return (int)(text - prepoint);
}
Expand Down
19 changes: 17 additions & 2 deletions tests/re_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ INSTANTIATE_TEST_SUITE_P(RegexTestInstantiation_Escaped,
// https://github.com/python/cpython/blob/main/Lib/test/re_tests.py
const RegexCase regex_cases_python[] = {
// { ")", "", TSM_SYNTAX_ERROR }, // () operator does not supported yet.
// { "", "", TSM_OK }, // this fails somehow
{ "", "", TSM_OK }, // this fails somehow
{ "abc", "abc", TSM_OK },
{ "abc", "xbc", TSM_FAIL },
{ "abc", "axc", TSM_FAIL },
Expand Down Expand Up @@ -197,7 +197,7 @@ const RegexCase regex_cases_python[] = {
{ "a+b+c", "aabbabc", TSM_OK },
{ "[^ab]*", "cde", TSM_OK },
{ "abc", "", TSM_FAIL },
// { "a*", "", TSM_OK }, // This fails somehow
{ "a*", "", TSM_OK }, // This fails somehow
{ "abcd*efg", "abcdefg", TSM_OK },
{ "ab*", "xabyabbbz", TSM_OK },
{ "ab*", "xayabbbz", TSM_OK },
Expand All @@ -222,6 +222,21 @@ INSTANTIATE_TEST_SUITE_P(RegexTestInstantiation_Python,
RegexTest,
::testing::ValuesIn(regex_cases_python));

// Original test cases.
const RegexCase regex_cases_tsm[] = {
{ "^[a-zA-Z_][z-zA-Z0-9_]*", "a", TSM_OK },
{ "^[a-zA-Z_][z-zA-Z0-9_]*", "abcd1234_", TSM_OK },
{ "^[a-zA-Z_][z-zA-Z0-9_]*", "1", TSM_FAIL },
{ "^[a-zA-Z_][z-zA-Z0-9_]*", "1bcd1234_", TSM_FAIL },
{ ".", "", TSM_FAIL },
{ "^.$", "", TSM_FAIL },
{ ".*", "", TSM_OK },
};

INSTANTIATE_TEST_SUITE_P(RegexTestInstantiation_Tsm,
RegexTest,
::testing::ValuesIn(regex_cases_tsm));

TEST_P(RegexTest, tsm_regex_match) {
const RegexCase test_case = GetParam();
int actual = tsm_regex_match(test_case.pattern, test_case.str);
Expand Down

0 comments on commit 2e64692

Please sign in to comment.