From 9593c956c17a46d01c150936d493c32fc9cc6a54 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Fri, 15 Nov 2019 10:15:18 -0700 Subject: [PATCH] Markdown.pl: claw back another minor performance gain Revise _DeTab yet again to provide a minor boost. This really does currently seem to be the fastest detab mechanism available. Signed-off-by: Kyle J. McKay --- Markdown.pl | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/Markdown.pl b/Markdown.pl index bf2564f..8989280 100755 --- a/Markdown.pl +++ b/Markdown.pl @@ -2840,28 +2840,16 @@ sub _DeTab { my $ans = ""; while (pos($text) < $end) { my $line; - if ($text =~ /\G([^\n]*\n)/gc) { + if ($text =~ /\G(.*?\n)/gcs) { $line = $1; } else { $line = substr($text, pos($text)); pos($text) = $end; } $line =~ s/$spr// if $leadsp; - my $eline = length($line); - pos($line) = 0; - my $xline = ""; - while (pos($line) < $eline) { - if ($line =~ /\G([^\t]+)/gc) { - $xline .= $1; - next; - } - if ($line =~ /\G(\t+)/gc) { - $xline .= ' ' x (length($1) * $ts - (length($xline) % $ts)); - next; - } - die "programmer error"; # cannot get here - } - $ans .= $xline; + # From the Perl camel book section "Fluent Perl" but modified a bit + $line =~ s/(.*?)(\t+)/$1 . ' ' x (length($2) * $ts - length($1) % $ts)/ges; + $ans .= $line; } return $ans; }