user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@yhbt.net>
To: meta@public-inbox.org
Subject: [PATCH 02/13] msg_iter: pass $idx as a scalar, not array
Date: Thu,  7 May 2020 21:05:45 +0000	[thread overview]
Message-ID: <20200507210556.22995-3-e@yhbt.net> (raw)
In-Reply-To: <20200507210556.22995-1-e@yhbt.net>

This doesn't make any difference for most multipart
messages (or any single part messages).  However,
this starts having space savings when parts start
nesting.

It also slightly simplifies callers.
---
 lib/PublicInbox/MsgIter.pm   |  6 ++++--
 lib/PublicInbox/SearchIdx.pm |  2 +-
 lib/PublicInbox/View.pm      | 18 +++++++++---------
 lib/PublicInbox/WwwAttach.pm |  4 ++--
 t/mime.t                     |  5 +++--
 t/msg_iter.t                 |  2 +-
 6 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/lib/PublicInbox/MsgIter.pm b/lib/PublicInbox/MsgIter.pm
index cd5a5d99..7c28d019 100644
--- a/lib/PublicInbox/MsgIter.pm
+++ b/lib/PublicInbox/MsgIter.pm
@@ -20,12 +20,14 @@ sub em_each_part ($$;$$) {
 		my $i = 0;
 		@parts = map { [ $_, 1, ++$i ] } @parts;
 		while (my $p = shift @parts) {
-			my ($part, $depth, @idx) = @$p;
+			my ($part, $depth, $idx) = @$p;
 			my @sub = $part->subparts;
 			if (@sub) {
 				$depth++;
 				$i = 0;
-				@sub = map { [ $_, $depth, @idx, ++$i ] } @sub;
+				@sub = map {
+					[ $_, $depth, "$idx.".(++$i) ]
+				} @sub;
 				@parts = (@sub, @parts);
 			} else {
 				$cb->($p, $cb_arg);
diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm
index 25118f43..a7e31b71 100644
--- a/lib/PublicInbox/SearchIdx.pm
+++ b/lib/PublicInbox/SearchIdx.pm
@@ -277,7 +277,7 @@ sub index_diff ($$$) {
 }
 
 sub index_xapian { # msg_iter callback
-	my $part = $_[0]->[0]; # ignore $depth and @idx
+	my $part = $_[0]->[0]; # ignore $depth and $idx
 	my ($self, $doc) = @{$_[1]};
 	my $ct = $part->content_type || 'text/plain';
 	my $fn = $part->filename;
diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index e42fb362..3328c865 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -482,9 +482,8 @@ sub multipart_text_as_html {
 
 sub attach_link ($$$$;$) {
 	my ($ctx, $ct, $p, $fn, $err) = @_;
-	my ($part, $depth, @idx) = @$p;
-	my $nl = $idx[-1] > 1 ? "\n" : '';
-	my $idx = join('.', @idx);
+	my ($part, $depth, $idx) = @$p;
+	my $nl = substr($idx, -2) eq '.1' ? '' : "\n"; # like join("\n", ...)
 	my $size = bytes::length($part->body);
 
 	# hide attributes normally, unless we want to aid users in
@@ -519,8 +518,8 @@ sub add_text_body { # callback for each_part
 	my ($p, $ctx) = @_;
 	my $upfx = $ctx->{mhref};
 	my $ibx = $ctx->{-inbox};
-	# $p - from each_part: [ Email::MIME-like, depth, @idx ]
-	my ($part, $depth, @idx) = @$p;
+	# $p - from each_part: [ Email::MIME-like, depth, $idx ]
+	my ($part, $depth, $idx) = @$p;
 	my $ct = $part->content_type || 'text/plain';
 	my $fn = $part->filename;
 	my ($s, $err) = msg_part_text($part, $ct);
@@ -537,13 +536,14 @@ sub add_text_body { # callback for each_part
 	# headers for solver unless some coderepo are configured:
 	my $diff;
 	if ($s =~ /^--- [^\n]+\n\+{3} [^\n]+\n@@ /ms) {
-		# diffstat anchors do not link across attachments or messages:
-		$idx[0] = $upfx . $idx[0] if $upfx ne '';
-		$ctx->{-apfx} = join('/', @idx);
+		# diffstat anchors do not link across attachments or messages,
+		# -apfx is just a stable prefix for making diffstat anchors
+		# linkable to the first diff hunk w/o crossing attachments
+		$idx =~ tr!.!/!; # compatibility with previous versions
+		$ctx->{-apfx} = $upfx . $idx;
 
 		# do attr => filename mappings for diffstats in git diffs:
 		$ctx->{-anchors} = {} if $s =~ /^diff --git /sm;
-
 		$diff = 1;
 		delete $ctx->{-long_path};
 		my $spfx;
diff --git a/lib/PublicInbox/WwwAttach.pm b/lib/PublicInbox/WwwAttach.pm
index 774b38ae..b1009907 100644
--- a/lib/PublicInbox/WwwAttach.pm
+++ b/lib/PublicInbox/WwwAttach.pm
@@ -11,9 +11,9 @@ use PublicInbox::MIME;
 use PublicInbox::MsgIter;
 
 sub get_attach_i { # ->each_part callback
-	my ($part, $depth, @idx) = @{$_[0]};
+	my ($part, $depth, $idx) = @{$_[0]};
 	my $res = $_[1];
-	return if join('.', @idx) ne $res->[3]; # $idx
+	return if $idx ne $res->[3]; # [0-9]+(?:\.[0-9]+)+
 	$res->[0] = 200;
 	my $ct = $part->content_type;
 	$ct = parse_content_type($ct) if $ct;
diff --git a/t/mime.t b/t/mime.t
index 0d478ace..b9a4d66b 100644
--- a/t/mime.t
+++ b/t/mime.t
@@ -98,9 +98,10 @@ $msg = PublicInbox::MIME->new($raw);
 my $nr = 0;
 msg_iter($msg, sub {
 	my ($part, $level, @ex) = @{$_[0]};
-	if ($ex[0] == 1) {
+	is($level, 1, 'at expected level');
+	if (join('fail if $#ex > 0', @ex) eq '1') {
 		is($part->body_str, "your tree directly? \r\n", 'body OK');
-	} elsif ($ex[0] == 2) {
+	} elsif (join('fail if $#ex > 0', @ex) eq '2') {
 		is($part->body, "-----BEGIN PGP SIGNATURE-----\n\n" .
 				"=7wIb\n" .
 				"-----END PGP SIGNATURE-----\n",
diff --git a/t/msg_iter.t b/t/msg_iter.t
index 5c57e043..e8115e25 100644
--- a/t/msg_iter.t
+++ b/t/msg_iter.t
@@ -28,7 +28,7 @@ use_ok('PublicInbox::MsgIter');
 		$s =~ s/\s+//s;
 		push @parts, [ $s, $level, @ex ];
 	});
-	is_deeply(\@parts, [ [qw(a 2 1 1)], [qw(b 2 1 2)], [qw(sig 1 2)] ],
+	is_deeply(\@parts, [ [qw(a 2 1.1)], [qw(b 2 1.2)], [qw(sig 1 2)] ],
 		'nested part shows up properly');
 }
 

  parent reply	other threads:[~2020-05-07 21:05 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-07 21:05 [PATCH 00/13] eml: pure-Perl replacement for Email::MIME Eric Wong
2020-05-07 21:05 ` [PATCH 01/13] msg_iter: make ->each_part method for PublicInbox::MIME Eric Wong
2020-05-07 21:05 ` Eric Wong [this message]
2020-05-07 21:05 ` [PATCH 03/13] filter/rubylang: avoid recursing subparts to strip trailers Eric Wong
2020-05-07 21:05 ` [PATCH 04/13] smsg: use capitalization for header retrieval Eric Wong
2020-05-07 21:05 ` [PATCH 05/13] eml: pure-Perl replacement for Email::MIME Eric Wong
2020-05-07 21:05 ` [PATCH 06/13] switch read-only Email::Simple users to Eml Eric Wong
2020-05-07 21:05 ` [PATCH 07/13] replace most uses of PublicInbox::MIME with Eml Eric Wong
2020-05-07 21:05 ` [PATCH 08/13] EmlContentFoo: Email::MIME::ContentType replacement Eric Wong
2020-05-07 21:05 ` [PATCH 09/13] EmlContentFoo: relax Encode version requirement Eric Wong
2020-05-07 21:05 ` [PATCH 10/13] eml: remove dependency on Email::MIME::Encodings Eric Wong
2020-05-07 21:05 ` [PATCH 11/13] xt: eml comparison tests Eric Wong
2020-05-08  4:47   ` Eric Wong
2020-05-07 21:05 ` [PATCH 12/13] remove most internal Email::MIME usage Eric Wong
2020-05-07 21:05 ` [PATCH 13/13] eml: drop trailing blank line on missing epilogue Eric Wong

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: http://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=20200507210556.22995-3-e@yhbt.net \
    --to=e@yhbt.net \
    --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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

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).