split with delimiter

Brad Anderson eco at gnuk.net
Thu Feb 16 17:00:42 PST 2012


On Friday, 17 February 2012 at 00:47:35 UTC, bioinfornatics wrote:
> Le vendredi 17 février 2012 à 01:33 +0100, bioinfornatics a 
> écrit :
>> reading
>> http://www.d-programming-language.org/phobos/std_array.html#split
>> 
>> ---------------------------------------------------
>> S[] split(S)(S s); // merge space together
>> 
>> and
>> 
>> Un­qual!(S1)[] split(S1, S2)(S1 s, S2 delim); // do not merge 
>> delim
>> together ?
>> 
>> ---------------------------------------------------
>> 
>> 
>> why the second split function do not merge delim together?
>> how merge delim together?
>> 
>
> Code to try
> ----------------------
> import std.string;
> import std.stdio;
> import std.array;
>
> void main( ){
>   string test = "hi\t\tD is fun";
>   string test2= "hi  D is fun";
>   writeln( test.split("\t"));
>   writeln( test2.split() );
>   writeln( test2.split(" ") );
> }
> ----------------------
> Result
> ["hi", "", "D is fun"]
> ["hi", "D", "is", "fun"]
> ["hi", "", "D", "is", "fun"]

I was talking with bioinfornatics on IRC so I can clarify a bit 
of what he's saying.  std.array.split without delimiter merges 
runs of adjacent whitespace.  In the version where you can 
specify the delimiter runs are not merged leaving empty items in 
the resulting array.  This is because the delimiter specifying 
version just calls std.algorithm.splitter which doesn't merge 
runs whereas the whitespace version of std.array.split uses its 
own internal algorithm to split.

Although the delimiter specifiable version's documentation 
doesn't say it would merge runs, one would assume it'd behave 
like its whitespace only cousin.

Either the docs should be clarified or the function should be 
changed to work like the other version (I prefer the latter 
solution).

Regards,
Brad Anderson


More information about the Digitalmars-d-learn mailing list