Skip to content
This repository
Browse code

have decompile work properly with Sinatra 1.3

  • Loading branch information...
commit 7c4eb00e4d7839d6675a4bd463813d3ad2edf802 1 parent a946e99
Konstantin Haase authored August 17, 2011
18  lib/sinatra/decompile.rb
... ...
@@ -1,5 +1,6 @@
1 1
 require 'sinatra/base'
2 2
 require 'backports'
  3
+require 'uri'
3 4
 
4 5
 module Sinatra
5 6
 
@@ -73,8 +74,9 @@ def decompile(pattern, keys = nil, *)
73 74
       keys, str     = keys.try(:dup), pattern.inspect
74 75
       return pattern unless str.start_with? '/' and str.end_with? '/'
75 76
       str.gsub! /^\/\^?|\$?\/$/, ''
  77
+      str.gsub! encoded(' '), ' '
76 78
       return pattern if str =~ /^[\.\+]/
77  
-      str.gsub! /\([^\(]*\)/ do |part|
  79
+      str.gsub! /\([^\(\)]*\)/ do |part|
78 80
         case part
79 81
         when '(.*?)'
80 82
           return pattern if keys.shift != 'splat'
@@ -82,6 +84,10 @@ def decompile(pattern, keys = nil, *)
82 84
         when '([^\/?#]+)'
83 85
           return pattern if keys.empty?
84 86
           ":" << keys.shift
  87
+        when /^\(\?\:\\?(.)\|/
  88
+          char = $1
  89
+          return pattern unless encoded(char) == part
  90
+          Regexp.escape(char)
85 91
         else
86 92
           return pattern
87 93
         end
@@ -91,6 +97,16 @@ def decompile(pattern, keys = nil, *)
91 97
         $2
92 98
       end
93 99
     end
  100
+
  101
+    private
  102
+
  103
+    def encoded(char)
  104
+      return super if defined? super
  105
+      enc = URI.encode(char)
  106
+      enc = "(?:#{Regexp.escape enc}|#{URI.encode char, /./})" if enc == char
  107
+      enc = "(?:#{enc}|#{encoded('+')})" if char == " "
  108
+      enc
  109
+    end
94 110
   end
95 111
 
96 112
   register Decompile
2  spec/decompile_spec.rb
@@ -28,6 +28,8 @@
28 28
   it { should decompile("/*/foo/*") }
29 29
   it { should decompile("*") }
30 30
   it { should decompile(":name.:format") }
  31
+  it { should decompile("a b") }
  32
+  it { should decompile("a+b") }
31 33
   it { should decompile(/./) }
32 34
   it { should decompile(/f(oo)/) }
33 35
   it { should decompile(/ba+r/) }

0 notes on commit 7c4eb00

Please sign in to comment.
Something went wrong with that request. Please try again.