Skip to content
Open
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
4 changes: 2 additions & 2 deletions cpp/src/arrow/compute/kernels/scalar_string_ascii.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2940,7 +2940,7 @@ struct SplitWhitespaceAsciiFinder : public StringSplitFinderBase<SplitOptions> {
*separator_begin = i;
do {
i++;
} while (IsSpaceCharacterAscii(*i) && i < end);
} while (i < end && IsSpaceCharacterAscii(*i));
*separator_end = i;
return true;
}
Expand All @@ -2958,7 +2958,7 @@ struct SplitWhitespaceAsciiFinder : public StringSplitFinderBase<SplitOptions> {
*separator_end = i + 1;
do {
i--;
} while (IsSpaceCharacterAscii(*i) && i >= begin);
} while (i >= begin && IsSpaceCharacterAscii(*i));
*separator_begin = i + 1;
return true;
}
Expand Down
71 changes: 71 additions & 0 deletions cpp/src/arrow/compute/kernels/scalar_string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,25 @@ TYPED_TEST(TestBaseBinaryKernels, SplitMax) {
R"([["foo", "bar"], ["foo"], ["foo---bar", "", "ar"]])", &options_reverse);
}

template <typename TestType>
std::shared_ptr<Array> MakeArrayInExactBuffer(const std::vector<std::string>& values,
const std::string& prefix = "",
const std::string& suffix = "") {
using offset_type = typename TypeTraits<TestType>::OffsetType::c_type;
std::string data = prefix;
std::vector<offset_type> offsets = {static_cast<offset_type>(data.size())};
for (const auto& value : values) {
data += value;
offsets.push_back(static_cast<offset_type>(data.size()));
}
data += suffix;
return MakeArray(ArrayData::Make(
TypeTraits<TestType>::type_singleton(), static_cast<int64_t>(values.size()),
{nullptr, Buffer::FromVector(std::move(offsets)),
Buffer::FromVector(std::vector<uint8_t>(data.begin(), data.end()))},
/*null_count=*/0));
}

TYPED_TEST(TestStringKernels, SplitWhitespaceAscii) {
SplitOptions options;
SplitOptions options_max{1};
Expand All @@ -1986,6 +2005,16 @@ TYPED_TEST(TestStringKernels, SplitWhitespaceAsciiReverse) {
&options_max);
}

TYPED_TEST(TestStringKernels, SplitWhitespaceAsciiStaysInBounds) {
SplitOptions options;
SplitOptions options_reverse{-1, /*reverse=*/true};
auto input = MakeArrayInExactBuffer<TypeParam>({" foo ", " bar "});
this->CheckUnary("ascii_split_whitespace", input, list(this->type()),
R"([["", "foo", ""], ["", "bar", ""]])", &options);
this->CheckUnary("ascii_split_whitespace", input, list(this->type()),
R"([["", "foo", ""], ["", "bar", ""]])", &options_reverse);
}

#ifdef ARROW_WITH_UTF8PROC
TYPED_TEST(TestStringKernels, SplitWhitespaceUTF8) {
SplitOptions options;
Expand All @@ -2011,6 +2040,48 @@ TYPED_TEST(TestStringKernels, SplitWhitespaceUTF8Reverse) {
"[[\"foo\", \"bar\"], [\"foo\xe2\x80\x88 bar\", \"ba\"]]",
&options_max);
}

TYPED_TEST(TestStringKernels, SplitWhitespaceUTF8LeadingTrailing) {
SplitOptions options;
SplitOptions options_max{1};
SplitOptions options_reverse{-1, /*reverse=*/true};
SplitOptions options_reverse_max{1, /*reverse=*/true};
// \xe2\x80\x88 is punctuation space
const char* input =
"[\"foo \", \" foo\", \" foo \", \" \", \" \", \"\", "
"\"foo\xe2\x80\x88\xe2\x80\x88\", \"\xe2\x80\x88 foo\"]";
this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
R"([["foo", ""], ["", "foo"], ["", "foo", ""], ["", ""], ["", ""],
[""], ["foo", ""], ["", "foo"]])",
&options);
this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
R"([["foo", ""], ["", "foo"], ["", "foo "], ["", ""], ["", ""],
[""], ["foo", ""], ["", "foo"]])",
&options_max);
this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
R"([["foo", ""], ["", "foo"], ["", "foo", ""], ["", ""], ["", ""],
[""], ["foo", ""], ["", "foo"]])",
&options_reverse);
this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
R"([["foo", ""], ["", "foo"], [" foo", ""], ["", ""], ["", ""],
[""], ["foo", ""], ["", "foo"]])",
&options_reverse_max);
}

TYPED_TEST(TestStringKernels, SplitWhitespaceUTF8StaysInBounds) {
SplitOptions options;
SplitOptions options_reverse{-1, /*reverse=*/true};
auto input = MakeArrayInExactBuffer<TypeParam>({" foo ", " bar "}, "\xff", "\xff");
this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
R"([["", "foo", ""], ["", "bar", ""]])", &options);
this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
R"([["", "foo", ""], ["", "bar", ""]])", &options_reverse);
input = MakeArrayInExactBuffer<TypeParam>({" foo ", " bar "});
this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
R"([["", "foo", ""], ["", "bar", ""]])", &options);
this->CheckUnary("utf8_split_whitespace", input, list(this->type()),
R"([["", "foo", ""], ["", "bar", ""]])", &options_reverse);
}
#endif

#ifdef ARROW_WITH_RE2
Expand Down
20 changes: 14 additions & 6 deletions cpp/src/arrow/compute/kernels/scalar_string_utf8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1381,12 +1381,16 @@ struct SplitWhitespaceUtf8Finder : public StringSplitFinderBase<SplitOptions> {
return false;
}
if (IsSpaceCharacterUnicode(codepoint)) {
do {
*separator_end = i;
*separator_end = i;
while (i < end) {
if (ARROW_PREDICT_FALSE(!arrow::util::UTF8Decode(&i, &codepoint))) {
return false;
}
} while (IsSpaceCharacterUnicode(codepoint) && i < end);
if (!IsSpaceCharacterUnicode(codepoint)) {
break;
}
*separator_end = i;
}
return true;
}
}
Expand All @@ -1404,12 +1408,16 @@ struct SplitWhitespaceUtf8Finder : public StringSplitFinderBase<SplitOptions> {
return false;
}
if (IsSpaceCharacterUnicode(codepoint)) {
do {
*separator_begin = i + 1;
*separator_begin = i + 1;
while (i >= begin) {
if (ARROW_PREDICT_FALSE(!arrow::util::UTF8DecodeReverse(&i, &codepoint))) {
return false;
}
} while (IsSpaceCharacterUnicode(codepoint) && i >= begin);
if (!IsSpaceCharacterUnicode(codepoint)) {
break;
}
*separator_begin = i + 1;
}
return true;
}
}
Expand Down