From 78fd550840f539125f8b304b97631a690c554173 Mon Sep 17 00:00:00 2001 From: akr Date: Mon, 24 Dec 2007 14:59:34 +0000 Subject: * lib/cgi.rb (CGI::escape): m17nized. (CGI::unescape): ditto. (CGI::escapeHTML): ditto. (CGI::unescapeHTML): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14614 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/cgi.rb | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'lib/cgi.rb') diff --git a/lib/cgi.rb b/lib/cgi.rb index 9af4c9cf24..963b6b1fc3 100644 --- a/lib/cgi.rb +++ b/lib/cgi.rb @@ -339,8 +339,8 @@ class CGI # url_encoded_string = CGI::escape("'Stop!' said Fred") # # => "%27Stop%21%27+said+Fred" def CGI::escape(string) - string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do - '%' + $1.unpack('H2' * $1.size).join('%').upcase + string.gsub(/([^ a-zA-Z0-9_.-]+)/) do + '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase end.tr(' ', '+') end @@ -349,8 +349,9 @@ class CGI # string = CGI::unescape("%27Stop%21%27+said+Fred") # # => "'Stop!' said Fred" def CGI::unescape(string) - string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do - [$1.delete('%')].pack('H*') + enc = string.encoding + string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/) do + [$1.delete('%')].pack('H*').force_encoding(enc) end end @@ -359,7 +360,7 @@ class CGI # CGI::escapeHTML('Usage: foo "bar" ') # # => "Usage: foo "bar" <baz>" def CGI::escapeHTML(string) - string.gsub(/&/n, '&').gsub(/\"/n, '"').gsub(/>/n, '>').gsub(//, '>').gsub(/ "Usage: foo \"bar\" " def CGI::unescapeHTML(string) - string.gsub(/&(amp|quot|gt|lt|\#[0-9]+|\#x[0-9A-Fa-f]+);/n) do + enc = string.encoding + string.gsub(/&(amp|quot|gt|lt|\#[0-9]+|\#x[0-9A-Fa-f]+);/) do match = $1.dup case match when 'amp' then '&' when 'quot' then '"' when 'gt' then '>' when 'lt' then '<' - when /\A#0*(\d+)\z/n then + when /\A#0*(\d+)\z/ then if Integer($1) < 256 - Integer($1).chr + Integer($1).chr.force_encoding(enc) else "&##{$1};" end - when /\A#x([0-9a-f]+)\z/ni then + when /\A#x([0-9a-f]+)\z/i then if $1.hex < 256 - $1.hex.chr + $1.hex.chr.force_encoding(enc) else "&#x#{$1};" end -- cgit v1.2.3