@@ -18,6 +18,7 @@ module CGI::Escape
1818 # url_encoded_string = CGI.escape("'Stop!' said Fred")
1919 # # => "%27Stop%21%27+said+Fred"
2020 def escape ( string )
21+ string = string_value ( string )
2122 encoding = string . encoding
2223 buffer = string . b
2324 buffer . gsub! ( /([^ a-zA-Z0-9_.\- ~]+)/ ) do |m |
@@ -31,6 +32,7 @@ def escape(string)
3132 # string = CGI.unescape("%27Stop%21%27+said+Fred")
3233 # # => "'Stop!' said Fred"
3334 def unescape ( string , encoding = @@accept_charset )
35+ string = string_value ( string )
3436 str = string . tr ( '+' , ' ' )
3537 str = str . b
3638 str . gsub! ( /((?:%[0-9a-fA-F]{2})+)/ ) do |m |
@@ -45,6 +47,7 @@ def unescape(string, encoding = @@accept_charset)
4547 # url_encoded_string = CGI.escapeURIComponent("'Stop!' said Fred")
4648 # # => "%27Stop%21%27%20said%20Fred"
4749 def escapeURIComponent ( string )
50+ string = string_value ( string )
4851 encoding = string . encoding
4952 buffer = string . b
5053 buffer . gsub! ( /([^a-zA-Z0-9_.\- ~]+)/ ) do |m |
@@ -58,6 +61,7 @@ def escapeURIComponent(string)
5861 # string = CGI.unescapeURIComponent("%27Stop%21%27+said%20Fred")
5962 # # => "'Stop!'+said Fred"
6063 def unescapeURIComponent ( string , encoding = @@accept_charset )
64+ string = string_value ( string )
6165 str = string . b
6266 str . gsub! ( /((?:%[0-9a-fA-F]{2})+)/ ) do |m |
6367 [ m . delete ( '%' ) ] . pack ( 'H*' )
@@ -81,6 +85,7 @@ def unescapeURIComponent(string, encoding = @@accept_charset)
8185 # CGI.escapeHTML('Usage: foo "bar" <baz>')
8286 # # => "Usage: foo "bar" <baz>"
8387 def escapeHTML ( string )
88+ string = string_value ( string )
8489 enc = string . encoding
8590 unless enc . ascii_compatible?
8691 if enc . dummy?
@@ -103,6 +108,7 @@ def escapeHTML(string)
103108 # CGI.unescapeHTML("Usage: foo "bar" <baz>")
104109 # # => "Usage: foo \"bar\" <baz>"
105110 def unescapeHTML ( string )
111+ string = string_value ( string )
106112 enc = string . encoding
107113 unless enc . ascii_compatible?
108114 if enc . dummy?
@@ -224,4 +230,10 @@ def unescapeElement(string, *elements)
224230 # Synonym for CGI.unescapeElement(str)
225231 alias unescape_element unescapeElement
226232
233+ private
234+
235+ # Like StringValue in C
236+ def string_value ( input ) # :nodoc:
237+ String . try_convert ( input ) || raise ( TypeError , "no implicit conversion of #{ input . class } into String" )
238+ end
227239end
0 commit comments