Add template for generic container element join
This commit is contained in:
@@ -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.
|
||||||
|
@@ -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)
|
||||||
|
@@ -22,9 +22,22 @@ 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);
|
||||||
|
|
||||||
std::string trim (const std::string& str,
|
std::string trim (const std::string& str,
|
||||||
const std::string& del = " ");
|
const std::string& del = " ");
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user