From 47126086215fae10ff16c5be390d876db3aedf15 Mon Sep 17 00:00:00 2001 From: Francesco Chemolli <5175948+kinkie@users.noreply.github.com> Date: Thu, 11 Apr 2024 14:22:46 +0000 Subject: [PATCH] Add AsList::quoted() (#1779) --- src/base/IoManip.h | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/base/IoManip.h b/src/base/IoManip.h index 3983c79664..b62865d49d 100644 --- a/src/base/IoManip.h +++ b/src/base/IoManip.h @@ -187,32 +187,57 @@ public: /// a c-string to print between consecutive items (if any). Caller must ensure lifetime. auto &delimitedBy(const char * const d) { delimiter = d; return *this; } + /// c-string to print before and after each item. Caller must ensure lifetime. + auto "ed(const char * const q = "\"") { preQuote = postQuote = q; return *this; } + + /// c-strings to print before and after each item. Caller must ensure lifetime. + auto "ed(const char * const preQ, const char * const postQ) { preQuote = preQ; postQuote = postQ; return *this; } + + /// writes the container to the given stream + void print(std::ostream &) const; + public: const Container &container; ///< zero or more items to print const char *prefix = nullptr; ///< \copydoc prefixedBy() const char *suffix = nullptr; ///< \copydoc suffixedBy() const char *delimiter = nullptr; ///< \copydoc delimitedBy() + const char *preQuote = nullptr; ///< optional c-string to print before each item; \sa quoted() + const char *postQuote = nullptr; ///< optional c-string to print after each item; \sa quoted() }; -template -inline std::ostream & -operator <<(std::ostream &os, const AsList &manipulator) +template +void +AsList::print(std::ostream &os) const { bool opened = false; - for (const auto &item: manipulator.container) { + + for (const auto &item: container) { if (!opened) { - if (manipulator.prefix) - os << manipulator.prefix; + if (prefix) + os << prefix; opened = true; } else { - if (manipulator.delimiter) - os << manipulator.delimiter; + if (delimiter) + os << delimiter; } + + if (preQuote) + os << preQuote; os << item; + if (postQuote) + os << postQuote; } - if (opened && manipulator.suffix) - os << manipulator.suffix; + + if (opened && suffix) + os << suffix; +} + +template +inline std::ostream & +operator <<(std::ostream &os, const AsList &manipulator) +{ + manipulator.print(os); return os; } -- 2.39.5