D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6735 - splitLines ignores trailing delimiter
Summary: splitLines ignores trailing delimiter
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-27 03:50 UTC by Nick Sabalausky
Modified: 2011-09-27 08:14 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Nick Sabalausky 2011-09-27 03:50:49 UTC
Both of these should pass, but they currently fail:

assert("a\n".splitLines() == ["a", ""]);
assert("a\n".splitLines() != ["a"]);

This is inconsistent with both "\na".splitLines() and with split(str, delim), as demonstrated here:

----------------------------------
import std.stdio;
import std.string;

void main()
{
//	assert("a\n".splitLines() == ["a", ""]);
	assert("a\n".splitLines() != ["a"]);
	
	foreach(str; ["a", "a\n", "\na"])
	{
		writefln("\nstr: '%s'", str);
		foreach(i, line; str.splitLines())
			writeln(i, ": ", line);
	}

	foreach(str; ["a", "a,", ",a"])
	{
		writefln("\nstr: '%s'", str);
		foreach(i, line; str.splitLines())
			writeln(i, ": ", line);
	}
}
----------------------------------

str: 'a
b'
0: a
1: b

str: 'a
b
'
0: a
1: b

str: '
a
b'
0:
1: a
2: b

str: 'a,b'
0: a
1: b

str: 'a,b,'
0: a
1: b
2:

str: ',a,b'
0:
1: a
2: b
----------------------------------
Comment 1 Nick Sabalausky 2011-09-27 03:52:29 UTC
Disregard those two assert lines inside main() in the second code example.
Comment 3 Andrei Alexandrescu 2011-09-27 08:14:52 UTC
This is by design. Refer to comment in https://github.com/D-Programming-Language/phobos/pull/277.