[Issue 5977] New: String splitting with empty separator

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue May 10 16:34:55 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5977

           Summary: String splitting with empty separator
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: patch
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2011-05-10 16:30:55 PDT ---
This D2 program seems to go in infinte loop (dmd 2.053beta):


import std.string;
void main() {
    split("a test", "");
}

------------------------

My suggestion is to add code like this in std.array.split():

if (delim.length == 0)
    return split(s);

This means that en empty splitting string is like splitting on generic
whitespace. This is useful in code like:

auto foo(string txt, string delim="") {
    return txt.split(delim);
}


This means that calling foo with no arguments splits txt on whitespace,
otherwise splits on the given string. This allows to use the two forms of split
in foo() without if conditions. This is done in Python too, where None is used
instead of an empty string.


The modified split is something like (there is a isSomeString!S2 because are
special, they aren't generic arrays, splitting on whitespace is meaningful for
strings only):


Unqual!(S1)[] split(S1, S2)(S1 s, S2 delim)
if (isForwardRange!(Unqual!S1) && isForwardRange!S2)
{
    Unqual!S1 us = s;
    if (isSomeString!S2 && delim.length == 0)
    {
        return split(s);
    }
    else
    {
        auto app = appender!(Unqual!(S1)[])();
        foreach (word; std.algorithm.splitter(us, delim))
        {
            app.put(word);
        }
        return app.data;
    }
}


Beside this change, I presume std.algorithm.splitter() too needs to test for an
empty delim.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list