]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
search: help: avoid ':' in user prefixes
authorEric Wong <e@80x24.org>
Sat, 10 Aug 2024 09:00:04 +0000 (09:00 +0000)
committerEric Wong <e@80x24.org>
Sat, 17 Aug 2024 17:36:15 +0000 (17:36 +0000)
The non-':'-suffixed variation of the string is already used as
hash keys and literals elsewhere.  Theoretically, a Perl
implementation can save some allocations this way (though Perl 5
currently doesn't).

In any case, we'll introduce a help2txt method to allow sharing
code between the callers in WwwText and Documentation/common.perl

Documentation/common.perl
lib/PublicInbox/AltId.pm
lib/PublicInbox/IndexHeader.pm
lib/PublicInbox/Isearch.pm
lib/PublicInbox/Search.pm
lib/PublicInbox/WwwText.pm

index 3a6617c474304ab598ac85964be00f27861aeef3..53bae4959ed0ac555d133a4c5aea24ea50a65bb6 100755 (executable)
@@ -3,7 +3,7 @@
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 use strict;
 use Fcntl qw(SEEK_SET);
-my $have_search = eval { require PublicInbox::Search; 1 };
+use PublicInbox::Search;
 my $addr = 'meta@public-inbox.org';
 for my $pod (@ARGV) {
        open my $fh, '+<', $pod or die "open($pod): $!";
@@ -27,7 +27,7 @@ L<http://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/meta/>
 
 =head1$1
                !ms;
-       $have_search and $s =~ s!^=for\scomment\n
+       $s =~ s!^=for\scomment\n
                        ^AUTO-GENERATED-SEARCH-TERMS-BEGIN\n
                        .+?
                        ^=for\scomment\n
@@ -46,23 +46,7 @@ L<http://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/meta/>
 }
 
 sub search_terms {
-       my $help = eval('\@PublicInbox::Search::HELP');
-       my $s = '';
-       my $pad = 0;
-       my $i;
-       for ($i = 0; $i < @$help; $i += 2) {
-               my $pfx = $help->[$i];
-               my $n = length($pfx);
-               $pad = $n if $n > $pad;
-               $s .= $pfx . "\0";
-               $s .= $help->[$i + 1];
-               $s .= "\f\n";
-       }
-       $pad += 2;
-       my $padding = ' ' x ($pad + 4);
-       $s =~ s/^/$padding/gms;
-       $s =~ s/^$padding(\S+)\0/"    $1".(' ' x ($pad - length($1)))/egms;
-       $s =~ s/\f\n/\n/gs;
+       my $s = PublicInbox::Search::help2txt(@PublicInbox::Search::HELP);
        $s =~ s/^  //gms;
        substr($s, 0, 0, "=for comment\nAUTO-GENERATED-SEARCH-TERMS-BEGIN\n\n");
        $s .= "\n=for comment\nAUTO-GENERATED-SEARCH-TERMS-END\n";
index bd6cf9735d151a0481f866baf6c9e7e15f90cc8f..76dc23e6ca76fa4d27e9eb0f581aea65fd50beed 100644 (file)
@@ -56,7 +56,7 @@ sub index_extra { # for PublicInbox::SearchIdx
 
 sub user_help { # for PublicInbox::Search
        my ($self) = @_;
-       ("$self->{prefix}:", <<EOF);
+       ($self->{prefix}, <<EOF);
 alternate serial number  e.g. $self->{prefix}:12345 (boolean)
 EOF
 }
index 07827959b8c4a87dccc49e0ebdaf89a90b8e8eb7..a67080f9c22216154a229df5fd7478727b9a9110 100644 (file)
@@ -62,7 +62,7 @@ sub index_extra { # for PublicInbox::SearchIdx
 
 sub user_help { # for PublicInbox::Search
        my ($self) = @_;
-       ("$self->{prefix}:", <<EOF);
+       ($self->{prefix}, <<EOF);
 the `$self->{header}' mail header  e.g. $self->{prefix}:stable
 EOF
 }
index 20808d6dde233cf130a295d6e0955545ede4d1aa..9566f71093bb6555c0dbb9ffec7d46ed7a5cb378 100644 (file)
@@ -131,7 +131,7 @@ sub mset_to_smsg {
 
 sub has_threadid { 1 }
 
-sub help { $_[0]->{es}->help }
+sub help_txt { $_[0]->{es}->help_txt }
 
 sub xh_args { # prep getopt args to feed to xap_helper.h socket
        my ($self, $opt) = @_; # TODO uid_range
index 6a0bdb0f66a15d58f399d674a66fc519f4a83d6b..bdf5591cd21e3232ba5d4bc5034e45d797ab5394 100644 (file)
@@ -190,33 +190,33 @@ my %prob_prefix = (
 # especially since we don't offer boolean searches for To/Cc/From
 # headers, either
 our @HELP = (
-       's:' => 'match within Subject  e.g. s:"a quick brown fox"',
-       'd:' => <<EOF,
+       s => 'match within Subject  e.g. s:"a quick brown fox"',
+       d => <<EOF,
 match date-time range, git "approxidate" formats supported
 Open-ended ranges such as `d:last.week..' and
 `d:..2.days.ago' are supported
 EOF
-       'b:' => 'match within message body, including text attachments',
-       'nq:' => 'match non-quoted text within message body',
-       'q:' => 'match quoted text within message body',
-       'n:' => 'match filename of attachment(s)',
-       't:' => 'match within the To header',
-       'c:' => 'match within the Cc header',
-       'f:' => 'match within the From header',
-       'a:' => 'match within the To, Cc, and From headers',
-       'tc:' => 'match within the To and Cc headers',
-       'l:' => 'match contents of the List-Id header',
-       'bs:' => 'match within the Subject and body',
-       'dfn:' => 'match filename from diff',
-       'dfa:' => 'match diff removed (-) lines',
-       'dfb:' => 'match diff added (+) lines',
-       'dfhh:' => 'match diff hunk header context (usually a function name)',
-       'dfctx:' => 'match diff context lines',
-       'dfpre:' => 'match pre-image git blob ID',
-       'dfpost:' => 'match post-image git blob ID',
-       'dfblob:' => 'match either pre or post-image git blob ID',
-       'patchid:' => "match `git patch-id --stable' output",
-       'rt:' => <<EOF,
+       b => 'match within message body, including text attachments',
+       nq => 'match non-quoted text within message body',
+       q => 'match quoted text within message body',
+       n => 'match filename of attachment(s)',
+       t => 'match within the To header',
+       c => 'match within the Cc header',
+       f => 'match within the From header',
+       a => 'match within the To, Cc, and From headers',
+       tc => 'match within the To and Cc headers',
+       l => 'match contents of the List-Id header',
+       bs => 'match within the Subject and body',
+       dfn => 'match filename from diff',
+       dfa => 'match diff removed (-) lines',
+       dfb => 'match diff added (+) lines',
+       dfhh => 'match diff hunk header context (usually a function name)',
+       dfctx => 'match diff context lines',
+       dfpre => 'match pre-image git blob ID',
+       dfpost => 'match post-image git blob ID',
+       dfblob => 'match either pre or post-image git blob ID',
+       patchid => "match `git patch-id --stable' output",
+       rt => <<EOF,
 match received time, like `d:' if sender's clock was correct
 EOF
 );
@@ -657,14 +657,27 @@ EOM
        $ret .= "}\n";
 }
 
-sub help {
+sub help2txt (@) { # also used by Documentation/common.perl
+       my @help = @_;
+       my $pad = 0;
+       my $htxt = '';
+       while (defined(my $pfx = shift @help)) {
+               my $n = length($pfx) + 1;
+               $pad = $n if $n > $pad;
+               $htxt .= $pfx . ":\0" . shift(@help) . "\f\n";
+       }
+       $pad += 2;
+       my $padding = ' ' x ($pad + 4);
+       $htxt =~ s/^/$padding/gms;
+       $htxt =~ s/^$padding(\S+)\0/"    $1".(' ' x ($pad - length($1)))/egms;
+       $htxt =~ s/\f\n/\n/gs;
+       $htxt;
+}
+
+sub help_txt {
        my ($self) = @_;
        $self->{qp} // $self->qparse_new; # parse altids + indexheaders
-       my @ret = @HELP;
-       if (my $user_pfx = $self->{-user_pfx}) {
-               push @ret, @$user_pfx;
-       }
-       \@ret;
+       help2txt(@HELP, @{$self->{-user_pfx} // []});
 }
 
 # always returns a scalar value
index 8279591a5f929b496532d8d5518015a3b164a78d..d39083b68a2e61968e700f193ffddc705ce1ae99 100644 (file)
@@ -75,29 +75,6 @@ sub get_text {
        PublicInbox::WwwStream::html_oneshot($ctx, $code, $txt);
 }
 
-sub _srch_prefix ($$) {
-       my ($ibx, $txt) = @_;
-       my $pad = 0;
-       my $htxt = '';
-       my $help = $ibx->isrch->help;
-       my $i;
-       for ($i = 0; $i < @$help; $i += 2) {
-               my $pfx = $help->[$i];
-               my $n = length($pfx);
-               $pad = $n if $n > $pad;
-               $htxt .= $pfx . "\0";
-               $htxt .= $help->[$i + 1];
-               $htxt .= "\f\n";
-       }
-       $pad += 2;
-       my $padding = ' ' x ($pad + 4);
-       $htxt =~ s/^/$padding/gms;
-       $htxt =~ s/^$padding(\S+)\0/"    $1".(' ' x ($pad - length($1)))/egms;
-       $htxt =~ s/\f\n/\n/gs;
-       $$txt .= $htxt;
-       1;
-}
-
 sub _colors_help ($$) {
        my ($ctx, $txt) = @_;
        my $ibx = $ctx->{ibx};
@@ -461,7 +438,7 @@ search
   Prefixes supported in this installation include:
 
 EOF
-               _srch_prefix($ibx, $txt);
+               $$txt .= $ibx->isrch->help_txt;
                $$txt .= <<EOF;
 
   Most prefixes are probabilistic, meaning they support stemming