The documentation says
/*******************************************
* Returns s[] sans trailing delimiter[], if any.
* If delimiter[] is null, removes trailing CR, LF, or CRLF, if any.
*/
The implementation on the other hand returns the input string if delimiter is null. So either the documentation or the implementation needs to be fixed.
If the implementation should work like the documentation, it could be something like this:
C[] chomp(C, C1)(C[] s, in C1[] delimiter)
{
if (delimiter == null)
return chomp(s);
else if (endsWith(s, delimiter))
return s[0 .. $ - delimiter.length];
else
return s;
}
Comment #1 by yao.gomez — 2012-02-05T21:18:12Z
DMD 2.058HEAD. With this example, all the assertions fail.
------
import std.string, std.stdio, std.conv;
void main()
{
auto foo = "foobar\n\r\n\n";
auto baz = chomp(foo);
auto bar = chomp(foo, "");
auto soz = chomp(foo, "\n\r");
assert( baz == "foobar");
assert( bar == "foobar");
assert( soz == "foobar");
}
------