Class: CGI
Overview
This is a very small part of the CGI class, borrowed from the Rubinius sources
Constant Summary collapse
- @@accept_charset =
"UTF-8"
Class Method Summary collapse
-
.escape(string) ⇒ Object
URL-encode a string.
-
.unescape(string, encoding = @@accept_charset) ⇒ Object
URL-decode a string with encoding(optional).
Class Method Details
.escape(string) ⇒ Object
URL-encode a string.
url_encoded_string = CGI::escape("'Stop!' said Fred")
# => "%27Stop%21%27+said+Fred"
7 8 9 10 11 |
# File 'motion/_stdlib/cgi.rb', line 7 def CGI::escape(string) string.gsub(/([^ a-zA-Z0-9_.-]+)/) do '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase end.tr(' ', '+') end |
.unescape(string, encoding = @@accept_charset) ⇒ Object
URL-decode a string with encoding(optional).
string = CGI::unescape("%27Stop%21%27+said+Fred")
# => "'Stop!' said Fred"
16 17 18 19 20 21 |
# File 'motion/_stdlib/cgi.rb', line 16 def CGI::unescape(string,encoding=@@accept_charset) str=string.tr('+', ' ').force_encoding(Encoding::ASCII_8BIT).gsub(/((?:%[0-9a-fA-F]{2})+)/) do [$1.delete('%')].pack('H*') end.force_encoding(encoding) str.valid_encoding? ? str : str.force_encoding(string.encoding) end |