mijin2/source/mijin/util/string.hpp
Patrick Wuttke a1f1717e22 Added ProcessStream to for running processes (on Linux) and streaming their output.
Added map() function for creating a mapping iterator.
Added mijin::pipe types for map() and join().
2023-08-06 13:52:51 +02:00

69 lines
1.1 KiB
C++

#pragma once
#if !defined(MIJIN_UTIL_STRING_HPP_INCLUDED)
#define MIJIN_UTIL_STRING_HPP_INCLUDED 1
#include <iterator>
#include <sstream>
#include <string>
namespace mijin
{
//
// public defines
//
//
// public constants
//
//
// public types
//
//
// public functions
//
template <typename TRange, typename TValue = typename TRange::value_type>
std::string join(const TRange& elements, const char* const delimiter)
{
std::ostringstream oss;
auto first = std::begin(elements);
auto last = std::end(elements);
if (first != last)
{
std::copy(first, std::prev(last), std::ostream_iterator<TValue>(oss, delimiter));
first = prev(last);
}
if (first != last)
{
oss << *first;
}
return oss.str();
}
namespace pipe
{
struct Join
{
const char* delimiter;
explicit Join(const char* delimiter_) noexcept : delimiter(delimiter_) {}
};
template<typename TIterable>
auto operator|(TIterable&& iterable, const Join& joiner)
{
return join(std::forward<TIterable>(iterable), joiner.delimiter);
}
}
} // namespace mijin
#endif // !defined(MIJIN_UTIL_STRING_HPP_INCLUDED)