-
-
Save anonymous/d5c226f5f42f5f9b1b1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/Mojolicious/Routes/Pattern.pm b/lib/Mojolicious/Routes/Pattern. | |
index 78db954..3b19e59 100644 | |
--- a/lib/Mojolicious/Routes/Pattern.pm | |
+++ b/lib/Mojolicious/Routes/Pattern.pm | |
@@ -62,21 +62,20 @@ sub render { | |
# Placeholders can only be optional without a format | |
my $optional = !(my $format = $values->{format}); | |
- my $str = ''; | |
- my $defaults = $self->defaults; | |
+ my $str = ''; | |
for my $token (reverse @{$self->tree}) { | |
my ($op, $value) = @$token[0, 1]; | |
my $fragment = ''; | |
- # Slash | |
- if ($op eq 'slash') { $fragment = '/' unless $optional } | |
- | |
# Text | |
- elsif ($op eq 'text') { ($fragment, $optional) = ($value, 0) } | |
+ if ($op eq 'text') { ($fragment, $optional) = ($value, 0) } | |
+ | |
+ # Slash | |
+ elsif ($op eq 'slash') { $fragment = '/' unless $optional } | |
# Placeholder | |
else { | |
- my $default = $defaults->{$value}; | |
+ my $default = $self->defaults->{$value}; | |
$fragment = $values->{$value} // $default // ''; | |
if (!defined $default || ($default ne $fragment)) { $optional = 0 } | |
elsif ($optional) { $fragment = '' } | |
@@ -207,9 +206,12 @@ sub _tokenize { | |
# Placeholder, relaxed or wildcard | |
elsif ($inside) { $tree[-1][-1] .= $char } | |
- # Text | |
+ # Text (optimize text followed by slash followed by text) | |
elsif ($tree[-1][0] eq 'text') { $tree[-1][-1] .= $char } | |
- else { push @tree, ['text', $char] } | |
+ elsif ($tree[-2] && $tree[-2][0] eq 'text' && $tree[-1][0] eq 'slash') { | |
+ pop @tree && ($tree[-1][-1] .= "/$char"); | |
+ } | |
+ else { push @tree, ['text', $char] } | |
} | |
return $self->pattern($pattern)->tree(\@tree); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment