]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
add compat package for List::Util::uniqstr
authorEric Wong <e@80x24.org>
Fri, 9 Jun 2023 10:31:08 +0000 (10:31 +0000)
committerEric Wong <e@80x24.org>
Fri, 9 Jun 2023 18:58:29 +0000 (18:58 +0000)
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 code we actually care about in the meantime.

MANIFEST
lib/PublicInbox/CodeSearchIdx.pm
lib/PublicInbox/Compat.pm [new file with mode: 0644]
lib/PublicInbox/Inbox.pm
lib/PublicInbox/LeiImport.pm
lib/PublicInbox/LeiImportKw.pm
lib/PublicInbox/LeiMailSync.pm
lib/PublicInbox/SolverGit.pm

index 697306232ae50b69fa2d0e968cc7830b7fae0ec0..dc895016ed980cd48deb446b54e146f908a83a5a 100644 (file)
--- 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
index 22097d03bf1b07416444f3185b0870d572cb9027..ba14e52ae4680484c716e3ccec61632c30d9e7ab 100644 (file)
@@ -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 (file)
index 0000000..78cba90
--- /dev/null
@@ -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;
index cb98d2ad5e29cbc017ddf65df293e9bbe1546d9f..9afbb4783a8015b9ff3e9ed605f7aceb6aacb765 100644 (file)
@@ -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!! }
index 9053048a22de4e7598c25730694ec687f6e16701..a324a65212fb26bec56c06a5c5e07de499f81358 100644 (file)
@@ -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) {
index 4dd938f5c12c85ad41d1ed322aad9a33d59c49b8..4b8e69fb2b506d527ce659a8a158a5db1e296ceb 100644 (file)
@@ -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");
index 308b1695b2efb742479bf592ec0c37762ca3556a..415459d5c127f3beb2b83175ba207ce27b20da59 100644 (file)
@@ -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 {
index ebb5cdff7dd11ac09247a2a91b1774a9e61b6446..5f317f51d19141acfc1cc4fea3a8981a87ab9fe0 100644 (file)
@@ -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};