[Issue 20184] String maxsplit

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Sep 2 06:56:02 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=20184

Alex <sascha.orlov at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sascha.orlov at gmail.com

--- Comment #4 from Alex <sascha.orlov at gmail.com> ---
As a workaround, this is possible: 

´´´
import std;

void main()
{
    "one two three four".fun1(1).writeln; 
    "one two three four".fun2(2).writeln; 
}

auto fun1(string s, size_t num)
{
    size_t summe; 
    auto r = s.splitter(' ').take(num).tee!(a => summe += a.length + 1).array;  
    return r ~ s[summe .. $];
}

auto fun2(string s, size_t num)
{
    auto i = s.splitter(' ').take(num);
    return i.array ~ s[i.map!(el => el.length).sum + num .. $];
}
´´´

If the splitter construct allowed public access to its underlying range, more
convenient solutions were possible.

--


More information about the Digitalmars-d-bugs mailing list