[Issue 8181] New: String splitting with nonempty delim produces empty result

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jun 1 13:49:45 PDT 2012


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

           Summary: String splitting with nonempty delim produces empty
                    result
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          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 2012-06-01 13:51:37 PDT ---
Splitting of an empty string according to another nonempty delim string
produces an empty array result:


import std.stdio, std.string;
void main() {
    writeln("".split("."));
}


Output:
[]


While in Python2 it produces a list (array) that contains an empty string:

>>> "".split()
[]
>>> "".split(".")
['']


In most cases such Python string methods are carefully designed.

That Python behavour is useful:

>>> file_name = "ball.jpg"
>>> file_name.split(".")[-1] == "jpg"
True
>>> file_name = ""
>>> file_name.split(".")[-1] == "jpg"
False


While in D:

import std.stdio, std.string;
void main() {
    auto file_name = "ball.jpg";
    writeln(file_name.split(".")[$-1] == "jpg"); // Output: true
    file_name = "";
    writeln(file_name.split(".")[$-1] == "jpg"); // range violation
}


Workaround:

import std.stdio, std.array;
void main() {
    auto file_name = "";
    writeln(!file_name.empty && file_name.split(".")[$-1] == "jpg");
}

-- 
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