@@ -47,26 +47,50 @@ std::string trim_from_last_delimiter(
47
47
// / \param b: Iterator pointing to first item to print
48
48
// / \param e: Iterator pointing past last item to print
49
49
// / \param delimiter: Object to print between each item in the iterator range
50
+ // / \param transform_func: Transform to apply to the value returned by the
51
+ // / iterator
50
52
// / \return A reference to the ostream that was passed in
51
- template <typename Stream, typename It, typename Delimiter>
53
+ template <typename Stream,
54
+ typename It,
55
+ typename Delimiter,
56
+ typename TransformFunc>
52
57
Stream &join_strings (
53
- Stream &os,
58
+ Stream && os,
54
59
const It b,
55
60
const It e,
56
- const Delimiter &delimiter)
61
+ const Delimiter &delimiter,
62
+ TransformFunc &&transform_func)
57
63
{
58
64
if (b==e)
59
65
{
60
66
return os;
61
67
}
62
- os << *b ;
68
+ os << transform_func (*b) ;
63
69
for (auto it=std::next (b); it!=e; ++it)
64
70
{
65
- os << delimiter << *it;
71
+ os << delimiter << transform_func ( *it) ;
66
72
}
67
73
return os;
68
74
}
69
75
76
+ // / Prints items to an stream, separated by a constant delimiter
77
+ // / \tparam It: An iterator type
78
+ // / \tparam Delimiter: A delimiter type which supports printing to ostreams
79
+ // / \param os: An ostream to write to
80
+ // / \param b: Iterator pointing to first item to print
81
+ // / \param e: Iterator pointing past last item to print
82
+ // / \param delimiter: Object to print between each item in the iterator range
83
+ // / \return A reference to the ostream that was passed in
84
+ template <typename Stream, typename It, typename Delimiter>
85
+ Stream &
86
+ join_strings (Stream &&os, const It b, const It e, const Delimiter &delimiter)
87
+ {
88
+ using value_type = decltype (*b);
89
+ // Call auxiliary function with identity function
90
+ return join_strings (
91
+ os, b, e, delimiter, [](const value_type &x) { return x; });
92
+ }
93
+
70
94
// / Generic escaping of strings; this is not meant to be a particular
71
95
// / programming language.
72
96
std::string escape (const std::string &);
0 commit comments