Skip to content

Commit

Permalink
ARROW-17370: [C++] Add limit to SplitString() (apache#13833)
Browse files Browse the repository at this point in the history
Authored-by: Sutou Kouhei <[email protected]>
Signed-off-by: Sutou Kouhei <[email protected]>
  • Loading branch information
kou authored Aug 12, 2022
1 parent d11a553 commit a70908d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
9 changes: 7 additions & 2 deletions cpp/src/arrow/util/string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,16 @@ Status ParseHexValue(const char* data, uint8_t* out) {

namespace internal {

std::vector<util::string_view> SplitString(util::string_view v, char delimiter) {
std::vector<util::string_view> SplitString(util::string_view v, char delimiter,
int64_t limit) {
std::vector<util::string_view> parts;
size_t start = 0, end;
while (true) {
end = v.find(delimiter, start);
if (limit > 0 && static_cast<size_t>(limit - 1) <= parts.size()) {
end = std::string::npos;
} else {
end = v.find(delimiter, start);
}
parts.push_back(v.substr(start, end - start));
if (end == std::string::npos) {
break;
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/arrow/util/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ namespace internal {

/// \brief Split a string with a delimiter
ARROW_EXPORT
std::vector<util::string_view> SplitString(util::string_view v, char delim);
std::vector<util::string_view> SplitString(util::string_view v, char delim,
int64_t limit = 0);

/// \brief Join strings with a delimiter
ARROW_EXPORT
Expand Down
26 changes: 26 additions & 0 deletions cpp/src/arrow/util/string_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,31 @@ TEST(SplitString, OnlyDemiliter) {
EXPECT_EQ(parts[1], "");
}

TEST(SplitString, Limit) {
std::string input = "a:b:c";
auto parts = SplitString(input, ':', 2);
ASSERT_EQ(parts.size(), 2);
EXPECT_EQ(parts[0], "a");
EXPECT_EQ(parts[1], "b:c");
}

TEST(SplitString, LimitOver) {
std::string input = "a:b:c";
auto parts = SplitString(input, ':', 4);
ASSERT_EQ(parts.size(), 3);
EXPECT_EQ(parts[0], "a");
EXPECT_EQ(parts[1], "b");
EXPECT_EQ(parts[2], "c");
}

TEST(SplitString, LimitZero) {
std::string input = "a:b:c";
auto parts = SplitString(input, ':', 0);
ASSERT_EQ(parts.size(), 3);
EXPECT_EQ(parts[0], "a");
EXPECT_EQ(parts[1], "b");
EXPECT_EQ(parts[2], "c");
}

} // namespace internal
} // namespace arrow

0 comments on commit a70908d

Please sign in to comment.