dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH 3/3] add compat package for List::Util::uniqstr
Date: Thu,  8 Jun 2023 17:22:42 +0000	[thread overview]
Message-ID: <20230608172242.39552-3-e@80x24.org> (raw)
In-Reply-To: <20230608172242.39552-1-e@80x24.org>

This will make it easier to switch in the far future while
making callers easier-to-read (and more callers will be added).

Anyways, Perl 5.26 is a long time away for enterprise users;
but isolating compatibility code away can improve readability
of our core code in the meantime.
---
 MANIFEST                         |  1 +
 lib/PublicInbox/CodeSearchIdx.pm |  7 +++----
 lib/PublicInbox/Compat.pm        | 24 ++++++++++++++++++++++++
 lib/PublicInbox/Inbox.pm         | 10 +++-------
 lib/PublicInbox/LeiImport.pm     |  4 ++--
 lib/PublicInbox/LeiImportKw.pm   |  4 ++--
 lib/PublicInbox/LeiMailSync.pm   | 17 ++++++++---------
 lib/PublicInbox/SolverGit.pm     |  4 ++--
 8 files changed, 45 insertions(+), 26 deletions(-)
 create mode 100644 lib/PublicInbox/Compat.pm

diff --git a/MANIFEST b/MANIFEST
index 69730623..dc895016 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -165,6 +165,7 @@ lib/PublicInbox/CidxLogP.pm
 lib/PublicInbox/CmdIPC4.pm
 lib/PublicInbox/CodeSearch.pm
 lib/PublicInbox/CodeSearchIdx.pm
+lib/PublicInbox/Compat.pm
 lib/PublicInbox/CompressNoop.pm
 lib/PublicInbox/Config.pm
 lib/PublicInbox/ConfigIter.pm
diff --git a/lib/PublicInbox/CodeSearchIdx.pm b/lib/PublicInbox/CodeSearchIdx.pm
index 22097d03..ba14e52a 100644
--- a/lib/PublicInbox/CodeSearchIdx.pm
+++ b/lib/PublicInbox/CodeSearchIdx.pm
@@ -31,6 +31,7 @@ use PublicInbox::OnDestroy;
 use PublicInbox::CidxLogP;
 use PublicInbox::CidxComm;
 use PublicInbox::Git qw(%OFMT2HEXLEN);
+use PublicInbox::Compat qw(uniqstr);
 use Socket qw(MSG_EOR);
 use Carp ();
 our (
@@ -578,8 +579,7 @@ sub load_existing ($) { # for -u/--update
 		}
 		push @$dirs, @cur;
 	}
-	my %uniq; # List::Util::uniq requires Perl 5.26+
-	@$dirs = grep { !$uniq{$_}++ } @$dirs;
+	@$dirs = uniqstr @$dirs;
 }
 
 # SIG handlers:
@@ -912,8 +912,7 @@ sub cidx_run { # main entry point
 			$_ =~ /$re/ ? (push(@excl, $_), 0) : 1;
 		} @{$self->{git_dirs}};
 		warn("# excluding $_\n") for @excl;
-		my %uniq; # List::Util::uniq requires Perl 5.26+
-		@GIT_DIR_GONE = grep { !$uniq{$_}++ } (@GIT_DIR_GONE, @excl);
+		@GIT_DIR_GONE = uniqstr @GIT_DIR_GONE, @excl;
 	}
 	local $NCHANGE = 0;
 	local $LIVE_JOBS = $self->{-opt}->{jobs} ||
diff --git a/lib/PublicInbox/Compat.pm b/lib/PublicInbox/Compat.pm
new file mode 100644
index 00000000..78cba90e
--- /dev/null
+++ b/lib/PublicInbox/Compat.pm
@@ -0,0 +1,24 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# compatibility code for old Perl and standard modules, mainly
+# List::Util but maybe other stuff
+package PublicInbox::Compat;
+use v5.12;
+use parent qw(Exporter);
+require List::Util;
+
+our @EXPORT_OK = qw(uniqstr);
+
+# uniqstr is in List::Util 1.45+, which means Perl 5.26+;
+# so maybe 2030 for us since we need to support enterprise distros.
+# We can use uniqstr everywhere in our codebase and don't need
+# to account for special cases of `uniqnum' nor `uniq' in List::Util
+# even if they make more sense in some contexts
+no warnings 'once';
+*uniqstr = List::Util->can('uniqstr') // sub (@) {
+	my %seen;
+	grep { !$seen{$_}++ } @_;
+};
+
+1;
diff --git a/lib/PublicInbox/Inbox.pm b/lib/PublicInbox/Inbox.pm
index cb98d2ad..9afbb478 100644
--- a/lib/PublicInbox/Inbox.pm
+++ b/lib/PublicInbox/Inbox.pm
@@ -10,6 +10,7 @@ use PublicInbox::MID qw(mid2path);
 use PublicInbox::Eml;
 use List::Util qw(max);
 use Carp qw(croak);
+use PublicInbox::Compat qw(uniqstr);
 
 # returns true if further checking is required
 sub check_inodes ($) {
@@ -250,11 +251,7 @@ EOM
 			# nntp://news.example.com/alt.example
 			push @m, $u;
 		}
-
-		# List::Util::uniq requires Perl 5.26+, maybe we
-		# can use it by 2030 or so
-		my %seen;
-		@urls = grep { !$seen{$_}++ } (@urls, @m);
+		@urls = uniqstr @urls, @m;
 	}
 	\@urls;
 }
@@ -274,8 +271,7 @@ sub pop3_url {
 			@urls = map { m!\Apop3?s?://! ? $_ : "pop3://$_" } @$ps;
 		if (my $mi = $self->{'pop3mirror'}) {
 			my @m = map { m!\Apop3?s?://! ? $_ : "pop3://$_" } @$mi;
-			my %seen; # List::Util::uniq requires Perl 5.26+
-			@urls = grep { !$seen{$_}++ } (@urls, @m);
+			@urls = uniqstr @urls, @m;
 		}
 		my $n = 0;
 		for (@urls) { $n += s!/+\z!! }
diff --git a/lib/PublicInbox/LeiImport.pm b/lib/PublicInbox/LeiImport.pm
index 9053048a..a324a652 100644
--- a/lib/PublicInbox/LeiImport.pm
+++ b/lib/PublicInbox/LeiImport.pm
@@ -7,6 +7,7 @@ use strict;
 use v5.10.1;
 use parent qw(PublicInbox::IPC PublicInbox::LeiInput);
 use PublicInbox::InboxWritable qw(eml_from_path);
+use PublicInbox::Compat qw(uniqstr);
 
 # /^input_/ subs are used by (or override) PublicInbox::LeiInput superclass
 
@@ -40,8 +41,7 @@ sub pmdir_cb { # called via wq_io_do from LeiPmdir->each_mdir_fn
 	my @oidbin = $lms ? $lms->name_oidbin($folder, $bn) : ();
 	@oidbin > 1 and warn("W: $folder/*/$$bn not unique:\n",
 				map { "\t".unpack('H*', $_)."\n" } @oidbin);
-	my %seen;
-	my @docids = sort { $a <=> $b } grep { !$seen{$_}++ }
+	my @docids = sort { $a <=> $b } uniqstr
 			map { $lse->over->oidbin_exists($_) } @oidbin;
 	my $vmd = $self->{-import_kw} ? { kw => $kw } : undef;
 	if (scalar @docids) {
diff --git a/lib/PublicInbox/LeiImportKw.pm b/lib/PublicInbox/LeiImportKw.pm
index 4dd938f5..4b8e69fb 100644
--- a/lib/PublicInbox/LeiImportKw.pm
+++ b/lib/PublicInbox/LeiImportKw.pm
@@ -7,6 +7,7 @@ package PublicInbox::LeiImportKw;
 use strict;
 use v5.10.1;
 use parent qw(PublicInbox::IPC);
+use PublicInbox::Compat qw(uniqstr);
 
 sub new {
 	my ($cls, $lei) = @_;
@@ -38,8 +39,7 @@ sub ck_update_kw { # via wq_io_do
 	my $uid_url = "$url/;UID=$uid";
 	@oidbin > 1 and warn("W: $uid_url not unique:\n",
 				map { "\t".unpack('H*', $_)."\n" } @oidbin);
-	my %seen;
-	my @docids = sort { $a <=> $b } grep { !$seen{$_}++ }
+	my @docids = sort { $a <=> $b } uniqstr
 		map { $self->{over}->oidbin_exists($_) } @oidbin;
 	$self->{lse}->kw_changed(undef, $kw, \@docids) or return;
 	$self->{verbose} and $self->{lei}->qerr("# $uid_url => @$kw\n");
diff --git a/lib/PublicInbox/LeiMailSync.pm b/lib/PublicInbox/LeiMailSync.pm
index 308b1695..415459d5 100644
--- a/lib/PublicInbox/LeiMailSync.pm
+++ b/lib/PublicInbox/LeiMailSync.pm
@@ -6,6 +6,7 @@ package PublicInbox::LeiMailSync;
 use strict;
 use v5.10.1;
 use parent qw(PublicInbox::Lock);
+use PublicInbox::Compat qw(uniqstr);
 use DBI qw(:sql_types); # SQL_BLOB
 use PublicInbox::ContentHash qw(git_sha);
 use Carp ();
@@ -543,20 +544,19 @@ EOM
 --all=@no not accepted (must be `local' and/or `remote')
 EOM
 	}
-	my (%seen, @inc);
 	my @all = $self->folders;
 	for my $ok (@ok) {
 		if ($ok eq 'local') {
-			@inc = grep(!m!\A[a-z0-9\+]+://!i, @all);
+			push @$folders, grep(!m!\A[a-z0-9\+]+://!i, @all);
 		} elsif ($ok eq 'remote') {
-			@inc = grep(m!\A[a-z0-9\+]+://!i, @all);
+			push @$folders, grep(m!\A[a-z0-9\+]+://!i, @all);
 		} elsif ($ok ne '') {
 			return $lei->fail("--all=$all not understood");
 		} else {
-			@inc = @all;
+			push @$folders, @all;
 		}
-		push(@$folders, (grep { !$seen{$_}++ } @inc));
 	}
+	@$folders = uniqstr @$folders;
 	scalar(@$folders) || $lei->fail(<<EOM);
 no --mail-sync folders known to lei
 EOM
@@ -656,8 +656,8 @@ sub num_oidbin ($$$) {
 SELECT oidbin FROM blob2num WHERE fid = ? AND uid = ? ORDER BY _rowid_
 EOM
 	$sth->execute($fid, $uid);
-	my %uniq; # for public-inbox <= 1.7.0
-	grep { !$uniq{$_}++ } map { $_->[0] } @{$sth->fetchall_arrayref};
+	# for public-inbox <= 1.7.0:
+	uniqstr(map { $_->[0] } @{$sth->fetchall_arrayref});
 }
 
 sub name_oidbin ($$$) {
@@ -674,8 +674,7 @@ EOM
 	$sth->bind_param(2, $nm, SQL_VARCHAR);
 	$sth->execute;
 	my @old = map { $_->[0] } @{$sth->fetchall_arrayref};
-	my %uniq; # for public-inbox <= 1.7.0
-	grep { !$uniq{$_}++ } (@bin, @old);
+	uniqstr @bin, @old # for public-inbox <= 1.7.0
 }
 
 sub imap_oidhex {
diff --git a/lib/PublicInbox/SolverGit.pm b/lib/PublicInbox/SolverGit.pm
index ebb5cdff..5f317f51 100644
--- a/lib/PublicInbox/SolverGit.pm
+++ b/lib/PublicInbox/SolverGit.pm
@@ -18,6 +18,7 @@ use PublicInbox::Qspawn;
 use PublicInbox::Tmpfile;
 use PublicInbox::GitAsyncCat;
 use PublicInbox::Eml;
+use PublicInbox::Compat qw(uniqstr);
 use URI::Escape qw(uri_escape_utf8);
 
 # POSIX requires _POSIX_ARG_MAX >= 4096, and xargs is required to
@@ -556,8 +557,7 @@ sub extract_diffs_done {
 	my $diffs = delete $self->{tmp_diffs};
 	if (scalar @$diffs) {
 		unshift @{$self->{patches}}, @$diffs;
-		my %seen; # List::Util::uniq requires Perl 5.26+ :<
-		my @u = grep { !$seen{$_}++ } map { di_url($self, $_) } @$diffs;
+		my @u = uniqstr(map { di_url($self, $_) } @$diffs);
 		dbg($self, "found $want->{oid_b} in " .  join(" ||\n\t", @u));
 		++$self->{nr_p};
 

      parent reply	other threads:[~2023-06-08 17:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08 17:22 [PATCH 1/3] search: add comments wrt codesearch, reduce ops Eric Wong
2023-06-08 17:22 ` [PATCH 2/3] search: hoist out do_enquire for codesearch Eric Wong
2023-06-08 17:22 ` 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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230608172242.39552-3-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.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).