* [PATCH] avoid length in boolean context
@ 2015-08-29 21:36 Eric Wong
0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-08-29 21:36 UTC (permalink / raw)
To: meta
Perl does not currently optimize for this.
ref (from p5p):
http://mid.gmane.org/D5C27970-9176-4C7A-8B99-7D78360E67A2@pobox.com
---
lib/PublicInbox/Feed.pm | 2 +-
lib/PublicInbox/Filter.pm | 2 +-
lib/PublicInbox/Search.pm | 3 ++-
lib/PublicInbox/SearchIdx.pm | 6 +++---
lib/PublicInbox/View.pm | 24 ++++++++++++++----------
lib/PublicInbox/WWW.pm | 2 +-
public-inbox-mda | 4 ++--
7 files changed, 24 insertions(+), 19 deletions(-)
diff --git a/lib/PublicInbox/Feed.pm b/lib/PublicInbox/Feed.pm
index 9e56747..bc76cdc 100644
--- a/lib/PublicInbox/Feed.pm
+++ b/lib/PublicInbox/Feed.pm
@@ -216,7 +216,7 @@ sub get_feedopts {
close $fh;
}
- if ($pi_config && defined $listname && length $listname) {
+ if ($pi_config && defined $listname && $listname ne '') {
foreach my $key (qw(address)) {
$rv{$key} = $pi_config->get($listname, $key) || "";
}
diff --git a/lib/PublicInbox/Filter.pm b/lib/PublicInbox/Filter.pm
index 0b1ec91..c0985dd 100644
--- a/lib/PublicInbox/Filter.pm
+++ b/lib/PublicInbox/Filter.pm
@@ -211,7 +211,7 @@ sub collapse {
$mime->header_set('Content-Type', $part->content_type);
$mime->body_set($part->body_raw);
my $cte = $part->header('Content-Transfer-Encoding');
- if (defined($cte) && length($cte)) {
+ if (defined($cte) && $cte ne '') {
$mime->header_set('Content-Transfer-Encoding', $cte);
}
mark_changed($mime);
diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index 4362a00..4b3830e 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -251,10 +251,11 @@ sub subject_summary {
last if length($new) >= $max;
$subj = $new;
}
- if (length $subj) {
+ if ($subj ne '') {
my $r = scalar @subj ? ' ...' : '';
$subj =~ s/ \z/$r/s;
} else {
+ # subject has one REALLY long word, and NOT spam? wtf
@subj = ($l =~ /\A(.{1,72})/);
$subj = $subj[0] . ' ...';
}
diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm
index 292419e..2ac53a7 100644
--- a/lib/PublicInbox/SearchIdx.pm
+++ b/lib/PublicInbox/SearchIdx.pm
@@ -74,7 +74,7 @@ sub add_message {
my $subj = $smsg->subject;
- if (length $subj) {
+ if ($subj ne '') {
$doc->add_term(xpfx('subject') . $subj);
my $path = $self->subject_path($subj);
@@ -308,7 +308,7 @@ sub _index_sync {
my $git = PublicInbox::GitCatFile->new($self->{git_dir});
my $latest = $db->get_metadata('last_commit');
- my $range = length $latest ? "$latest..$head" : $head;
+ my $range = $latest eq '' ? $head : "$latest..$head";
$latest = undef;
# get indexed messages
@@ -394,7 +394,7 @@ sub _read_git_config_perm {
sub _git_config_perm {
my $self = shift;
my $perm = scalar @_ ? $_[0] : _read_git_config_perm($self);
- return PERM_GROUP if (!defined($perm) || !length($perm));
+ return PERM_GROUP if (!defined($perm) || $perm eq '');
return PERM_UMASK if ($perm eq 'umask');
return PERM_GROUP if ($perm eq 'group');
if ($perm =~ /\A(?:all|world|everybody)\z/) {
diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm
index 51fee04..cc68229 100644
--- a/lib/PublicInbox/View.pm
+++ b/lib/PublicInbox/View.pm
@@ -198,7 +198,7 @@ sub index_walk {
$s =~ s/^\s*$//sgm;
$s =~ s/\s+\z//s;
- if (length $s) {
+ if ($s ne '') {
# kill per-line trailing whitespace
$s =~ s/[ \t]+$//sgm;
$s .= "\n" unless $s =~ /\n\z/s;
@@ -352,7 +352,7 @@ sub headers_to_html_header {
my $mid_href = $mid->as_href;
foreach my $h (qw(From To Cc Subject Date)) {
my $v = $mime->header($h);
- defined($v) && length($v) or next;
+ defined($v) && ($v ne '') or next;
$v = PublicInbox::Hval->new_oneline($v);
if ($h eq 'From') {
@@ -414,7 +414,7 @@ sub html_footer {
foreach my $h (qw(From To Cc)) {
my $v = $mime->header($h);
- defined($v) && length($v) or next;
+ defined($v) && ($v ne '') or next;
my @addrs = Email::Address->parse($v);
foreach my $recip (@addrs) {
my $address = $recip->address;
@@ -439,9 +439,9 @@ sub html_footer {
my $srch = $ctx->{srch} if $ctx;
my $idx = $standalone ? " <a\nhref=\"../../\">index</a>" : '';
if ($idx && $srch) {
- $irt = $mime->header('In-Reply-To') || '';
$mid = mid_compress(mid_clean($mid));
- my $t_anchor = length $irt ? T_ANCHOR : '';
+ my $t_anchor = defined $irt ? T_ANCHOR : '';
+ $irt = $mime->header('In-Reply-To');
$idx = " <a\nhref=\"../../t/$mid/$t_anchor\">".
"threadlink</a>$idx";
my $res = $srch->get_followups($mid);
@@ -458,7 +458,7 @@ sub html_footer {
} else {
$idx .= "\n(no followups, yet)\n";
}
- if ($irt) {
+ if (defined $irt) {
$irt = PublicInbox::Hval->new_msgid($irt);
$irt = $irt->as_href;
$irt = "<a\nhref=\"../$irt/\">parent</a> ";
@@ -499,10 +499,14 @@ sub simple_dump {
my $mid = $x->header('Message-ID');
my $pfx = ' ' x $level;
$$dst .= $pfx;
+
+ # Subject is never undef, this mail was loaded from
+ # our Xapian which would've resulted in '' if it were
+ # really missing (and Filter rejects empty subjects)
my $s = $x->header('Subject');
my $h = $root->[2]->subject_path($s);
if ($root->[1]->{$h}) {
- $s = '';
+ $s = undef;
} else {
$root->[1]->{$h} = 1;
$s = PublicInbox::Hval->new($s);
@@ -514,11 +518,11 @@ sub simple_dump {
$m = '../' . $m->as_href . '/';
$f = $f->as_html;
$d = $d->as_html . ' UTC';
- if (length($s) == 0) {
- $$dst .= "` <a\nhref=\"$m\">$f @ $d</a>\n";
- } else {
+ if (defined $s) {
$$dst .= "` <a\nhref=\"$m\">$s</a>\n" .
"$pfx by $f @ $d\n";
+ } else {
+ $$dst .= "` <a\nhref=\"$m\">$f @ $d</a>\n";
}
}
}
diff --git a/lib/PublicInbox/WWW.pm b/lib/PublicInbox/WWW.pm
index 8058f3e..a01271b 100644
--- a/lib/PublicInbox/WWW.pm
+++ b/lib/PublicInbox/WWW.pm
@@ -249,7 +249,7 @@ sub do_redirect {
sub ctx_get {
my ($ctx, $key) = @_;
my $val = $ctx->{$key};
- (defined $val && length $val) or die "BUG: bad ctx, $key unusable\n";
+ (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
$val;
}
diff --git a/public-inbox-mda b/public-inbox-mda
index c4822b6..b1f33af 100755
--- a/public-inbox-mda
+++ b/public-inbox-mda
@@ -64,7 +64,7 @@ if (PublicInbox::MDA->precheck($filter, $dst->{address}) &&
} else {
# Ensure emergency spam gets spamassassin headers.
# This makes it easier to prioritize obvious spam from less obvious
- if (defined($filtered) && length($filtered)) {
+ if (defined($filtered) && $filtered ne '') {
my $drop = Email::MIME->new(\$filtered);
$filtered = undef;
$filter->simple($drop);
@@ -82,7 +82,7 @@ sub do_spamc {
run([qw/spamc -E --headers/], \$orig, $out);
};
- return ($@ || $? || !defined($$out) || length($$out) == 0) ? 0 : 1;
+ return ($@ || $? || !defined($$out) || $$out eq '') ? 0 : 1;
}
sub search_index_sync {
--
EW
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2015-08-29 21:36 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-29 21:36 [PATCH] avoid length in boolean context Eric Wong
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).