From 6a1b9bbb1f0cad09e5087d405652e65818336dd4 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Tue, 24 Mar 2020 06:38:39 -0700 Subject: [PATCH] improve efficiency of sub is_essential_whitespace() --- lib/Perl/Tidy/Formatter.pm | 149 +++++++++++++++++++------------------ 1 file changed, 78 insertions(+), 71 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 29e66fef..25519094 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -6724,12 +6724,14 @@ EOM # Note: This routine should almost never need to be changed. It is # for avoiding syntax problems rather than for formatting. - # Uses these global hashes: - # $is_sort_grep_map, $is_trigraph, $is_for_foreach + # Uses global hashe: $is_trigraph my ( $tokenll, $typell, $tokenl, $typel, $tokenr, $typer ) = @_; - my $tokenr_is_bareword = ( $tokenr =~ /^\w/ && $tokenr !~ /^\d/ ); + my $tokenr_is_bareword = $tokenr =~ /^\w/ && $tokenr !~ /^\d/; + my $tokenr_is_open_paren = $tokenr eq '('; + my $token_joined = $tokenl . $tokenr; + my $tokenl_is_dash = $tokenl eq '-'; my $result = @@ -6748,15 +6750,52 @@ EOM # do not combine a number with a concatenation dot # example: pom.caputo: # $vt100_compatible ? "\e[0;0H" : ('-' x 78 . "\n"); - || ( $typel eq 'n' && $tokenr eq '.' ) - || ( $typer eq 'n' && $tokenl eq '.' ) + || $typel eq 'n' && $tokenr eq '.' + || $typer eq 'n' && $tokenl eq '.' - # do not join a minus with a bare word, because you might form - # a file test operator. Example from Complex.pm: - # if (CORE::abs($z - i) < $eps); "z-i" would be taken as a file test. - || ( $tokenl eq '-' - && $tokenr_is_bareword - && length($tokenr) == 1 ) + # cases of a space before a bareword... + || ( + $tokenr_is_bareword && ( + + # do not join a minus with a bare word, because you might form + # a file test operator. Example from Complex.pm: + # if (CORE::abs($z - i) < $eps); + # "z-i" would be taken as a file test. + $tokenl_is_dash && length($tokenr) == 1 + + # and something like this could become ambiguous without space + # after the '-': + # use constant III=>1; + # $a = $b - III; + # and even this: + # $a = - III; + || $tokenl_is_dash && $typer =~ /^[wC]$/ + + # keep a space between a quote and a bareword to prevent the + # bareword from becoming a quote modifier. + || $typel eq 'Q' + + # keep a space between a token ending in '$' and any word; + # this caused trouble: "die @$ if $@" + || $typel eq 'i' && $tokenl =~ /\$$/ + + # do not remove space between an '&' and a bare word because + # it may turn into a function evaluation, like here + # between '&' and 'O_ACCMODE', producing a syntax error [File.pm] + # $opts{rdonly} = (($opts{mode} & O_ACCMODE) == O_RDONLY); + || $typel eq '&' + + # don't combine $$ or $# with any alphanumeric + # (testfile mangle.t with --mangle) + || $tokenl =~ /^\$[\$\#]$/ + + ) + ) ## end $tokenr_is_bareword + + # OLD, not used + # '= -' should not become =- or you will get a warning + # about reversed -= + # || ($tokenr eq '-') # do not join a bare word with a minus, like between 'Send' and # '-recipients' here <> @@ -6765,73 +6804,54 @@ EOM # -data => $data; # This is the safest thing to do. If we had the token to the right of # the minus we could do a better check. - || ( $tokenr eq '-' && $typel eq 'w' ) - - # and something like this could become ambiguous without space - # after the '-': - # use constant III=>1; - # $a = $b - III; - # and even this: - # $a = - III; - || ( $tokenl eq '-' - && $typer =~ /^[wC]$/ - && $tokenr_is_bareword ) - - # '= -' should not become =- or you will get a warning - # about reversed -= - # || ($tokenr eq '-') - - # keep a space between a quote and a bareword to prevent the - # bareword from becoming a quote modifier. - || ( $typel eq 'Q' && $tokenr_is_bareword ) - - # keep a space between a token ending in '$' and any word; - # this caused trouble: "die @$ if $@" - || ( $typel eq 'i' - && $tokenl =~ /\$$/ - && $tokenr_is_bareword ) + || $tokenr eq '-' && $typel eq 'w' # perl is very fussy about spaces before << - || ( $tokenr =~ /^\<\