class CGI
Borrowed this from motion-support 0.2.6, as it's all we need. Don't want the dependency
Public Class Methods
escape(string)
click to toggle source
URL-encode a string.
url_encoded_string = CGI::escape("'Stop!' said Fred") # => "%27Stop%21%27+said+Fred"
# File lib/mdurl-rb/cgi.rb, line 10 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)
click to toggle source
URL-decode a string with encoding(optional).
string = CGI::unescape("%27Stop%21%27+said+Fred") # => "'Stop!' said Fred"
# File lib/mdurl-rb/cgi.rb, line 19 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