EvdB has asked for the wisdom of the Perl Monks concerning the following question:
# No taint implications. sub A { s/^\s*//; s/\s*$//; return $_; } # These untaint the return value. sub B { m/^\s*(.*\S)\s*$/; return $1; } sub C { m/(\S+.*\S*)/; return $1; } sub D { m/(\S?.*\S*)/; return $1; }
I tend to use A as it does not untaint the string due to selecting $1.
Bench marks and complete code follow:
Rate A B C D A 4709/s -- -34% -39% -44% B 7091/s 51% -- -8% -16% C 7680/s 63% 8% -- -9% D 8449/s 79% 19% 10% --
Code is:
use strict; use warnings; use Benchmark qw(cmpthese); # No taint implications. sub A { s/^\s*//; s/\s*$//; return $_; } # These untaint the return value. sub B { m/^\s*(.*\S)\s*$/; return $1; } sub C { m/(\S+.*\S*)/; return $1; } sub D { m/(\S?.*\S*)/; return $1; } my @data = ( 'hello', ' hello', ' hello ', 'hello hello', ' hello hello', 'hello hello ', ' hello hello', ' hello hello ', 'h', ' h', 'h ', ' h ', "\n\t hello \n\t" ); for (@data) { my $A = A( $_ ); my $B = B( $_ ); my $C = C( $_ ); my $D = D( $_ ); unless ( $A eq $B && $A eq $C && $A eq $D ) { warn "Found a disagreement\n"; warn "\tA is '$A'\n\tB is '$B'\n\tC is '$C'\n\tD is '$D'\n\n"; } if ( $A =~ m/(^\s|\s$)/ ) { warn "Regexp not working\n\tA is '$A'\n\n"; } } cmpthese( -3, { A => sub { for (@data) { A( $_ ); }}, B => sub { for (@data) { B( $_ ); }}, C => sub { for (@data) { C( $_ ); }}, D => sub { for (@data) { D( $_ ); }}, });
--tidiness is the memory loss of environmental mnemonics
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Fastest (Golfish) way to strip whitspace off ends.
by BrowserUk (Patriarch) on Jul 02, 2003 at 16:25 UTC | |
by halley (Prior) on Jul 02, 2003 at 17:02 UTC | |
by BrowserUk (Patriarch) on Jul 02, 2003 at 17:21 UTC | |
by Enlil (Parson) on Jul 02, 2003 at 22:32 UTC | |
by EvdB (Deacon) on Jul 03, 2003 at 09:07 UTC |