Skip to content

Commit

Permalink
Add interleaveComma function from LLVM
Browse files Browse the repository at this point in the history
  • Loading branch information
raccog committed Nov 24, 2023
1 parent 821dde4 commit a49b855
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 18 deletions.
63 changes: 63 additions & 0 deletions src/STLExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,69 @@ auto find_if_not(R &&Range, UnaryPredicate P) {
return std::find_if_not(adl_begin(Range), adl_end(Range), P);
}

/// An STL-style algorithm similar to std::for_each that applies a second
/// functor between every pair of elements.
///
/// This provides the control flow logic to, for example, print a
/// comma-separated list:
/// \code
/// interleave(names.begin(), names.end(),
/// [&](StringRef name) { os << name; },
/// [&] { os << ", "; });
/// \endcode
template <typename ForwardIterator, typename UnaryFunctor,
typename NullaryFunctor,
typename = std::enable_if_t<
!std::is_constructible<std::string_view, UnaryFunctor>::value &&
!std::is_constructible<std::string_view, NullaryFunctor>::value>>
inline void interleave(ForwardIterator begin, ForwardIterator end,
UnaryFunctor each_fn, NullaryFunctor between_fn) {
if (begin == end)
return;
each_fn(*begin);
++begin;
for (; begin != end; ++begin) {
between_fn();
each_fn(*begin);
}
}

template <typename Container, typename UnaryFunctor, typename NullaryFunctor,
typename = std::enable_if_t<
!std::is_constructible<std::string_view, UnaryFunctor>::value &&
!std::is_constructible<std::string_view, NullaryFunctor>::value>>
inline void interleave(const Container &c, UnaryFunctor each_fn,
NullaryFunctor between_fn) {
interleave(c.begin(), c.end(), each_fn, between_fn);
}

/// Overload of interleave for the common case of string separator.
template <typename Container, typename UnaryFunctor, typename StreamT,
typename T = detail::ValueOfRange<Container>>
inline void interleave(const Container &c, StreamT &os, UnaryFunctor each_fn,
const std::string_view &separator) {
interleave(c.begin(), c.end(), each_fn, [&] { os << separator; });
}
template <typename Container, typename StreamT,
typename T = detail::ValueOfRange<Container>>
inline void interleave(const Container &c, StreamT &os,
const std::string_view &separator) {
interleave(
c, os, [&](const T &a) { os << a; }, separator);
}

template <typename Container, typename UnaryFunctor, typename StreamT,
typename T = detail::ValueOfRange<Container>>
inline void interleaveComma(const Container &c, StreamT &os,
UnaryFunctor each_fn) {
interleave(c, os, each_fn, ", ");
}
template <typename Container, typename StreamT,
typename T = detail::ValueOfRange<Container>>
inline void interleaveComma(const Container &c, StreamT &os) {
interleaveComma(c, os, [&](const T &a) { os << a; });
}

} // namespace llvm

#endif // LLVM_ADT_STLEXTRAS_H
27 changes: 9 additions & 18 deletions src/cli/clioptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <QFile>
#include <QMetaEnum>

#include "isa/rv64isainfo.h"
#include "STLExtras.h"

namespace Ripes {

Expand Down Expand Up @@ -186,23 +186,14 @@ bool parseCLIOptions(QCommandLineParser &parser, QString &errorMessage,
errorMessage = "Invalid register file type '" + regFile +
"' specified (--reginit). Valid types for '" +
parser.value("proc") + "' with extensions [";
for (auto iter = options.isaExtensions.begin();
iter != options.isaExtensions.end();) {
errorMessage += *iter;
++iter;
if (iter != options.isaExtensions.end()) {
errorMessage += ", ";
}
}
errorMessage += "]: [";
for (auto iter = fileNames.begin(); iter != fileNames.end();) {
errorMessage += *iter;
++iter;
if (iter != fileNames.end()) {
errorMessage += ", ";
}
}
errorMessage += "]";
std::stringstream extInfo;
std::string isaExtensions =
options.isaExtensions.join("").toStdString();
llvm::interleaveComma(isaExtensions, extInfo);
extInfo << "]: [";
llvm::interleaveComma(fileNames, extInfo);
extInfo << "]";
errorMessage += extInfo.str();
return false;
}

Expand Down

0 comments on commit a49b855

Please sign in to comment.