Skip to content

Commit 691f592

Browse files
committed
fdsdump: common: fix and improve string_split function
The function now correctly determines delimiter size when splitting, and supports the use of the max_pieces parameter.
1 parent c18bbd7 commit 691f592

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/tools/fdsdump/src/common/common.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@
66
namespace fdsdump {
77

88
std::vector<std::string>
9-
string_split(const std::string &str, const std::string &delimiter)
9+
string_split(
10+
const std::string &str,
11+
const std::string &delimiter,
12+
unsigned int max_pieces)
1013
{
1114
std::vector<std::string> pieces;
1215
std::size_t pos = 0;
1316

1417
for (;;) {
18+
if (max_pieces > 0 && pieces.size() == max_pieces - 1) {
19+
pieces.emplace_back(str.begin() + pos, str.end());
20+
break;
21+
}
22+
1523
std::size_t next_pos = str.find(delimiter, pos);
1624
if (next_pos == std::string::npos) {
1725
pieces.emplace_back(str.begin() + pos, str.end());
1826
break;
1927
}
2028
pieces.emplace_back(str.begin() + pos, str.begin() + next_pos);
21-
pos = next_pos + 1;
29+
pos = next_pos + delimiter.size();
2230
}
2331
return pieces;
2432
}

src/tools/fdsdump/src/common/common.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ using shared_tsnapshot = std::shared_ptr<fds_tsnapshot_t>;
2727
* If no delimeter is found, the result contains only the original string.
2828
* @param str String to be splitted
2929
* @param delimiter String delimeter
30+
* @param max_pieces The maximum number of pieces (0 = no limit)
3031
* @return Vector of piecies.
3132
*/
3233
std::vector<std::string>
33-
string_split(const std::string &str, const std::string &delimiter);
34+
string_split(
35+
const std::string &str,
36+
const std::string &delimiter,
37+
unsigned int max_pieces = 0);
3438

3539
/**
3640
* @brief Split string by a delimiter from right.

0 commit comments

Comments
 (0)