mirror of
https://github.com/Kitware/CMake.git
synced 2026-05-03 21:00:01 -05:00
cmStringAlgorithms: Simplify cmJoin using cm::string_view
This commit is contained in:
+10
-19
@@ -7,8 +7,6 @@
|
|||||||
|
|
||||||
#include "cmRange.h"
|
#include "cmRange.h"
|
||||||
#include "cm_string_view.hxx"
|
#include "cm_string_view.hxx"
|
||||||
#include <algorithm>
|
|
||||||
#include <iterator>
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -32,31 +30,24 @@ private:
|
|||||||
std::string const Test_;
|
std::string const Test_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Joins elements of a range with separator into a single string. */
|
||||||
template <typename Range>
|
template <typename Range>
|
||||||
std::string cmJoin(Range const& r, const char* delimiter)
|
std::string cmJoin(Range const& rng, cm::string_view separator)
|
||||||
{
|
{
|
||||||
if (r.empty()) {
|
if (rng.empty()) {
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
typedef typename Range::value_type ValueType;
|
auto it = rng.begin();
|
||||||
typedef typename Range::const_iterator InputIt;
|
auto const end = rng.end();
|
||||||
const InputIt first = r.begin();
|
os << *it;
|
||||||
InputIt last = r.end();
|
while (++it != end) {
|
||||||
--last;
|
os << separator << *it;
|
||||||
std::copy(first, last, std::ostream_iterator<ValueType>(os, delimiter));
|
}
|
||||||
|
|
||||||
os << *last;
|
|
||||||
|
|
||||||
return os.str();
|
return os.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Range>
|
|
||||||
std::string cmJoin(Range const& r, std::string const& delimiter)
|
|
||||||
{
|
|
||||||
return cmJoin(r, delimiter.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename Range>
|
template <typename Range>
|
||||||
std::string cmWrap(std::string const& prefix, Range const& r,
|
std::string cmWrap(std::string const& prefix, Range const& r,
|
||||||
std::string const& suffix, std::string const& sep)
|
std::string const& suffix, std::string const& sep)
|
||||||
|
|||||||
Reference in New Issue
Block a user