Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 23, 2012
1 parent dc0f88f commit 6b59143
Show file tree
Hide file tree
Showing 50 changed files with 365 additions and 263 deletions.
4 changes: 2 additions & 2 deletions Changes
@@ -1,8 +1,8 @@

3.51 2012-10-22
3.51 2012-10-23
- Improved documentation.
- Improved tests.
- Fixed json_is bug and warning in Test::Mojo.
- Fixed multiple small context bugs.

3.50 2012-10-20
- Improved option handling of all commands.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Content.pm
Expand Up @@ -17,7 +17,7 @@ sub body_size { croak 'Method "body_size" not implemented by subclass' }
sub boundary {
my $type = shift->headers->content_type || '';
$type =~ m!multipart.*boundary="*([a-zA-Z0-9'(),.:?\-_+/]+)!i and return $1;
return;
return undef;
}

sub build_body { shift->_build('get_body_chunk') }
Expand All @@ -30,7 +30,7 @@ sub charset {

sub clone {
my $self = shift;
return if $self->is_dynamic;
return undef if $self->is_dynamic;
return $self->new(headers => $self->headers->clone);
}

Expand Down
18 changes: 9 additions & 9 deletions lib/Mojo/Content/MultiPart.pm
Expand Up @@ -17,7 +17,7 @@ sub body_contains {
return 1 if index($part->build_headers, $chunk) >= 0;
return 1 if $part->body_contains($chunk);
}
return;
return undef;
}

sub body_size {
Expand Down Expand Up @@ -63,7 +63,7 @@ sub build_boundary {

sub clone {
my $self = shift;
return unless my $clone = $self->SUPER::clone();
return undef unless my $clone = $self->SUPER::clone();
return $clone->parts($self->parts);
}

Expand Down Expand Up @@ -119,18 +119,18 @@ sub _parse_multipart_body {
my $pos = index $self->{multipart}, "\x0d\x0a--$boundary";
if ($pos < 0) {
my $len = length($self->{multipart}) - (length($boundary) + 8);
return unless $len > 0;
return undef unless $len > 0;

# Store chunk
my $chunk = substr $self->{multipart}, 0, $len, '';
$self->parts->[-1] = $self->parts->[-1]->parse($chunk);
return;
return undef;
}

# Store chunk
my $chunk = substr $self->{multipart}, 0, $pos, '';
$self->parts->[-1] = $self->parts->[-1]->parse($chunk);
return $self->{multi_state} = 'multipart_boundary';
return !!($self->{multi_state} = 'multipart_boundary');
}

sub _parse_multipart_boundary {
Expand All @@ -144,7 +144,7 @@ sub _parse_multipart_boundary {
my $part = Mojo::Content::Single->new(relaxed => 1);
$self->emit(part => $part);
push @{$self->parts}, $part;
return $self->{multi_state} = 'multipart_body';
return !!($self->{multi_state} = 'multipart_body');
}

# Boundary ends
Expand All @@ -156,7 +156,7 @@ sub _parse_multipart_boundary {
$self->{state} = $self->{multi_state} = 'finished';
}

return;
return undef;
}

sub _parse_multipart_preamble {
Expand All @@ -168,11 +168,11 @@ sub _parse_multipart_preamble {
substr $self->{multipart}, 0, $pos, "\x0d\x0a";

# Parse boundary
return $self->{multi_state} = 'multipart_boundary';
return !!($self->{multi_state} = 'multipart_boundary');
}

# No boundary yet
return;
return undef;
}

sub _read {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Content/Single.pm
Expand Up @@ -23,7 +23,7 @@ sub body_size {

sub clone {
my $self = shift;
return unless my $clone = $self->SUPER::clone();
return undef unless my $clone = $self->SUPER::clone();
return $clone->asset($self->asset);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/DOM.pm
Expand Up @@ -155,7 +155,7 @@ sub parent {
my $self = shift;

# Not a tag
return if (my $tree = $self->tree)->[0] eq 'root';
return undef if (my $tree = $self->tree)->[0] eq 'root';

# Parent
return $self->new->charset($self->charset)->tree($tree->[3])
Expand Down
44 changes: 26 additions & 18 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -67,10 +67,10 @@ sub select {
sub _ancestor {
my ($self, $selectors, $current, $tree) = @_;
while ($current = $current->[3]) {
return if $current->[0] eq 'root' || $current eq $tree;
return undef if $current->[0] eq 'root' || $current eq $tree;
return 1 if $self->_combinator($selectors, $current, $tree);
}
return;
return undef;
}

sub _attr {
Expand All @@ -84,32 +84,38 @@ sub _attr {
return 1 if $attrs->{$name} =~ $regex;
}

return;
return undef;
}

sub _combinator {
my ($self, $selectors, $current, $tree) = @_;

# Selector
my @s = @$selectors;
return unless my $combinator = shift @s;
return undef unless my $combinator = shift @s;
if ($combinator->[0] ne 'combinator') {
return unless $self->_selector($combinator, $current);
return undef unless $self->_selector($combinator, $current);
return 1 unless $combinator = shift @s;
}

# " " (ancestor)
my $c = $combinator->[1];
if ($c eq ' ') { return unless $self->_ancestor(\@s, $current, $tree) }
if ($c eq ' ') { return undef unless $self->_ancestor(\@s, $current, $tree) }

# ">" (parent only)
elsif ($c eq '>') { return unless $self->_parent(\@s, $current, $tree) }
elsif ($c eq '>') {
return undef unless $self->_parent(\@s, $current, $tree);
}

# "~" (preceding siblings)
elsif ($c eq '~') { return unless $self->_sibling(\@s, $current, $tree, 0) }
elsif ($c eq '~') {
return undef unless $self->_sibling(\@s, $current, $tree, 0);
}

# "+" (immediately preceding siblings)
elsif ($c eq '+') { return unless $self->_sibling(\@s, $current, $tree, 1) }
elsif ($c eq '+') {
return undef unless $self->_sibling(\@s, $current, $tree, 1);
}

return 1;
}
Expand Down Expand Up @@ -204,8 +210,8 @@ sub _equation {

sub _parent {
my ($self, $selectors, $current, $tree) = @_;
return unless my $parent = $current->[3];
return if $parent->[0] eq 'root';
return undef unless my $parent = $current->[3];
return undef if $parent->[0] eq 'root';
return $self->_combinator($selectors, $parent, $tree) ? 1 : undef;
}

Expand Down Expand Up @@ -281,19 +287,19 @@ sub _pc {
for my $i ($start .. $#$parent) {
my $sibling = $parent->[$i];
next if $sibling->[0] ne 'tag' || $sibling eq $current;
return unless defined $type && $sibling->[1] ne $type;
return undef unless defined $type && $sibling->[1] ne $type;
}

# No siblings
return 1;
}

return;
return undef;
}

sub _regex {
my ($self, $op, $value) = @_;
return unless defined $value;
return undef unless defined $value;
$value = quotemeta $self->_unescape($value);

# "~=" (word)
Expand Down Expand Up @@ -322,15 +328,17 @@ sub _selector {
# Tag (ignore namespace prefix)
if ($type eq 'tag') {
my $tag = $s->[1];
return unless $tag eq '*' || $current->[1] =~ /(?:^|:)$tag$/;
return undef unless $tag eq '*' || $current->[1] =~ /(?:^|:)$tag$/;
}

# Attribute
elsif ($type eq 'attr') { return unless $self->_attr(@$s[1, 2], $current) }
elsif ($type eq 'attr') {
return undef unless $self->_attr(@$s[1, 2], $current);
}

# Pseudo class
elsif ($type eq 'pc') {
return unless $self->_pc(lc $s->[1], $s->[2], $current);
return undef unless $self->_pc(lc $s->[1], $s->[2], $current);
}
}

Expand All @@ -355,7 +363,7 @@ sub _sibling {
else { return 1 if $self->_combinator($selectors, $e, $tree) }
}

return;
return undef;
}

sub _unescape {
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/IOLoop.pm
Expand Up @@ -170,7 +170,7 @@ sub stream {
return $self->_stream($stream, $self->_id) if blessed $stream;

# Find stream for id
return unless my $c = $self->{connections}{$stream};
return undef unless my $c = $self->{connections}{$stream};
return $c->{stream};
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/IOLoop/Stream.pm
Expand Up @@ -39,7 +39,7 @@ sub is_readable {

sub is_writing {
my $self = shift;
return unless exists $self->{handle};
return undef unless exists $self->{handle};
return !!length($self->{buffer}) || $self->has_subscribers('drain');
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/JSON.pm
Expand Up @@ -44,13 +44,13 @@ sub decode {
$self->error(undef);

# Missing input
$self->error('Missing or empty input') and return unless $bytes;
$self->error('Missing or empty input') and return undef unless $bytes;

# Remove BOM
$bytes =~ s/^(?:\357\273\277|\377\376\0\0|\0\0\376\377|\376\377|\377\376)//g;

# Wide characters
$self->error('Wide character in input') and return
$self->error('Wide character in input') and return undef
unless utf8::downgrade($bytes, 1);

# Detect and decode Unicode
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/JSON/Pointer.pm
Expand Up @@ -26,7 +26,7 @@ sub _pointer {
}

# Nothing
else {return}
else { return undef }
}

return $contains ? 1 : $data;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Loader.pm
Expand Up @@ -21,7 +21,7 @@ sub load {
return 1 if !$module || $module !~ /^\w(?:[\w:']*\w)?$/;

# Load
return if $module->can('new') || eval "require $module; 1";
return undef if $module->can('new') || eval "require $module; 1";

# Exists
my $path = class_to_path $module;
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/Message.pm
Expand Up @@ -94,7 +94,7 @@ sub cookies { croak 'Method "cookies" not implemented by subclass' }
sub dom {
my $self = shift;

return if $self->is_multipart;
return undef if $self->is_multipart;
my $dom = $self->{dom}
||= Mojo::DOM->new->charset($self->content->charset // undef)
->parse($self->body);
Expand Down Expand Up @@ -178,15 +178,15 @@ sub is_dynamic { shift->content->is_dynamic }
sub is_finished { (shift->{state} // '') eq 'finished' }

sub is_limit_exceeded {
return unless my $code = (shift->error)[1];
return undef unless my $code = (shift->error)[1];
return !!grep { $_ eq $code } 413, 431;
}

sub is_multipart { shift->content->is_multipart }

sub json {
my ($self, $pointer) = @_;
return if $self->is_multipart;
return undef if $self->is_multipart;
my $data = $self->{json} ||= Mojo::JSON->new->decode($self->body);
return $pointer ? Mojo::JSON::Pointer->new->get($data, $pointer) : $data;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/Message/Request.pm
Expand Up @@ -23,7 +23,7 @@ sub clone {
my $self = shift;

# Dynamic requests cannot be cloned
return unless my $content = $self->content->clone;
return undef unless my $content = $self->content->clone;
my $clone = $self->new(
content => $content,
method => $self->method,
Expand Down Expand Up @@ -59,10 +59,10 @@ sub extract_start_line {

# Ignore any leading empty lines
$$bufferref =~ s/^\s+//;
return unless defined(my $line = get_line $bufferref);
return undef unless defined(my $line = get_line $bufferref);

# We have a (hopefully) full request line
$self->error('Bad request start line', 400) and return
$self->error('Bad request start line', 400) and return undef
unless $line =~ $START_LINE_RE;
my $url = $self->method($1)->version($3)->url;
return !!($1 eq 'CONNECT' ? $url->authority($2) : $url->parse($2));
Expand Down Expand Up @@ -199,7 +199,7 @@ sub proxy {
sub query_params { shift->url->query }

sub _parse_basic_auth {
return unless my $header = shift;
return undef unless my $header = shift;
return $header =~ /Basic (.+)$/ ? b64_decode($1) : undef;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/Message/Response.pm
Expand Up @@ -95,8 +95,8 @@ sub extract_start_line {
my ($self, $bufferref) = @_;

# We have a full response line
return unless defined(my $line = get_line $bufferref);
$self->error('Bad response start line') and return
return undef unless defined(my $line = get_line $bufferref);
$self->error('Bad response start line') and return undef
unless $line =~ m!^\s*HTTP/(\d\.\d)\s+(\d\d\d)\s*(.+)?$!;
$self->content->skip_body(1) if $self->code($2)->is_empty;
return !!$self->version($1)->message($3)->content->auto_relax(1);
Expand Down Expand Up @@ -132,13 +132,13 @@ sub get_start_line_chunk {

sub is_empty {
my $self = shift;
return unless my $code = $self->code;
return undef unless my $code = $self->code;
return $self->is_status_class(100) || grep { $_ eq $code } qw(204 304);
}

sub is_status_class {
my ($self, $class) = @_;
return unless my $code = $self->code;
return undef unless my $code = $self->code;
return $code >= $class && $code < ($class + 100);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Path.pm
Expand Up @@ -51,7 +51,7 @@ sub contains {
my $parts = $self->new($path)->parts;
for my $part (@{$self->parts}) {
return 1 unless defined(my $try = shift @$parts);
return unless $part eq $try;
return undef unless $part eq $try;
}

return !@$parts;
Expand Down

0 comments on commit 6b59143

Please sign in to comment.