]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ViewDiff.pm
www: avoid `state' usage to perform allocations up-front
[public-inbox.git] / lib / PublicInbox / ViewDiff.pm
index 8c37c139c6201391c93477c82a924bf127c29431..57a1b5d66dd9dda27de87c3106da22fb3f385355 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # used by PublicInbox::View
@@ -20,6 +20,9 @@ sub UNSAFE () { "^A-Za-z0-9\-\._~/" }
 
 my $OID_NULL = '0{7,40}';
 my $OID_BLOB = '[a-f0-9]{7,40}';
+my $LF = qr!\n!;
+my $ANY = qr![^\n]!;
+my $FN = qr!(?:"?[^/\n]+/[^\n]+|/dev/null)!;
 
 # cf. git diff.c :: get_compact_summary
 my $DIFFSTAT_COMMENT = qr/\((?:new|gone|(?:(?:new|mode) [\+\-][lx]))\)/;
@@ -106,17 +109,18 @@ sub diff_header ($$$$) {
        my (undef, undef, $pa, $pb) = splice(@$top, 0, 4); # ignore oid_{a,b}
        my $spfx = $ctx->{-spfx};
        my $dctx = { spfx => $spfx };
+
+       # get rid of leading "a/" or "b/" (or whatever --{src,dst}-prefix are)
+       $pa = (split('/', git_unquote($pa), 2))[1] if $pa ne '/dev/null';
+       $pb = (split('/', git_unquote($pb), 2))[1] if $pb ne '/dev/null';
        if ($pa eq $pb && $pb ne '/dev/null') {
-               $pa = $pb = (split('/', git_unquote($pb), 2))[1];
                $dctx->{Q} = "?b=".uri_escape_utf8($pb, UNSAFE);
        } else {
                my @q;
                if ($pb ne '/dev/null') {
-                       $pb = (split('/', git_unquote($pb), 2))[1];
                        push @q, 'b='.uri_escape_utf8($pb, UNSAFE);
                }
                if ($pa ne '/dev/null') {
-                       $pa = (split('/', git_unquote($pa), 2))[1];
                        push @q, 'a='.uri_escape_utf8($pa, UNSAFE);
                }
                $dctx->{Q} = '?'.join('&amp;', @q);
@@ -149,8 +153,8 @@ sub diff_header ($$$$) {
 sub diff_before_or_after ($$$) {
        my ($dst, $ctx, $x) = @_;
        my $linkify = $ctx->{-linkify};
-       for my $y (split(/(^---\r?\n)/sm, $$x)) {
-               if ($y =~ /\A---\r?\n\z/s) {
+       for my $y (split(/(^---\n)/sm, $$x)) {
+               if ($y =~ /\A---\n\z/s) {
                        $$dst .= "---\n"; # all HTML is "\r\n" => "\n"
                } elsif ($y =~ /^ [0-9]+ files? changed, /sm) {
                        # ok, looks like a diffstat, go line-by-line:
@@ -166,11 +170,9 @@ sub diff_before_or_after ($$$) {
        }
 }
 
+# callers must do CRLF => LF conversion before calling this
 sub flush_diff ($$$) {
        my ($dst, $ctx, $cur) = @_;
-       state $LF = qr!\r?\n!;
-       state $ANY = qr![^\r\n]!;
-       state $FN = qr!(?:"?[^/\n]+/[^\r\n]+|/dev/null)!;
 
        my @top = split(/(
                (?:     # begin header stuff, don't capture filenames, here,