Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 24, 2014
1 parent 8170c4b commit ab50193
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
28 changes: 11 additions & 17 deletions lib/Mojo/Template.pm
Expand Up @@ -31,41 +31,35 @@ sub build {
for my $line (@{$self->tree}) {
push @lines, '';
for (my $j = 0; $j < @{$line}; $j += 2) {
my $type = $line->[$j];
my $value = $line->[$j + 1] || '';
my $newline = chomp $value;
my ($op, $value) = @$line[$j, $j + 1];
my $newline = chomp($value //= '');

# Capture end
if ($type eq 'cpen') {

# End block
if ($op eq 'cpen') {
$lines[-1] .= 'return Mojo::ByteStream->new($_M) }';

# No following code
my $next = $line->[$j + 3];
$lines[-1] .= ';' if !defined $next || $next =~ /^\s*$/;
}

# Text
if ($type eq 'text') {

# Quote and fix line ending
$value = quotemeta $value;
$value .= '\n' if $newline;
# Text (quote and fix line ending)
if ($op eq 'text') {
$value = $newline ? quotemeta($value) . '\n' : quotemeta $value;
$lines[-1] .= "\$_M .= \"" . $value . "\";" if length $value;
}

# Code or multiline expression
if ($type eq 'code' || $multi) { $lines[-1] .= "$value" }
if ($op eq 'code' || $multi) { $lines[-1] .= "$value" }

# Expression
if ($type eq 'expr' || $type eq 'escp') {
if ($op eq 'expr' || $op eq 'escp') {

# Start
unless ($multi) {

# Escaped
if (($type eq 'escp' && !$escape) || ($type eq 'expr' && $escape)) {
if (($op eq 'escp' && !$escape) || ($op eq 'expr' && $escape)) {
$lines[-1] .= "\$_M .= _escape";
$lines[-1] .= " scalar $value" if length $value;
}
Expand All @@ -79,15 +73,15 @@ sub build {
&& ($line->[$j + 3] // '') eq '');

# Append semicolon
$lines[-1] .= ';' if !$multi && !$cpst;
$lines[-1] .= ';' unless $multi || $cpst;
}

# Capture start
if ($cpst) {
$lines[-1] .= $cpst;
$cpst = undef;
}
$cpst = " sub { my \$_M = ''; " if $type eq 'cpst';
$cpst = " sub { my \$_M = ''; " if $op eq 'cpst';
}
}

Expand Down
37 changes: 18 additions & 19 deletions lib/Mojolicious/Routes/Pattern.pm
Expand Up @@ -70,31 +70,30 @@ sub render {

my $str = '';
for my $token (reverse @{$self->tree}) {
my $op = $token->[0];
my $rendered = '';
my ($op, $value) = @$token[0, 1];
my $fragment = '';

# Slash
if ($op eq 'slash') { $rendered = '/' unless $optional }
if ($op eq 'slash') { $fragment = '/' unless $optional }

# Text
elsif ($op eq 'text') {
$rendered = $token->[1];
$fragment = $value;
$optional = 0;
}

# Placeholder, relaxed or wildcard
elsif ($op eq 'placeholder' || $op eq 'relaxed' || $op eq 'wildcard') {
my $name = $token->[1];
$rendered = $values->{$name} // '';
my $default = $self->defaults->{$name};
if (!defined $default || ($default ne $rendered)) { $optional = 0 }
elsif ($optional) { $rendered = '' }
$fragment = $values->{$value} // '';
my $default = $self->defaults->{$value};
if (!defined $default || ($default ne $fragment)) { $optional = 0 }
elsif ($optional) { $fragment = '' }
}

$str = "$rendered$str";
$str = "$fragment$str";
}

# Format is optional
# Format can be optional
$str ||= '/';
return $render && $format ? "$str.$format" : $str;
}
Expand All @@ -109,7 +108,7 @@ sub _compile {
my $block = my $regex = '';
my $optional = 1;
for my $token (reverse @{$self->tree}) {
my $op = $token->[0];
my ($op, $value) = @$token[0, 1];
my $fragment = '';

# Slash
Expand All @@ -122,13 +121,13 @@ sub _compile {

# Text
elsif ($op eq 'text') {
$fragment = quotemeta $token->[1];
$fragment = quotemeta $value;
$optional = 0;
}

# Placeholder
elsif ($op eq 'placeholder' || $op eq 'relaxed' || $op eq 'wildcard') {
unshift @$placeholders, my $name = $token->[1];
unshift @$placeholders, $value;

# Placeholder
if ($op eq 'placeholder') { $fragment = '([^\/\.]+)' }
Expand All @@ -140,11 +139,11 @@ sub _compile {
elsif ($op eq 'wildcard') { $fragment = '(.+)' }

# Custom regex
my $constraint = $constraints->{$name};
my $constraint = $constraints->{$value};
$fragment = _compile_req($constraint) if $constraint;

# Optional placeholder
exists $defaults->{$name} ? ($fragment .= '?') : ($optional = 0);
exists $defaults->{$value} ? ($fragment .= '?') : ($optional = 0);
}

$block = "$fragment$block";
Expand Down Expand Up @@ -194,9 +193,9 @@ sub _tokenize {

# Quote start
if ($char eq $quote_start) {
$quoted = 1;
push @tree, ['placeholder', ''];
$state = 'placeholder';
$state = 'placeholder';
$quoted = 1;
}

# Placeholder start
Expand All @@ -213,8 +212,8 @@ sub _tokenize {

# Quote end
elsif ($char eq $quote_end) {
$quoted = 0;
$state = 'text';
$quoted = 0;
}

# Slash
Expand Down

0 comments on commit ab50193

Please sign in to comment.