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. # Single source of truth for version.
MAJOR := 1 MAJOR := 1
MINOR := 4 MINOR := 5
PATCH := 0 PATCH := 0
# Specify desired C++ standard for this project. # 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; 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 {}}; return {sregex_token_iterator {str.begin(), str.end(), sep, -1}, sregex_token_iterator {}};
} }
string sc::join(const vector<string>& components, const string& join) string sc::join(const vector<string>& components, const string& join)
{ {
string result; return sc::join<vector<string>::const_iterator>(components.cbegin(), components.cend(), join);
for (vector<string>::const_iterator i = components.cbegin(); i != components.cend(); ++i) {
if (i != components.cbegin()) {
result += join;
}
result += *i;
}
return result;
} }
string sc::trim(const string& str, const string& del) string sc::trim(const string& str, const string& del)

View File

@@ -22,6 +22,19 @@ namespace sc {
std::vector<std::string> split (const std::string& str, std::vector<std::string> split (const std::string& str,
const std::regex& sep); 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, std::string join (const std::vector<std::string>& components,
const std::string& join); const std::string& join);