Skip to content

Instantly share code, notes, and snippets.

Created August 19, 2014 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d5c226f5f42f5f9b1b1f to your computer and use it in GitHub Desktop.
Save anonymous/d5c226f5f42f5f9b1b1f to your computer and use it in GitHub Desktop.
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