]> Sergey Matveev's repositories - public-inbox.git/commitdiff
mbox_reader: do not chomp non-blank EOL
authorEric Wong <e@80x24.org>
Fri, 12 Feb 2021 07:05:52 +0000 (00:05 -0700)
committerEric Wong <e@80x24.org>
Sat, 13 Feb 2021 02:58:31 +0000 (22:58 -0400)
It's conceivable some cases won't generate an empty line before
an mboxrd or mboxo From_ line.  Ensure we can handle that case
and don't leave the Eml->{bdy} without a trailing LF character.

And drop an unnecessary alarm import while we're in the area.

lib/PublicInbox/MboxReader.pm
t/mbox_reader.t

index 59ce4fb624d57000fcf6a913c9072e927486f483..df7c78faf885c6cf9dae692ef30cfdac43741f0f 100644 (file)
@@ -26,7 +26,7 @@ sub _mbox_from {
                }
                @raw = grep /[^ \t\r\n]/s, @raw; # skip empty messages
                while (defined(my $raw = shift @raw)) {
-                       $raw =~ s/\r?\n\z//s;
+                       $raw =~ s/^\r?\n\z//ms;
                        $raw =~ s/$from_re/$1/gms;
                        my $eml = PublicInbox::Eml->new(\$raw);
                        $eml_cb->($eml, @arg);
index 30a5e6e3590e5d3c6e63963afbfe3f5973e4e172..18d0fd6818a35f4ac519347fd48cd1fe18231c2d 100644 (file)
@@ -74,9 +74,29 @@ for my $fmt (@mbox) { $check_fmt->($fmt) }
 s/\n/\r\n/sg for (values %raw);
 for my $fmt (@mbox) { $check_fmt->($fmt) }
 
+{
+       my $no_blank_eom = <<'EOM';
+From x@y Fri Oct  2 00:00:00 1993
+a: b
+
+body1
+From x@y Fri Oct  2 00:00:00 1993
+c: d
+
+body2
+EOM
+       # chop($no_blank_eom) eq "\n" or BAIL_OUT 'broken LF';
+       for my $variant (qw(mboxrd mboxo)) {
+               my @x;
+               open my $fh, '<', \$no_blank_eom or BAIL_OUT 'PerlIO::scalar';
+               $reader->$variant($fh, sub { push @x, shift });
+               is_deeply($x[0]->{bdy}, \"body1\n", 'LF preserved in 1st');
+               is_deeply($x[1]->{bdy}, \"body2\n", 'no LF added in 2nd');
+       }
+}
+
 SKIP: {
        use PublicInbox::Spawn qw(popen_rd);
-       use Time::HiRes qw(alarm);
        my $fh = popen_rd([ $^X, '-E', <<'' ]);
 say "From x@y Fri Oct  2 00:00:00 1993";
 print "a: b\n\n", "x" x 70000, "\n\n";