Thread overview
std.algorithm.splitter improovement?
Dec 14, 2013
seany
Dec 14, 2013
Peter Alexander
Dec 14, 2013
bearophile
Dec 14, 2013
Marco Leise
Dec 14, 2013
bearophile
Dec 14, 2013
Dmitry Olshansky
Dec 15, 2013
Marco Leise
Dec 16, 2013
seany
December 14, 2013
the std.algorithm.splitter returns a blank or null (eg a null string "") between two consecuting delimeters.

for example, splitting "hello  world" (two spaces between words) will return ["hello" , "", "world"]

is there an improoved version of it, which wont return such a blank/null when multiple delimeters are found consecutively? (i tried to search the tango for d2, but i was not successful, should there be one like this already, it must have escaped my sight)
December 14, 2013
On Saturday, 14 December 2013 at 16:00:06 UTC, seany wrote:
> the std.algorithm.splitter returns a blank or null (eg a null string "") between two consecuting delimeters.
>
> for example, splitting "hello  world" (two spaces between words) will return ["hello" , "", "world"]
>
> is there an improoved version of it, which wont return such a blank/null when multiple delimeters are found consecutively? (i tried to search the tango for d2, but i was not successful, should there be one like this already, it must have escaped my sight)

Just filter out the empty ranges:

r.splitter().filter!(x => !x.empty)
December 14, 2013
seany:

> for example, splitting "hello  world" (two spaces between words) will return ["hello" , "", "world"]

It's a bug.

Bye,
bearophile
December 14, 2013
Am Sat, 14 Dec 2013 17:41:22 +0100
schrieb "bearophile" <bearophileHUGS@lycos.com>:

> seany:
> 
> > for example, splitting "hello  world" (two spaces between words) will return ["hello" , "", "world"]
> 
> It's a bug.
> 
> Bye,
> bearophile

Not at all, the documentation explicitly states:

  assert(equal(splitter("hello  world", ' '), [ "hello", "", "world" ]));

-- 
Marco

December 14, 2013
Marco Leise:

> Not at all, the documentation explicitly states:
>
>   assert(equal(splitter("hello  world", ' '), [ "hello", "", "world" ]));

I didn't see the ' ' in the OP code, sorry.

A test:


void main() {
    import std.stdio, std.string, std.algorithm;
    auto s = "hello  world";
    s.split().writeln;
    std.array.splitter(s).writeln;
    s.splitter(' ').writeln;
}


The output seems OK:

["hello", "world"]
["hello", "world"]
["hello", "", "world"]

Bye,
bearophile
December 14, 2013
14-Dec-2013 21:20, bearophile пишет:
> Marco Leise:
>
>> Not at all, the documentation explicitly states:
>>
>>   assert(equal(splitter("hello  world", ' '), [ "hello", "", "world" ]));
>
> I didn't see the ' ' in the OP code, sorry.
>
> A test:
>
>
> void main() {
>      import std.stdio, std.string, std.algorithm;
>      auto s = "hello  world";
>      s.split().writeln;
>      std.array.splitter(s).writeln;
>      s.splitter(' ').writeln;
> }
>
>
> The output seems OK:

Yup, there are 2 splitters - one that uses explicit separator and one that uses predicate. AFAIK the default predicate is std.uni.isWhite.

> ["hello", "world"]
> ["hello", "world"]
> ["hello", "", "world"]
>
> Bye,
> bearophile


-- 
Dmitry Olshansky
December 15, 2013
Am Sat, 14 Dec 2013 18:20:13 +0100
schrieb "bearophile" <bearophileHUGS@lycos.com>:

> Marco Leise:
> 
> > Not at all, the documentation explicitly states:
> >
> >   assert(equal(splitter("hello  world", ' '), [ "hello", "",
> > "world" ]));
> 
> I didn't see the ' ' in the OP code, sorry.
> 
> A test:
> 
> 
> void main() {
>      import std.stdio, std.string, std.algorithm;
>      auto s = "hello  world";
>      s.split().writeln;
>      std.array.splitter(s).writeln;
>      s.splitter(' ').writeln;
> }
> 
> 
> The output seems OK:
> 
> ["hello", "world"]
> ["hello", "world"]
> ["hello", "", "world"]
> 
> Bye,
> bearophile

Somehow I cannot say this makes me happy. I totally thought there was only one splitter and it has to be used with a delimiter. You are right that the OP didn't say which version he used. The result made it clear in the end. So the solution to this is "use the other splitter".

-- 
Marco

December 16, 2013
On Sunday, 15 December 2013 at 01:25:39 UTC, Marco Leise wrote:
> Am Sat, 14 Dec 2013 18:20:13 +0100
> schrieb "bearophile" <bearophileHUGS@lycos.com>:
>
>> Marco Leise:
>> 
>> > Not at all, the documentation explicitly states:
>> >
>> >   assert(equal(splitter("hello  world", ' '), [ "hello", "", "world" ]));
>> 
>> I didn't see the ' ' in the OP code, sorry.
>> 
>> A test:
>> 
>> 
>> void main() {
>>      import std.stdio, std.string, std.algorithm;
>>      auto s = "hello  world";
>>      s.split().writeln;
>>      std.array.splitter(s).writeln;
>>      s.splitter(' ').writeln;
>> }
>> 
>> 
>> The output seems OK:
>> 
>> ["hello", "world"]
>> ["hello", "world"]
>> ["hello", "", "world"]
>> 
>> Bye,
>> bearophile
>
> Somehow I cannot say this makes me happy. I totally thought
> there was only one splitter and it has to be used with a
> delimiter. You are right that the OP didn't say which version
> he used. The result made it clear in the end. So the solution
> to this is "use the other splitter".

I was using, as I said, std.algorithm.splitter, and I did not know that it could be called without a delimeter. i always called it like

std.algorithm.splitter("hello  world", ' '); //two spaces in array

And that is giving me one empty element.