Skip to content

Commit

Permalink
Fix slice_strings to return empty strings for stop < start indices (#…
Browse files Browse the repository at this point in the history
…13178)

Fixes bug where `stop` value is less than `start` value in calls to `cudf::strings::slice_strings` should result in an empty string. Optimization in #13057 introduced this bug. Additional gtest was added to check for this condition.

Close #13173

Authors:
  - David Wendt (https://github.com/davidwendt)

Approvers:
  - Jason Lowe (https://github.com/jlowe)
  - Nghia Truong (https://github.com/ttnghia)
  - MithunR (https://github.com/mythrocks)

URL: #13178
  • Loading branch information
davidwendt authored Apr 22, 2023
1 parent bccf3ab commit 097d6f2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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{};
}

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

0 comments on commit 097d6f2

Please sign in to comment.