Thread overview
Phobos: Arbitrary delimiter variants of (std.string) stripLeft and stripRight
Jun 24, 2012
Chad J
Jun 24, 2012
Jonathan M Davis
Jun 24, 2012
Chad J
Jun 27, 2012
Jonathan M Davis
June 24, 2012
I'm not exactly sure what the arbitrary delimiter variants of stripLeft and stripRight are, but they should be referenced in the documentation of those functions.  And if they don't exist, why don't we have them?

I was looking around for trimLeft and trimRight, but no luck.  chomp seems to be the arbitrary delimiter variant of trimRight, but then there is no chompLeft.  If we had chompLeft I'd expect chompRight to exist and chomp to remove both the left and right parts.

There are also no examples/unittests in the documentation on the website for stripLeft, stripRight, chomp, or a bunch of others.

Link for your convenience:
http://dlang.org/phobos/std_string.html#stripLeft
June 24, 2012
On Saturday, June 23, 2012 20:04:11 Chad J wrote:
> I'm not exactly sure what the arbitrary delimiter variants of stripLeft and stripRight are, but they should be referenced in the documentation of those functions.  And if they don't exist, why don't we have them?

There are no such variants. They could be added, but they don't currently exist.

> I was looking around for trimLeft and trimRight, but no luck.

trim and strip are the same thing. Having both would be bad design. It's understandable if you were looking for trim first, since a library could go with either name, and you probably have seen others using trim, but we chose strip, and we're not going to have both.

> chomp
> seems to be the arbitrary delimiter variant of trimRight, but then there
> is no chompLeft.

chompPrefix is chompLeft. And no, _neither_ is the delimiter variant of stripLeft or stripRight. stripRight with an arbitrary delimiter would remove all instances of that delimiter from the right-hand side of the array (and that delimiter would have to be a single character), whereas chomp takes a substring and removes _exactly_ that substring. The same would go for stripLeft and chompPrefix.

> If we had chompLeft I'd expect chompRight to exist and chomp to remove both the left and right parts.
> 
> There are also no examples/unittests in the documentation on the website for stripLeft, stripRight, chomp, or a bunch of others.

They could be added.

- Jonathan M Davis
June 24, 2012
On 06/23/2012 08:25 PM, Jonathan M Davis wrote:
> On Saturday, June 23, 2012 20:04:11 Chad J wrote:
>> I'm not exactly sure what the arbitrary delimiter variants of stripLeft
>> and stripRight are, but they should be referenced in the documentation
>> of those functions.  And if they don't exist, why don't we have them?
>
> There are no such variants. They could be added, but they don't currently
> exist.
>
>> I was looking around for trimLeft and trimRight, but no luck.
>
> trim and strip are the same thing. Having both would be bad design. It's
> understandable if you were looking for trim first, since a library could go
> with either name, and you probably have seen others using trim, but we chose
> strip, and we're not going to have both.
>

I've seen them used to differentiate between whitespace and any-delimiter variants in other places, and the vocabulary seems to be used entirely inconsistently.  I just kind of run under the assumption that trim|strip mean remove /something/ from the right|left side of a given string.  The /something/ may or may not be whitespace or some delimiter of your choosing depending on what language you are in.

So yeah, I'm all for using something besides "trim". ;)

>> chomp
>> seems to be the arbitrary delimiter variant of trimRight, but then there
>> is no chompLeft.
>
> chompPrefix is chompLeft. And no, _neither_ is the delimiter variant of
> stripLeft or stripRight. stripRight with an arbitrary delimiter would remove
> all instances of that delimiter from the right-hand side of the array (and
> that delimiter would have to be a single character), whereas chomp takes a
> substring and removes _exactly_ that substring. The same would go for
> stripLeft and chompPrefix.
>

Ah.  Looks like a doc rewrite may be in order (and add unittests/examples!):

Returns s sans the trailing delimiter, if any. If no delimiter is given, then any trailing '\r', '\n', "\r\n", std.uni.lineSep, or std.uni.paraSeps are removed.

My parse of this: "any trailing ...s are removed."

I took the plural to mean that more than one newline/separator may be removed.

It should parse like this: "one trailing ... is removed."

>> If we had chompLeft I'd expect chompRight to exist and
>> chomp to remove both the left and right parts.
>>
>> There are also no examples/unittests in the documentation on the website
>> for stripLeft, stripRight, chomp, or a bunch of others.
>
> They could be added.
>
> - Jonathan M Davis

I would say "should be added" myself, given how bread-and-butter this stuff is for me (at least at work).  I had to write all of those myself when I started using this Synergy/DE|DBL language at work, and it would be just silly to have to do that in a powerful language like D.

I'd add all of this stuff myself except that http://forum.dlang.org/thread/js5nl8$h94$1@digitalmars.com
June 27, 2012
On Saturday, June 23, 2012 20:04:11 Chad J wrote:
> I'm not exactly sure what the arbitrary delimiter variants of stripLeft and stripRight are, but they should be referenced in the documentation of those functions.  And if they don't exist, why don't we have them?

You know, you can use find for the same thing quite easily (heck, you can use find instead of the current version of strip quite easily), so I'm not sure that this buys us much. It is a bit ugly to deal with stripRight that way, but if anything, I think that's an argument for adding rfind rather than an overload for strip.

auto strippedLeft = find!"a != b"(str, delim);
auto strippedRight = retro(find!"a != b"(retro(str), delim));
auto stripped = find!"a != b"(retro(find!"a != b"(retro(str), delim), delim);

If we had rfind, it could be

auto stripped = find!"a != b"(rfind!"a != b"(str, delim), delim);

Stripping whitespace is done often enough that strip, stripLeft, and stripRight are is merited, but I'm not convinced that stripping anything other than whitespace is common enough to merit it.

- Jonathan M Davis