Skip to content

Commit

Permalink
length might be a little faster
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 2, 2016
1 parent a5dbee7 commit d19e4fd
Show file tree
Hide file tree
Showing 25 changed files with 42 additions and 42 deletions.
12 changes: 6 additions & 6 deletions lib/Mojo/Content.pm
Expand Up @@ -38,9 +38,9 @@ sub clone {
sub generate_body_chunk {
my ($self, $offset) = @_;

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

return $chunk;
}
Expand Down Expand Up @@ -103,7 +103,7 @@ sub parse {
# Relaxed parsing
my $headers = $self->headers;
my $len = $headers->content_length // '';
if ($self->auto_relax && $len eq '') {
if ($self->auto_relax && !length $len) {
my $connection = lc($headers->connection // '');
$self->relaxed(1)
if $connection eq 'close' || (!$connection && $self->expect_close);
Expand Down Expand Up @@ -149,7 +149,7 @@ sub write {
$self->{dynamic} = 1;
$self->{body_buffer} .= $chunk if defined $chunk;
$self->once(drain => $cb) if $cb;
$self->{eof} = 1 if defined $chunk && $chunk eq '';
$self->{eof} = 1 if defined $chunk && !length $chunk;

return $self;
}
Expand All @@ -158,15 +158,15 @@ sub write_chunk {
my ($self, $chunk, $cb) = @_;
$self->headers->transfer_encoding('chunked') unless $self->is_chunked;
$self->write(defined $chunk ? $self->_build_chunk($chunk) : $chunk, $cb);
$self->{eof} = 1 if defined $chunk && $chunk eq '';
$self->{eof} = 1 if defined $chunk && !length $chunk;
return $self;
}

sub _build_chunk {
my ($self, $chunk) = @_;

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

# 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 '' if (my $name = $self->name // '') eq '';
return '' unless length(my $name = $self->name // '');
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 @@ -33,7 +33,7 @@ sub to_string {
my $self = shift;

# Name and value
return '' if (my $name = $self->name // '') eq '';
return '' unless length(my $name = $self->name // '');
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 @@ -145,7 +145,7 @@ sub _equation {
# "n", "4n", "+4n", "-4n", "n+1", "4n-1", "+4n-1" (and other variations)
return [0, 0]
unless $equation =~ /^\s*((?:\+|-)?(?:\d+)?)?n\s*((?:\+|-)\s*\d+)?\s*$/i;
return [$1 eq '-' ? -1 : $1 eq '' ? 1 : $1, join('', split(' ', $2 // 0))];
return [$1 eq '-' ? -1 : !length $1 ? 1 : $1, join('', split(' ', $2 // 0))];
}

sub _match {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Home.pm
Expand Up @@ -23,7 +23,7 @@ sub detect {
my @home = splitdir $path;

# Remove "lib" and "blib"
pop @home while @home && ($home[-1] =~ /^b?lib$/ || $home[-1] eq '');
pop @home while @home && ($home[-1] =~ /^b?lib$/ || !length $home[-1]);

# Turn into absolute path
return $self->parts([splitdir abs_path catdir(@home) || '.']);
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -81,7 +81,7 @@ sub write {

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

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

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

$self->emit('drain') if $self->{buffer} eq '';
return if $self->is_writing;
return $self->close if $self->{graceful};
$self->emit('drain') unless length $self->{buffer};
return if $self->is_writing;
return $self->close if $self->{graceful};
$self->reactor->watch($handle, !$self->{paused}, 0) if $self->{handle};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/JSON.pm
Expand Up @@ -54,7 +54,7 @@ sub _decode {
eval {

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

# UTF-8
$_ = Mojo::Util::decode 'UTF-8', $_ unless shift;
Expand Down Expand Up @@ -251,7 +251,7 @@ sub _encode_value {
# Number
no warnings 'numeric';
return $value
if ((my $dummy = '') & $value) ne ''
if length((my $dummy = '') & $value)
&& 0 + $value eq $value
&& $value * 0 == 0;

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/JSON/Pointer.pm
Expand Up @@ -13,7 +13,7 @@ sub _pointer {

my $data = $self->data;
return $contains ? 1 : $data unless $pointer =~ s!^/!!;
for my $p ($pointer eq '' ? ($pointer) : (split '/', $pointer, -1)) {
for my $p (length $pointer ? (split '/', $pointer, -1) : ($pointer)) {
$p =~ s!~1!/!g;
$p =~ s/~0/~/g;

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 || $chunk ne '';
return $chunk if !defined $chunk || length $chunk;
$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 if $str eq '';
return $pairs unless length $str;

my $charset = $self->charset;
for my $pair (split '&', $str) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Path.pm
Expand Up @@ -15,7 +15,7 @@ sub canonicalize {

my $parts = $self->parts;
for (my $i = 0; $i <= $#$parts;) {
if ($parts->[$i] eq '' || $parts->[$i] eq '.' || $parts->[$i] eq '...') {
if (!length $parts->[$i] || $parts->[$i] eq '.' || $parts->[$i] eq '...') {
splice @$parts, $i, 1;
}
elsif ($i < 1 || $parts->[$i] ne '..' || $parts->[$i - 1] eq '..') { $i++ }
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -148,7 +148,7 @@ sub _finish {
return $self->_remove($id) if $tx->error || !$tx->keep_alive;

# Build new transaction for leftovers
return if (my $leftovers = $tx->req->content->leftovers) eq '';
return unless length(my $leftovers = $tx->req->content->leftovers);
$tx = $c->{tx} = $self->_build_tx($id, $c);
$tx->server_read($leftovers);
}
Expand Down Expand Up @@ -228,7 +228,7 @@ sub _write {
local $c->{writing} = 1;
my $chunk = $tx->server_write;
warn term_escape "-- Server >>> Client (@{[_url($tx)]})\n$chunk\n" if DEBUG;
my $next = $tx->is_finished ? '_finish' : $chunk eq '' ? undef : '_write';
my $next = $tx->is_finished ? '_finish' : length $chunk ? '_write' : undef;
return $self->ioloop->stream($id)->write($chunk) unless $next;
weaken $self;
$self->ioloop->stream($id)->write($chunk => sub { $self->$next($id) });
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 if $chunk eq '';
return undef unless length $chunk;

$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 $value ne '';
$blocks[-1] .= "\$_O .= \"" . $value . "\";" if length $value;
}

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

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

# Finished
$finish ? $self->completed : ($self->{writing} = 0)
if $self->{write} <= 0 || defined $buffer && $buffer eq '';
if $self->{write} <= 0 || defined $buffer && !length $buffer;

return $buffer // '';
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Transaction/WebSocket.pm
Expand Up @@ -114,7 +114,7 @@ sub parse_message {
WindowBits => -15
);
$inflate->inflate(($msg .= "\x00\x00\xff\xff"), my $out);
return $self->finish(1009) if $msg ne '';
return $self->finish(1009) if length $msg;
$msg = $out;
}

Expand Down Expand Up @@ -157,8 +157,8 @@ sub server_read {

sub server_write {
my $self = shift;
$self->emit('drain') if ($self->{write} //= '') eq '';
$self->completed if $self->{write} eq '' && $self->{closing};
$self->emit('drain') unless length($self->{write} //= '');
$self->completed if !length $self->{write} && $self->{closing};
return delete $self->{write};
}

Expand Down
6 changes: 3 additions & 3 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 . ($query eq '' ? '' : "?$query");
return $self->path->to_string . (length $query ? "?$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 $abs->query->to_string ne '';
return $abs if length $abs->query->to_string;
$abs->query($base->query->clone);
}

Expand All @@ -176,7 +176,7 @@ sub to_string {

# Path and query
my $path = $self->path_query;
$url .= !$authority || $path eq '' || $path =~ m!^[/?]! ? $path : "/$path";
$url .= !$authority || !length $path || $path =~ m!^[/?]! ? $path : "/$path";

# Fragment
return $url unless defined(my $fragment = $self->fragment);
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -321,7 +321,7 @@ sub _write {
local $c->{writing} = 1;
my $chunk = $tx->client_write;
warn term_escape "-- Client >>> Server (@{[_url($tx)]})\n$chunk\n" if DEBUG;
return if $chunk eq '';
return unless length $chunk;
weaken $self;
$c->{ioloop}->stream($id)->write($chunk => sub { $self->_write($id) });
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent/CookieJar.pm
Expand Up @@ -25,7 +25,7 @@ sub add {
my $origin = $cookie->origin // '';
next unless my $domain = lc($cookie->domain // $origin);
next unless my $path = $cookie->path;
next if (my $name = $cookie->name // '') eq '';
next unless length(my $name = $cookie->name // '');
my $jar = $self->{jar}{$domain} ||= [];
@$jar = (grep({ _compare($_, $path, $name, $origin) } @$jar), $cookie);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Util.pm
Expand Up @@ -138,7 +138,7 @@ sub punycode_decode {
# Consume all code points before the last delimiter
push @output, split('', $1) if $input =~ s/(.*)\x2d//s;

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

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -67,7 +67,7 @@ sub run {

# JSON Pointer
return unless defined $selector;
return _json($buffer, $selector) if $selector eq '' || $selector =~ m!^/!;
return _json($buffer, $selector) if !length $selector || $selector =~ m!^/!;

# Selector
$charset //= $tx->res->content->charset || $tx->res->default_charset;
Expand All @@ -83,7 +83,7 @@ sub _json {
say encode_json($data);
}

sub _say { $_ ne '' && say encode('UTF-8', $_) for @_ }
sub _say { length && 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 @@ -1067,7 +1067,7 @@ Sending a streaming request is almost just as easy.
$drain = sub {
my $content = shift;
my $chunk = substr $body, 0, 1, '';
$drain = undef if $body eq '';
$drain = undef unless length $body;
$content->write($chunk, $drain);
};
$tx->req->content->$drain;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Renderer.pm
Expand Up @@ -71,7 +71,7 @@ sub get_helper {

my $found;
my $class = 'Mojolicious::Renderer::Helpers::' . md5_sum "$name:$self";
my $re = $name eq '' ? qr/^(([^.]+))/ : qr/^(\Q$name\E\.([^.]+))/;
my $re = length $name ? qr/^(\Q$name\E\.([^.]+))/ : qr/^(([^.]+))/;
for my $key (keys %{$self->helpers}) {
$key =~ $re ? ($found, my $method) = (1, $2) : next;
my $sub = $self->get_helper($1);
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 = $path eq '' || $path eq '/';
my $empty = !length $path || $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 @@ -73,7 +73,7 @@ sub optional {
@input = map { $self->$cb($name, $_) } @input;
}
$self->output->{$name} = @input > 1 ? \@input : $input[0]
unless grep { $_ eq '' } @input;
unless grep { !length } @input;

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

0 comments on commit d19e4fd

Please sign in to comment.