www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.algorithm.splitter improovement?

reply "seany" <seany uni-bonn.de> writes:
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)
Dec 14 2013
next sibling parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
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)
Dec 14 2013
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
seany:

 for example, splitting "hello  world" (two spaces between 
 words) will return ["hello" , "", "world"]
It's a bug. Bye, bearophile
Dec 14 2013
parent reply Marco Leise <Marco.Leise gmx.de> writes:
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
Dec 14 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Dec 14 2013
next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
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
Dec 14 2013
prev sibling parent reply Marco Leise <Marco.Leise gmx.de> writes:
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
Dec 14 2013
parent "seany" <seany uni-bonn.de> writes:
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.
Dec 15 2013