module CGI::Util

def unescape(string, encoding = @@accept_charset)

# => "'Stop!' said Fred"
string = CGI.unescape("%27Stop%21%27+said+Fred")
URL-decode an application/x-www-form-urlencoded string with encoding(optional).
def unescape(string, encoding = @@accept_charset)
  str = string.tr('+', ' ')
  str = str.b
  str.gsub!(/((?:%[0-9a-fA-F]{2})+)/) do |m|
    [m.delete('%')].pack('H*')
  end
  str.force_encoding(encoding)
  str.valid_encoding? ? str : str.force_encoding(string.encoding)
end