Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regex handling of fixed quantifier with 0 range #17067

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions cpp/src/strings/regex/regcomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,7 @@ class regex_parser {
std::stack<int> lbra_stack;
auto repeat_start_index = -1;

for (std::size_t index = 0; index < in.size(); index++) {
auto const item = in[index];

for (auto const item : in) {
if (item.type != COUNTED && item.type != COUNTED_LAZY) {
out.push_back(item);
if (item.type == LBRA || item.type == LBRA_NC) {
Expand All @@ -739,7 +737,7 @@ class regex_parser {
auto const m = item.d.count.m; // maximum count
assert(n >= 0 && "invalid repeat count value n");
// zero-repeat edge-case: need to erase the previous items
if (n == 0 && m == 0) { out.erase(begin, end); }
if (n == 0) { out.erase(begin, end); }

std::vector<regex_parser::Item> repeat_copy(begin, end);
// special handling for quantified capture groups
Expand Down
34 changes: 34 additions & 0 deletions cpp/tests/strings/contains_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,40 @@ TEST_F(StringsContainsTests, FixedQuantifier)
}
}

TEST_F(StringsContainsTests, ZeroRangeQuantifier)
{
auto input = cudf::test::strings_column_wrapper({"a", "", "abc", "XYAZ", "ABC", "ZYXA"});
auto sv = cudf::strings_column_view(input);

auto pattern = std::string("A{0,}"); // should match everyting
auto prog = cudf::strings::regex_program::create(pattern);

{
auto expected = cudf::test::fixed_width_column_wrapper<bool>({1, 1, 1, 1, 1, 1});
auto results = cudf::strings::contains_re(sv, *prog);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected);
}
{
auto expected = cudf::test::fixed_width_column_wrapper<cudf::size_type>({2, 1, 4, 5, 4, 5});
auto results = cudf::strings::count_re(sv, *prog);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected);
}

pattern = std::string("(?:ab){0,3}");
prog = cudf::strings::regex_program::create(pattern);

{
auto expected = cudf::test::fixed_width_column_wrapper<bool>({1, 1, 1, 1, 1, 1});
auto results = cudf::strings::contains_re(sv, *prog);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected);
}
{
auto expected = cudf::test::fixed_width_column_wrapper<cudf::size_type>({2, 1, 3, 5, 4, 5});
auto results = cudf::strings::count_re(sv, *prog);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected);
}
}

TEST_F(StringsContainsTests, NestedQuantifier)
{
auto input = cudf::test::strings_column_wrapper({"TEST12 1111 2222 3333 4444 5555",
Expand Down
28 changes: 28 additions & 0 deletions cpp/tests/strings/replace_regex_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,34 @@ TEST_F(StringsReplaceRegexTest, ZeroLengthMatch)
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*results, expected);
}

TEST_F(StringsReplaceRegexTest, ZeroRangeQuantifier)
{
auto input = cudf::test::strings_column_wrapper({"a", "", "123", "XYAZ", "abc", "zéyab"});
auto sv = cudf::strings_column_view(input);

auto pattern = std::string("A{0,5}");
auto prog = cudf::strings::regex_program::create(pattern);
auto repl = cudf::string_scalar("_");
auto expected = cudf::test::strings_column_wrapper(
{"_a_", "_", "_1_2_3_", "_X_Y__Z_", "_a_b_c_", "_z_é_y_a_b_"});
auto results = cudf::strings::replace_re(sv, *prog, repl);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected);

pattern = std::string("[a0-9]{0,2}");
prog = cudf::strings::regex_program::create(pattern);
expected =
cudf::test::strings_column_wrapper({"__", "_", "___", "_X_Y_A_Z_", "__b_c_", "_z_é_y__b_"});
results = cudf::strings::replace_re(sv, *prog, repl);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected);

pattern = std::string("(?:ab){0,3}");
prog = cudf::strings::regex_program::create(pattern);
expected =
cudf::test::strings_column_wrapper({"_a_", "_", "_1_2_3_", "_X_Y_A_Z_", "__c_", "_z_é_y__"});
results = cudf::strings::replace_re(sv, *prog, repl);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected);
}

TEST_F(StringsReplaceRegexTest, Multiline)
{
auto const multiline = cudf::strings::regex_flags::MULTILINE;
Expand Down
Loading