From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/3] mbox_reader: do not chomp non-blank EOL
Date: Fri, 12 Feb 2021 00:05:52 -0700 [thread overview]
Message-ID: <20210212070552.13901-4-e@80x24.org> (raw)
In-Reply-To: <20210212070552.13901-1-e@80x24.org>
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 | 2 +-
t/mbox_reader.t | 22 +++++++++++++++++++++-
2 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/lib/PublicInbox/MboxReader.pm b/lib/PublicInbox/MboxReader.pm
index 59ce4fb6..df7c78fa 100644
--- a/lib/PublicInbox/MboxReader.pm
+++ b/lib/PublicInbox/MboxReader.pm
@@ -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);
diff --git a/t/mbox_reader.t b/t/mbox_reader.t
index 30a5e6e3..18d0fd68 100644
--- a/t/mbox_reader.t
+++ b/t/mbox_reader.t
@@ -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";
prev parent reply other threads:[~2021-02-12 7:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-12 7:05 [PATCH 0/3] MboxReader usage and changes Eric Wong
2021-02-12 7:05 ` [PATCH 1/3] filter/vger: kill trailing newlines aggressively Eric Wong
2021-02-12 7:05 ` [PATCH 2/3] import_mbox: use MboxReader Eric Wong
2021-02-12 7:05 ` Eric Wong [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210212070552.13901-4-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).