std.range.byLine

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 10 15:45:06 PDT 2014


On Wednesday, 10 September 2014 at 22:29:55 UTC, Ali Çehreli 
wrote:
>     assert("foo\nbar\n"
>            .splitter(newline)
>            .filter!(a => !a.empty)
>            .equal([ "foo", "bar" ]));
> }
>
> Ali

Ok, great.

So I got.

auto byLine(Range)(Range input) if (isForwardRange!Range)
{
     import std.algorithm: splitter;
     import std.ascii: newline;
     static if (newline.length == 1)
     {
         return input.splitter(newline.front);
     }
     else
     {
         return input.splitter(newline);
     }
}

unittest
{
     import std.algorithm: equal;
     assert(equal("a\nb".byLine, ["a", "b"]));
}

One thing still:

Is my optimization for newline.length == 1 unnecessary or perhaps 
even wrong?


More information about the Digitalmars-d-learn mailing list