]> git.donarmstrong.com Git - perltidy.git/commitdiff
minor optimizations
authorSteve Hancock <perltidy@users.sourceforge.net>
Sun, 24 Sep 2023 23:26:22 +0000 (16:26 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Sun, 24 Sep 2023 23:26:22 +0000 (16:26 -0700)
lib/Perl/Tidy/Formatter.pm
lib/Perl/Tidy/Tokenizer.pm

index c4f288008d7a01570eeef1a64990ca7d47a98c37..191f61b946d2770cbd80f80997f81930f0ba4040 100644 (file)
@@ -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);
 
index 9275bb38e7b4953f543b7697d6972023e3cd1bb2..2a8603f72491bd75847edee9fe9a0f07a9f3c195 100644 (file)
@@ -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