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 slice_strings to return empty strings for stop < start indices #13178

Merged
merged 3 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion cpp/src/strings/slice.cu
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct substring_from_fn {

auto const stop = stops[idx];
auto const end = (((stop < 0) || (stop > length)) ? length : stop);
return d_str.substr(start, end - start);
return start < end ? d_str.substr(start, end - start) : string_view{};
davidwendt marked this conversation as resolved.
Show resolved Hide resolved
}

substring_from_fn(column_device_view const& d_column, IndexIterator starts, IndexIterator stops)
Expand Down
18 changes: 18 additions & 0 deletions cpp/tests/strings/slice_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ TEST_P(Parameters, Substring_From)
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected);
}

TEST_P(Parameters, SubstringStopZero)
{
cudf::size_type start = GetParam();
cudf::test::strings_column_wrapper input({"abc", "défgh", "", "XYZ"});
auto view = cudf::strings_column_view(input);

auto results = cudf::strings::slice_strings(view, start, 0);
cudf::test::strings_column_wrapper expected({"", "", "", ""});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected);

auto starts =
cudf::test::fixed_width_column_wrapper<cudf::size_type>({start, start, start, start});
auto stops = cudf::test::fixed_width_column_wrapper<cudf::size_type>({0, 0, 0, 0});

results = cudf::strings::slice_strings(view, starts, stops);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*results, expected);
}

TEST_P(Parameters, AllEmpty)
{
std::vector<std::string> h_strings{"", "", "", ""};
Expand Down