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

Add a string_view variant of base::split_string() #117

Merged
merged 1 commit into from
Dec 5, 2024
Merged
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
43 changes: 40 additions & 3 deletions base/split_string.cpp
Original file line number Diff line number Diff line change
@@ -13,12 +13,10 @@
#include <algorithm>

namespace {

struct is_separator {
const std::string* separators;

is_separator(const std::string* seps) : separators(seps) {
}
is_separator(const std::string* seps) : separators(seps) {}

bool operator()(std::string::value_type chr) const
{
@@ -32,6 +30,22 @@ namespace {
}
};

struct is_separator_view {
const std::string_view* separators;
ckaiser marked this conversation as resolved.
Show resolved Hide resolved

is_separator_view(const std::string_view* seps) : separators(seps) {}

bool operator()(std::string_view::value_type chr) const
{
for (std::string_view::const_iterator
it = separators->begin(),
end = separators->end(); it != end; ++it) {
if (chr == *it)
return true;
}
return false;
}
};
}

void base::split_string(const std::string& string,
@@ -56,3 +70,26 @@ void base::split_string(const std::string& string,
}
}
}

void base::split_string(const std::string_view& string,
std::vector<std::string_view>& parts,
const std::string_view& separators)
{
const std::size_t elements =
1 + std::count_if(string.begin(), string.end(),
is_separator_view(&separators));
parts.reserve(elements);

std::size_t beg = 0, end;
while (true) {
end = string.find_first_of(separators, beg);
if (end != std::string::npos) {
parts.push_back(string.substr(beg, end - beg));
beg = end + 1;
}
else {
parts.push_back(string.substr(beg));
break;
}
}
}
5 changes: 4 additions & 1 deletion base/split_string.h
Original file line number Diff line number Diff line change
@@ -9,14 +9,17 @@
#pragma once

#include <string>
#include <string_view>
#include <vector>

namespace base {

void split_string(const std::string& string,
std::vector<std::string>& parts,
const std::string& separators);

void split_string(const std::string_view& string,
std::vector<std::string_view>& parts,
const std::string_view& separators);
}

#endif
40 changes: 40 additions & 0 deletions base/split_string_tests.cpp
Original file line number Diff line number Diff line change
@@ -38,7 +38,47 @@ TEST(SplitString, OneSeparator)

TEST(SplitString, MultipleSeparators)
{
std::string string = "Hello,World";
ckaiser marked this conversation as resolved.
Show resolved Hide resolved
std::vector<std::string> result;
base::split_string(string, result, ",r");
ASSERT_EQ(3, result.size());
EXPECT_EQ("Hello", result[0]);
EXPECT_EQ("Wo", result[1]);
EXPECT_EQ("ld", result[2]);
}

TEST(SplitStringView, Empty)
{
const std::string_view string = "";
ckaiser marked this conversation as resolved.
Show resolved Hide resolved
ckaiser marked this conversation as resolved.
Show resolved Hide resolved
std::vector<std::string_view> result;
base::split_string("", result, ",");
ASSERT_EQ(1, result.size());
EXPECT_EQ("", result[0]);
}

TEST(SplitStringView, NoSeparator)
{
const std::string_view string = "Hello,World";
std::vector<std::string_view> result;
base::split_string(string, result, "");
ASSERT_EQ(1, result.size());
EXPECT_EQ("Hello,World", result[0]);
}

TEST(SplitStringView, OneSeparator)
{
const std::string_view string = "Hello,World";
std::vector<std::string_view> result;
base::split_string("Hello,World", result, ",");
ASSERT_EQ(2, result.size());
EXPECT_EQ("Hello", result[0]);
EXPECT_EQ("World", result[1]);
}

TEST(SplitStringView, MultipleSeparators)
{
const std::string_view string = "Hello,World";
std::vector<std::string_view> result;
base::split_string("Hello,World", result, ",r");
ASSERT_EQ(3, result.size());
EXPECT_EQ("Hello", result[0]);