Skip to content

Commit

Permalink
use a better idiom to check for an empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 29, 2015
1 parent dea5fd2 commit 20bb97d
Show file tree
Hide file tree
Showing 20 changed files with 28 additions and 28 deletions.
8 changes: 4 additions & 4 deletions lib/Mojo/Content.pm
Expand Up @@ -39,9 +39,9 @@ sub generate_body_chunk {
my ($self, $offset) = @_;

$self->emit(drain => $offset)
if !delete $self->{delay} && !length($self->{body_buffer} // '');
if !delete $self->{delay} && ($self->{body_buffer} // '') eq '';
my $chunk = delete $self->{body_buffer} // '';
return $self->{eof} ? '' : undef unless length $chunk;
return $self->{eof} ? '' : undef if $chunk eq '';

return $chunk;
}
Expand Down Expand Up @@ -104,7 +104,7 @@ sub parse {
# Relaxed parsing
my $headers = $self->headers;
my $len = $headers->content_length // '';
if ($self->auto_relax && !length $len) {
if ($self->auto_relax && $len eq '') {
my $connection = lc($headers->connection // '');
$self->relaxed(1)
if $connection eq 'close' || (!$connection && $self->expect_close);
Expand Down Expand Up @@ -168,7 +168,7 @@ sub _build_chunk {
my ($self, $chunk) = @_;

# End
return "\x0d\x0a0\x0d\x0a\x0d\x0a" unless length $chunk;
return "\x0d\x0a0\x0d\x0a\x0d\x0a" if $chunk eq '';

# First chunk has no leading CRLF
my $crlf = $self->{chunks}++ ? "\x0d\x0a" : '';
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Cookie/Request.pm
Expand Up @@ -18,7 +18,7 @@ sub parse {

sub to_string {
my $self = shift;
return '' unless length(my $name = $self->name // '');
return '' if (my $name = $self->name // '') eq '';
my $value = $self->value // '';
return join '=', $name, $value =~ /[,;" ]/ ? quote $value : $value;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Cookie/Response.pm
Expand Up @@ -32,7 +32,7 @@ sub to_string {
my $self = shift;

# Name and value
return '' unless length(my $name = $self->name // '');
return '' if (my $name = $self->name // '') eq '';
my $value = $self->value // '';
my $cookie = join '=', $name, $value =~ /[,;" ]/ ? quote $value : $value;

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/DOM/CSS.pm
Expand Up @@ -126,7 +126,7 @@ sub _equation {
# Equation
my $num = [1, 1];
return $num if $equation !~ /(?:(-?(?:\d+)?)?(n))?\s*\+?\s*(-?\s*\d+)?\s*$/i;
$num->[0] = defined($1) && length($1) ? $1 : $2 ? 1 : 0;
$num->[0] = defined($1) && $1 ne '' ? $1 : $2 ? 1 : 0;
$num->[0] = -1 if $num->[0] eq '-';
$num->[1] = $3 // 0;
$num->[1] =~ s/\s+//g;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -81,7 +81,7 @@ sub write {

$self->{buffer} .= $chunk;
if ($cb) { $self->once(drain => $cb) }
elsif (!length $self->{buffer}) { return $self }
elsif ($self->{buffer} eq '') { return $self }
$self->reactor->watch($self->{handle}, !$self->{paused}, 1)
if $self->{handle};

Expand Down Expand Up @@ -109,12 +109,12 @@ sub _write {

# Handle errors only when reading (to avoid timing problems)
my $handle = $self->{handle};
if (length $self->{buffer}) {
if ($self->{buffer} ne '') {
return unless defined(my $written = $handle->syswrite($self->{buffer}));
$self->emit(write => substr($self->{buffer}, 0, $written, ''))->_again;
}

$self->emit('drain') if !length $self->{buffer};
$self->emit('drain') if $self->{buffer} eq '';
return if $self->is_writing;
return $self->close if $self->{graceful};
$self->reactor->watch($handle, !$self->{paused}, 0) if $self->{handle};
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/JSON.pm
Expand Up @@ -57,7 +57,7 @@ sub _decode {
eval {

# Missing input
die "Missing or empty input\n" unless length(local $_ = shift);
die "Missing or empty input\n" if (local $_ = shift) eq '';

# UTF-8
$_ = Mojo::Util::decode 'UTF-8', $_ unless shift;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message.pm
Expand Up @@ -110,7 +110,7 @@ sub get_body_chunk {

$self->emit('progress', 'body', $offset);
my $chunk = $self->content->get_body_chunk($offset);
return $chunk if !defined $chunk || length $chunk;
return $chunk if !defined $chunk || $chunk ne '';
$self->finish;

return $chunk;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Parameters.pm
Expand Up @@ -78,7 +78,7 @@ sub pairs {
# Parse string
if (defined(my $str = delete $self->{string})) {
my $pairs = $self->{pairs} = [];
return $pairs unless length $str;
return $pairs if $str eq '';

my $charset = $self->charset;
for my $pair (split '&', $str) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/Daemon.pm
Expand Up @@ -139,7 +139,7 @@ sub _finish {
return $self->_remove($id) if $req->error || !$tx->keep_alive;

# Build new transaction for leftovers
return unless length(my $leftovers = $req->content->leftovers);
return if (my $leftovers = $req->content->leftovers) eq '';
$tx = $c->{tx} = $self->_build_tx($id, $c);
$tx->server_read($leftovers);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Server/PSGI.pm
Expand Up @@ -57,7 +57,7 @@ sub getline {
return '' unless defined $chunk;

# End of content
return undef unless length $chunk;
return undef if $chunk eq '';

$self->{offset} += length $chunk;
return $chunk;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Template.pm
Expand Up @@ -40,7 +40,7 @@ sub build {
if ($op eq 'text') {
$value = join "\n", map { quotemeta $_ } split("\n", $value, -1);
$value .= '\n' if $newline;
$blocks[-1] .= "\$_O .= \"" . $value . "\";" if length $value;
$blocks[-1] .= "\$_O .= \"" . $value . "\";" if $value ne '';
}

# Code or multiline expression
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -17,7 +17,7 @@ sub client_read {
return $self->{state} = 'finished'
if !$res->is_status_class(100) || $res->headers->upgrade;
$self->res($res->new)->emit(unexpected => $res);
return unless length(my $leftovers = $res->content->leftovers);
return if (my $leftovers = $res->content->leftovers) eq '';
$self->client_read($leftovers);
}

Expand Down Expand Up @@ -85,7 +85,7 @@ sub _body {

# Finished
$self->{state} = $finish ? 'finished' : 'read'
if $self->{write} <= 0 || defined $buffer && !length $buffer;
if $self->{write} <= 0 || defined $buffer && $buffer eq '';

return defined $buffer ? $buffer : '';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -316,7 +316,7 @@ sub _message {
WindowBits => -15
);
$inflate->inflate(($msg .= "\x00\x00\xff\xff"), my $out);
return $self->finish(1009) if length $msg;
return $self->finish(1009) if $msg ne '';
$msg = $out;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/URL.pm
Expand Up @@ -101,7 +101,7 @@ sub path {
sub path_query {
my $self = shift;
my $query = $self->query->to_string;
return $self->path->to_string . (length $query ? "?$query" : '');
return $self->path->to_string . ($query eq '' ? '' : "?$query");
}

sub protocol { lc(shift->scheme // '') }
Expand Down Expand Up @@ -153,7 +153,7 @@ sub to_abs {
= $abs->path($base_path->clone)->path->trailing_slash(0)->canonicalize;

# Query
return $abs if length $abs->query->to_string;
return $abs if $abs->query->to_string ne '';
$abs->query($base->query->clone);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent/CookieJar.pm
Expand Up @@ -24,7 +24,7 @@ sub add {
next unless my $domain = lc($cookie->domain // $origin);
$domain =~ s/^\.//;
next unless my $path = $cookie->path;
next unless length(my $name = $cookie->name // '');
next if (my $name = $cookie->name // '') eq '';
my $jar = $self->{jar}{$domain} ||= [];
@$jar = (grep({ _compare($_, $path, $name, $origin) } @$jar), $cookie);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Util.pm
Expand Up @@ -137,7 +137,7 @@ sub punycode_decode {
# Consume all code points before the last delimiter
push @output, split('', $1) if $input =~ s/(.*)\x2d//s;

while (length $input) {
while ($input ne '') {
my $oldi = $i;
my $w = 1;

Expand Down Expand Up @@ -364,7 +364,7 @@ sub _decode {

# Find entity name
my $rest = '';
while (length $name) {
while ($name ne '') {
return "$ENTITIES{$name}$rest" if exists $ENTITIES{$name};
$rest = chop($name) . $rest;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Command/get.pm
Expand Up @@ -83,7 +83,7 @@ sub _json {
say encode_json($data);
}

sub _say { length && say encode('UTF-8', $_) for @_ }
sub _say { $_ ne '' && say encode('UTF-8', $_) for @_ }

sub _select {
my ($buffer, $selector, $charset, @args) = @_;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -1057,7 +1057,7 @@ Sending a streaming request is almost just as easy.
$drain = sub {
my $content = shift;
my $chunk = substr $body, 0, 1, '';
$drain = undef unless length $body;
$drain = undef if $body eq '';
$content->write($chunk, $drain);
};
$tx->req->content->$drain;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Routes/Match.pm
Expand Up @@ -65,7 +65,7 @@ sub _match {
return undef if $r->is_websocket && !$options->{websocket};
# Partial
my $empty = !length $path || $path eq '/';
my $empty = $path eq '' || $path eq '/';
if ($partial) {
$captures->{path} = $path;
$self->endpoint($r);
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Validator/Validation.pm
Expand Up @@ -69,7 +69,7 @@ sub optional {
my $input = $self->input->{$name};
my @input = ref $input eq 'ARRAY' ? @$input : $input;
$self->output->{$name} = $input
unless grep { !defined($_) || !length($_) } @input;
unless grep { !defined($_) || $_ eq '' } @input;

return $self->topic($name);
}
Expand Down

0 comments on commit 20bb97d

Please sign in to comment.