Discussion:
[PATCH] Add .trim method
(too old to reply)
Ovid
2009-01-12 00:49:24 UTC
Permalink
----- Original Message ----
This patch implements the .trim() method for strings.
Now that I'm reading S29, I see there is no .trim() method there. I got that because it was referenced in pugs in the cookbook (not in tests, though) and I was trying to get the examples to run. Bummer :(


Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://use.perl.org/~Ovid/journal/
Twitter - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
Ovid
2009-01-12 10:22:00 UTC
Permalink
----- Original Message ----
Post by Ovid
This patch implements the .trim() method for strings.
Now that I'm reading S29, I see there is no .trim() method there. I got that
because it was referenced in pugs in the cookbook (not in tests, though) and I
was trying to get the examples to run. Bummer :(
Sorry for constant spamming, but now that I've put disparate pieces together, I see what's going on here. I'll stop soon, but I am *sick* of rewriting the trim() function over and over :)

There are no tests because it's not in the spec. If there's a spec, I know where to write the tests and will happily commit tests for them to Pugs and I'll submit a new patch against any-str.pir and t/spectest.data to get them to pass.


http://tinyurl.com/4xjnh, a mailing list thread where Larry wrote:

: (Replying to p6l instead of p6c as requested.)
:
: On Mon, Apr 04, 2005 at 10:39:16AM -0700, Larry Wall wrote:
: > (Now that builtins are just functions out in * space, we can probably
: > afford to throw a few more convenience functions out there for common
: > operations like word splitting and whitespace trimming. (Specific
: > proposals to p6l please.))

So even though it's not in the spec, it seems like something Larry is not entirely opposed to (or wasn't back in 2005). So here's my proposal (copied to p6l):

=item trim

our Str multi Str::trim ( Str $string )

Removes leading and trailing whitespace from a string.

=cut

I could optionally make the following work:

$string.trim(:leading<0>);
$string.trim(:trailing<0>);

Setting leading or trailing to false (they default to true) would result in either leading or trailing whitespace not being trimmed. Setting both to false would be a no-op.

Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://use.perl.org/~Ovid/journal/
Twitter - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
Jesse
2009-01-12 15:07:21 UTC
Permalink
Post by Ovid
$string.trim(:leading<0>);
$string.trim(:trailing<0>);
Alternatively, those could be ltrim() and rtrim().
'left' and 'right' are probably not the right names for functions which
trim leading and/or trailing space, since their meanings get somewhat
ambiguous if a language renders right-to-left instead of left-to-right
or vice-versa

-jesse
Ovid
2009-01-12 15:23:33 UTC
Permalink
----- Original Message ----
Post by Jesse
Post by Ovid
$string.trim(:leading<0>);
$string.trim(:trailing<0>);
Alternatively, those could be ltrim() and rtrim().
'left' and 'right' are probably not the right names for functions which
trim leading and/or trailing space, since their meanings get somewhat
ambiguous if a language renders right-to-left instead of left-to-right
or vice-versa
Um, er. Damn. Now I'm wondering how my "leading" and "trailing" trimming works with Hebrew. How are the strings implemented internally?

And then there are languages such as Manchu and Uygher which can be written vertically. http://www.omniglot.com/writing/direction.htm


Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://use.perl.org/~Ovid/journal/
Twitter - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
Gianni Ceccarelli
2009-01-12 15:30:06 UTC
Permalink
Post by Ovid
Um, er. Damn. Now I'm wondering how my "leading" and "trailing"
trimming works with Hebrew. How are the strings implemented
internally?
RTL (and bidi) languages are written in strings so that the character
order is the logical, reading, order. That is, the character "nearer
to the beginning of the string" is the first one to be read by a human
reader (so, such character would be displayed on the right for a RTL
language, on the left for a LTR language, at the top for a TTB language)

Short answer: your implementation is right :)
--
Dakkar - <Mobilis in mobile>
GPG public key fingerprint = A071 E618 DD2C 5901 9574
6FE2 40EA 9883 7519 3F88
key id = 0x75193F88

You possess a mind not merely twisted, but actually sprained.
Larry Wall
2009-01-12 16:31:26 UTC
Permalink
On Mon, Jan 12, 2009 at 05:04:50AM -0800, Ovid wrote:
: ...the trivial $string.trim and trim($string) case.

Hmm, I'd think .trim should work like .chomp, and return the trimmed
string without changing the original. You'd use $str.=trim to do it
in place.

Can't say I really like the negated options though. They smell funny.

Larry
Jonathan Worthington
2009-01-12 16:56:42 UTC
Permalink
Post by Ovid
----- Original Message ----
In the pir, doesn't the "s = self" line copy self, thus ensuring that I'm changing "s" and not "self"?
No, it's binding.
Post by Ovid
Or do I need "s = clone self" (or however it's written).
Yeah, but also note that substr would return a copy...
Post by Ovid
Post by Larry Wall
Can't say I really like the negated options though. They smell funny.
Agreed, but ltrim and rtrim will disappoint Israelis and dyslexics alike. Suggestions welcome as I can't think of anything better.
The .Net framework calls 'em TrimStart and TrimEnd (and has a Trim that
does both). So maybe trim_start and trim_end if we wanted to take that
lead...

Jonathan
Andy Lester
2009-01-12 22:14:31 UTC
Permalink
How about .trim(:start) and .trim(:end)?
And .trim(:both) for orthogonality.

--
Andy Lester => ***@petdance.com => www.petdance.com => AIM:petdance
Ovid
2009-01-12 16:50:44 UTC
Permalink
----- Original Message ----
Post by Larry Wall
: ...the trivial $string.trim and trim($string) case.
Hmm, I'd think .trim should work like .chomp, and return the trimmed
string without changing the original. You'd use $str.=trim to do it
in place.
In the pir, doesn't the "s = self" line copy self, thus ensuring that I'm changing "s" and not "self"? Or do I need "s = clone self" (or however it's written).
Post by Larry Wall
Can't say I really like the negated options though. They smell funny.
Agreed, but ltrim and rtrim will disappoint Israelis and dyslexics alike. Suggestions welcome as I can't think of anything better.


Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://use.perl.org/~Ovid/journal/
Twitter - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
Geoffrey Broadwell
2009-01-12 17:33:32 UTC
Permalink
Post by Ovid
----- Original Message ----
Post by Ovid
$string.trim(:leading<0>);
$string.trim(:trailing<0>);
Alternatively, those could be ltrim() and rtrim(). If you need to dynamically determine what you're going to trim, you'd couldn't just set variables to do it, though. You'd have to figure out which methods to call. Or all could be allowed and $string.trim(:leading<0>) could all $string.rtrim internally.
When I saw your proposed syntax above, instead of reading "don't trim
leading/trailing whitespace", I read "change the definition of
'whitespace' to 'codepoint 0' for leading/trailing".

That of course raises the question of how one *would* properly override
trim's concept of whitespace ....


-'f
Larry Wall
2009-01-12 18:23:25 UTC
Permalink
On Mon, Jan 12, 2009 at 09:33:32AM -0800, Geoffrey Broadwell wrote:
: That of course raises the question of how one *would* properly override
: trim's concept of whitespace ....

Well, given that .trim is essentially just .comb(/\S.*\S/), which in
turn is really just m:g/(\S.*\S)/, I don't see much need for alternate
trimmings.

Larry
Jonathan Scott Duff
2009-01-12 15:37:35 UTC
Permalink
On Mon, Jan 12, 2009 at 9:01 AM, Ovid
Post by Ovid
----- Original Message ----
Post by Ovid
$string.trim(:leading<0>);
$string.trim(:trailing<0>);
Alternatively, those could be ltrim() and rtrim(). If you need to
dynamically determine what you're going to trim, you'd couldn't just set
variables to do it, though. You'd have to figure out which methods to call.
Or all could be allowed and $string.trim(:leading<0>) could all
$string.rtrim internally.
If I were going to have ltrim() and rtrim(), I'd implement them in terms of
trim() rather than the other way around.

-Scott
--
Jonathan Scott Duff
***@gmail.com
Loading...