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.
This commit is contained in:
Fischlurch 2016-02-04 23:30:49 +01:00
parent 2cb1ea6920
commit 1913620f37
2 changed files with 23 additions and 13 deletions

View file

@ -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<class IT>
inline lib::TransformIter<IT, string>
stringify (IT const& src)
{
using Val = typename IT::value_type;
return lib::transformIterator(src, util::toString<Val>);
}
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<CON>::TypePlain;
using Val = typename Coll::value_type;
std::function<string(Val const&)> toString = [] (Val const& val) { return util::toString(val); };
_RangeIter<Coll> range(std::forward<CON>(coll));
auto strings = lib::transformIterator(range.iter, toString);
auto strings = stringify (range.iter);
if (!strings) return "";
std::ostringstream buffer;

View file

@ -42,14 +42,6 @@ using std::to_string;
namespace util {
template<class IT>
inline lib::TransformIter<IT, string>
stringify (IT const& src)
{
using Val = typename IT::value_type;
return transformIterator(src, util::toString<Val>);
}
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<string>;
// another variant: collect arbitrary number of arguments
VecS vals = stringify<VecS> (short(12), 345L, "67", '8');
CHECK (vals == VecS({"12", "345", "67", "8"}));
}