Subject: | Uses $&, slowing down all regex use |
This module uses the variable $&, which slows down every regex match
done by perl for the entire process.
Attached is a patch which changes those uses to use captures and $1 for
most uses, and also a different way to count characters in another.
Subject: | remove-bad-regex-vars.txt |
diff --git a/lib/JavaScript/Packer.pm b/lib/JavaScript/Packer.pm
index 5c1e774..c1dfdf0 100644
--- a/lib/JavaScript/Packer.pm
+++ b/lib/JavaScript/Packer.pm
@@ -322,9 +322,9 @@ sub init {
$self->{'comments'}->{'reggrp'}->[-2]->{'replacement'} = sub {
if ( $_[1]->[0] eq '@' ) {
- $_[1]->[1] =~ s/$self->{'comments'}->{'re_str'}/$self->_process_minify( 'comments', $&, %+ )/egsm;
- $_[1]->[1] =~ s/$self->{'clean'}->{'re_str'}/$self->_process_minify( 'clean', $&, %+ )/egsm;
- $_[1]->[1] =~ s/$self->{'whitespace'}->{'re_str'}/$self->_process_minify( 'whitespace', $&, %+ )/egsm;
+ $_[1]->[1] =~ s/($self->{'comments'}->{'re_str'})/$self->_process_minify( 'comments', $1, %+ )/egsm;
+ $_[1]->[1] =~ s/($self->{'clean'}->{'re_str'})/$self->_process_minify( 'clean', $1, %+ )/egsm;
+ $_[1]->[1] =~ s/($self->{'whitespace'}->{'re_str'})/$self->_process_minify( 'whitespace', $1, %+ )/egsm;
return sprintf( "//%s%s\n%s", @{$_[1]} );
}
@@ -334,9 +334,9 @@ sub init {
$self->{'comments'}->{'reggrp'}->[-1]->{'replacement'} = sub {
if ( $_[1]->[0] =~ /^\/\*\@(.*)\@\*\/$/sm ) {
my $cmnt = $1;
- $cmnt =~ s/$self->{'comments'}->{'re_str'}/$self->_process_minify( 'comments', $&, %+ )/egsm;
- $cmnt =~ s/$self->{'clean'}->{'re_str'}/$self->_process_minify( 'clean', $&, %+ )/egsm;
- $cmnt =~ s/$self->{'whitespace'}->{'re_str'}/$self->_process_minify( 'whitespace', $&, %+ )/egsm;
+ $cmnt =~ s/($self->{'comments'}->{'re_str'})/$self->_process_minify( 'comments', $1, %+ )/egsm;
+ $cmnt =~ s/($self->{'clean'}->{'re_str'})/$self->_process_minify( 'clean', $1, %+ )/egsm;
+ $cmnt =~ s/($self->{'whitespace'}->{'re_str'})/$self->_process_minify( 'whitespace', $1, %+ )/egsm;
return sprintf( '/*@%s@*/ %s', $cmnt, $_[1]->[1] );
}
@@ -423,16 +423,16 @@ sub minify {
${$javascript} =~ s/\r//gsm;
${$javascript} .= "\n";
- ${$javascript} =~ s/$self->{'comments'}->{'re_str'}/$self->_process_minify( 'comments', $&, %+ )/egsm;
- ${$javascript} =~ s/$self->{'clean'}->{'re_str'}/$self->_process_minify( 'clean', $&, %+ )/egsm;
- ${$javascript} =~ s/$self->{'whitespace'}->{'re_str'}/$self->_process_minify( 'whitespace', $&, %+ )/egsm;
- ${$javascript} =~ s/$self->{'concat'}->{'re_str'}/$self->_process_minify( 'concat', $&, %+ )/egsm;
+ ${$javascript} =~ s/($self->{'comments'}->{'re_str'})/$self->_process_minify( 'comments', $1, %+ )/egsm;
+ ${$javascript} =~ s/($self->{'clean'}->{'re_str'})/$self->_process_minify( 'clean', $1, %+ )/egsm;
+ ${$javascript} =~ s/($self->{'whitespace'}->{'re_str'})/$self->_process_minify( 'whitespace', $1, %+ )/egsm;
+ ${$javascript} =~ s/($self->{'concat'}->{'re_str'})/$self->_process_minify( 'concat', $1, %+ )/egsm;
if ( $opts->{'compress'} ne 'clean' ) {
- ${$javascript} =~ s/$self->{'data_store'}->{'re_str'}/$self->_store_shrink( $&, %+ )/egsm;
+ ${$javascript} =~ s/($self->{'data_store'}->{'re_str'})/$self->_store_shrink( $1, %+ )/egsm;
while( ${$javascript} =~ /$SHRINK_VARS->{'BLOCK'}/ ) {
- ${$javascript} =~ s/$SHRINK_VARS->{'BLOCK'}/$self->_encode_shrink( $& )/egsm;
+ ${$javascript} =~ s/($SHRINK_VARS->{'BLOCK'})/$self->_encode_shrink( $1 )/egsm;
}
$self->_decode_shrink( $javascript, 'block_data', $SHRINK_VARS->{'ENCODED_BLOCK'} );
@@ -535,7 +535,7 @@ sub minify {
my $pd = join( '|', @pattern );
- $pd =~ s/$self->{'trim'}->{'re_str'}/$self->_process_minify( 'trim', $&, %+ )/egsm;
+ $pd =~ s/($self->{'trim'}->{'re_str'})/$self->_process_minify( 'trim', $1, %+ )/egsm;
unless ( $pd ) {
$pd = '^$';
@@ -603,7 +603,7 @@ sub minify {
$pk =~ s/(?>\|+)$//;
$packed_length += length( $pk );
- my $pc = length( $pk ) ? ( ( $pk =~ s/\|/$&/g ) + 1 ) : 0;
+ my $pc = length( $pk ) ? ( ( $pk =~ tr/|// ) + 1 ) : 0;
$packed_length += length( $pc );
my $pa = '[]';
@@ -624,7 +624,7 @@ sub minify {
if ( $opts->{'compress'} eq 'obfuscate' or $packed_length <= length( ${$javascript} ) ) {
- ${$javascript} =~ s/$BASE62_VARS->{'WORDS'}/sprintf( "%s", $words->{$&}->{'encoded'} )/eg;
+ ${$javascript} =~ s/($BASE62_VARS->{'WORDS'})/sprintf( "%s", $words->{$1}->{'encoded'} )/eg;
${$javascript} =~ s/([\\'])/\\$1/g;
#'