From 730b3b5cf8779009ffda1ed2ee920b9493202db8 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sun, 24 Sep 2023 16:26:22 -0700 Subject: [PATCH] minor optimizations --- lib/Perl/Tidy/Formatter.pm | 19 +++++++++++++------ lib/Perl/Tidy/Tokenizer.pm | 23 +++++++++++++++++------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index c4f28800..191f61b9 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -22239,6 +22239,13 @@ sub break_long_lines { use constant TINY_BIAS => 0.0001; use constant MAX_BIAS => 0.001; +my %is_dot_and_or; + +BEGIN { + my @q = qw( . && || ); + @is_dot_and_or{@q} = (1) x scalar(@q); +} + sub break_lines_inner_loop { #----------------------------------------------------------------- @@ -22247,7 +22254,7 @@ sub break_lines_inner_loop { #----------------------------------------------------------------- my ( - $self, # + $self, $i_begin, $i_last_break, @@ -22256,7 +22263,6 @@ sub break_lines_inner_loop { $line_count, $rbond_strength_to_go, $saw_good_break, - ) = @_; # Given: @@ -22430,7 +22436,8 @@ sub break_lines_inner_loop { && ( $nesting_depth_to_go[$i_begin] > $nesting_depth_to_go[$i_next_nonblank] ) && ( - $next_nonblank_type =~ /^(\.|\&\&|\|\|)$/ + ## /^(\.|\&\&|\|\|)$/ + $is_dot_and_or{$next_nonblank_type} || ( $next_nonblank_type eq 'k' @@ -28991,9 +28998,9 @@ sub get_seqno { $tok_next = $tokens_to_go[$ibeg_next]; $type_next = $types_to_go[$ibeg_next]; - $has_leading_op_next = ( $tok_next =~ /^\w/ ) - ? $is_chain_operator{$tok_next} # + - * / : ? && || - : $is_chain_operator{$type_next}; # and, or + $has_leading_op_next = ( $type_next eq 'k' ) + ? $is_chain_operator{$tok_next} # and, or + : $is_chain_operator{$type_next}; # + - * / : ? && || next unless ($has_leading_op_next); diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index 9275bb38..2a8603f7 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -2825,7 +2825,9 @@ EOM $want_brace = $want_paren; $want_paren = EMPTY_STRING; } - elsif ( $statement_type =~ /^sub\b/ ) { + elsif ( substr( $statement_type, 0, 3 ) eq 'sub' + && $statement_type =~ /^sub\b/ ) + { $container_type = $statement_type; } else { @@ -2981,7 +2983,9 @@ EOM # restore statement type as 'sub' at closing paren of a signature # so that a subsequent ':' is identified as an attribute - if ( $container_type =~ /^sub\b/ ) { + if ( substr( $container_type, 0, 3 ) eq 'sub' + && $container_type =~ /^sub\b/ ) + { $statement_type = $container_type; } @@ -4116,7 +4120,7 @@ EOM $type = 'U'; $prototype = $ruser_function_prototype->{$current_package}{$tok}; } - elsif ( $tok =~ /^v\d+$/ ) { + elsif ( substr( $tok, 0, 1 ) eq 'v' && $tok =~ /^v\d+$/ ) { $type = 'v'; $self->report_v_string($tok); } @@ -7327,8 +7331,15 @@ sub scan_bare_identifier_do { # A:: # ::A # A'B - if ( $input_line =~ m/\G\s*((?:\w*(?:'|::)))*(?:(?:->)?(\w+))?/gc ) { - + if ( + $input_line =~ m{ + \G\s* # start at pos + ( (?:\w*(?:'|::)) )* # $1 = maybe package name like A:: A::B:: or A' + (?:(?:->)? # maybe followed by '->' + (\w+))? # $2 = maybe followed by sub name + }gcx + ) + { my $pos = pos($input_line); my $numc = $pos - $pos_beg; $tok = substr( $input_line, $pos_beg, $numc ); @@ -7367,7 +7378,7 @@ sub scan_bare_identifier_do { # check for v-string with leading 'v' type character # (This seems to have precedence over filehandle, type 'Y') - if ( $tok =~ /^v\d[_\d]*$/ ) { + if ( substr( $tok, 0, 1 ) eq 'v' && $tok =~ /^v\d[_\d]*$/ ) { # we only have the first part - something like 'v101' - # look for more -- 2.39.5