Add template for generic container element join

This commit is contained in:
2025-08-08 10:48:33 +02:00
parent 855a8208a1
commit 505f801b7c
3 changed files with 18 additions and 11 deletions

View File

@@ -12,7 +12,7 @@ PRODUCT := lib
# Single source of truth for version.
MAJOR := 1
MINOR := 4
MINOR := 5
PATCH := 0
# Specify desired C++ standard for this project.

View File

@@ -26,20 +26,14 @@ vector<string> sc::split(const string& str, const string& sep)
return components;
}
vector<string> sc::split(const string& str, const regex& sep) {
vector<string> sc::split(const string& str, const regex& sep)
{
return {sregex_token_iterator {str.begin(), str.end(), sep, -1}, sregex_token_iterator {}};
}
string sc::join(const vector<string>& components, const string& join)
{
string result;
for (vector<string>::const_iterator i = components.cbegin(); i != components.cend(); ++i) {
if (i != components.cbegin()) {
result += join;
}
result += *i;
}
return result;
return sc::join<vector<string>::const_iterator>(components.cbegin(), components.cend(), join);
}
string sc::trim(const string& str, const string& del)

View File

@@ -22,9 +22,22 @@ namespace sc {
std::vector<std::string> split (const std::string& str,
const std::regex& sep);
template<typename It>
std::string join (It beg, It end, const std::string& join)
{
std::string result;
for (auto i = beg; i != end; ++i) {
if (i != beg) {
result += join;
}
result += *i;
}
return result;
}
std::string join (const std::vector<std::string>& components,
const std::string& join);
std::string trim (const std::string& str,
const std::string& del = " ");