How to make rsplit (like in Python) in D

Uranuz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Oct 3 12:25:59 PDT 2016


On Saturday, 1 October 2016 at 18:55:54 UTC, pineapple wrote:
> On Saturday, 1 October 2016 at 17:55:08 UTC, Uranuz wrote:
>> On Saturday, 1 October 2016 at 17:32:59 UTC, Uranuz wrote:
>>> On Saturday, 1 October 2016 at 17:23:16 UTC, Uranuz wrote:
>>>> [...]
>>>
>>> But these example fails. Oops. Looks like a bug(
>>>
>>> import std.stdio;
>>> import std.algorithm;
>>> import std.range;
>>> import std.string;
>>>
>>> [...]
>>
>> I created bug report on this:
>> https://issues.dlang.org/show_bug.cgi?id=16569
>
> This isn't a bug. It's illegal to access the front or back of 
> an empty range. (If anything is a bug, it's the 
> nondescriptiveness of the error.) You should write this instead:
>
> void main()
> {
>     string str = "";
>     auto split = str.splitter('.');
>     if(!split.empty) writeln(split.back);
> }

When I pass empty string to splitter in most of languages I 
expect to get list with 1 item (empty string) as a result, but I 
get error instead. And I see inconsistency in that .front behaves 
normally, but .back is not. Usually I access front of range 
directly without any check when I expect it to have exactly 1 
item. But in this case it not working and is very strange.


More information about the Digitalmars-d-learn mailing list