# 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; }
####
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% --
####
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( $_ ); }},
});