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