Skip to content

Commit 0346f87

Browse files
committed
Add a join function for strings
1 parent 22a68fe commit 0346f87

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/util/string_utils.h

+28
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Author: Daniel Poetzl
1010
#ifndef CPROVER_UTIL_STRING_UTILS_H
1111
#define CPROVER_UTIL_STRING_UTILS_H
1212

13+
#include <ostream>
1314
#include <string>
1415
#include <vector>
1516

@@ -33,4 +34,31 @@ std::string trim_from_last_delimiter(
3334
const std::string &s,
3435
const char delim);
3536

37+
/// Prints items to an stream, separated by a constant delimiter
38+
/// \tparam It An iterator type
39+
/// \tparam Delimiter A delimiter type which supports printing to ostreams
40+
/// \param os An ostream to write to
41+
/// \param b Iterator pointing to first item to print
42+
/// \param e Iterator pointing past last item to print
43+
/// \param delimiter Object to print between each item in the iterator range
44+
/// \return A reference to the ostream that was passed in
45+
template<typename Stream, typename It, typename Delimiter>
46+
Stream &join_strings(
47+
Stream &os,
48+
const It b,
49+
const It e,
50+
const Delimiter &delimiter)
51+
{
52+
if(b==e)
53+
{
54+
return os;
55+
}
56+
os << *b;
57+
for(auto it=std::next(b); it!=e; ++it)
58+
{
59+
os << delimiter << *it;
60+
}
61+
return os;
62+
}
63+
3664
#endif

0 commit comments

Comments
 (0)