From 1913620f37c686e9a605ea343f9d9a0dd6ce0af9 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Thu, 4 Feb 2016 23:30:49 +0100 Subject: [PATCH] integrate new stringify() variant and add test coverage ...also for the existing variant, which packages an arbitrary number of arguments in stringified form into a given container type. Moreover, the new form of stringify allows to write util::join in a clearer way, eliminating the lambda. --- src/lib/format-util.hpp | 20 +++++++++++++++----- tests/library/format-helper-test.cpp | 16 ++++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/lib/format-util.hpp b/src/lib/format-util.hpp index 5af1f0efa..b70bc0b5c 100644 --- a/src/lib/format-util.hpp +++ b/src/lib/format-util.hpp @@ -121,6 +121,20 @@ namespace util { return CON {move(storage)}; } + /** convert to string as transforming step in a pipeline + * @param src a "Lumiera Forward Iterator" with arbitrary result type + * @return a "Lumiera Forward Iterator" with string elements + * @see FormatHelper_test::checkStringify() + */ + template + inline lib::TransformIter + stringify (IT const& src) + { + using Val = typename IT::value_type; + + return lib::transformIterator(src, util::toString); + } + namespace { // helper to build range iterator on demand @@ -175,13 +189,9 @@ namespace util { join (CON&& coll, string const& delim =", ") { using Coll = typename lib::meta::Strip::TypePlain; - using Val = typename Coll::value_type; - - std::function toString = [] (Val const& val) { return util::toString(val); }; - _RangeIter range(std::forward(coll)); - auto strings = lib::transformIterator(range.iter, toString); + auto strings = stringify (range.iter); if (!strings) return ""; std::ostringstream buffer; diff --git a/tests/library/format-helper-test.cpp b/tests/library/format-helper-test.cpp index 1b4075d8d..753baebcb 100644 --- a/tests/library/format-helper-test.cpp +++ b/tests/library/format-helper-test.cpp @@ -42,14 +42,6 @@ using std::to_string; namespace util { - template - inline lib::TransformIter - stringify (IT const& src) - { - using Val = typename IT::value_type; - - return transformIterator(src, util::toString); - } namespace test { namespace { // test fixture... @@ -141,6 +133,7 @@ namespace test { void checkStringify() { + // use as transformer within an (iterator) pipeline auto ss = stringify (eachNum (1.11, 10.2)); CHECK (ss); @@ -153,6 +146,13 @@ namespace test { res += s; CHECK (res == "..2.113.114.115.116.117.118.119.1110.11"); + + + using VecS = vector; + + // another variant: collect arbitrary number of arguments + VecS vals = stringify (short(12), 345L, "67", '8'); + CHECK (vals == VecS({"12", "345", "67", "8"})); }