Skip to content

Add a join function for strings #1450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/util/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Author: Daniel Poetzl
#ifndef CPROVER_UTIL_STRING_UTILS_H
#define CPROVER_UTIL_STRING_UTILS_H

#include <ostream>
#include <string>
#include <vector>

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

/// Prints items to an stream, separated by a constant delimiter
/// \tparam It An iterator type
/// \tparam Delimiter A delimiter type which supports printing to ostreams
/// \param os An ostream to write to
/// \param b Iterator pointing to first item to print
/// \param e Iterator pointing past last item to print
/// \param delimiter Object to print between each item in the iterator range
/// \return A reference to the ostream that was passed in
template<typename Stream, typename It, typename Delimiter>
Stream &join_strings(
Stream &os,
const It b,
const It e,
const Delimiter &delimiter)
{
if(b==e)
{
return os;
}
os << *b;
for(auto it=std::next(b); it!=e; ++it)
{
os << delimiter << *it;
}
return os;
}

#endif