[前][次][番号順一覧][スレッド一覧]

ruby-changes:42581

From: nobu <ko1@a...>
Date: Wed, 20 Apr 2016 16:36:57 +0900 (JST)
Subject: [ruby-changes:42581] nobu:r54655 (trunk): cgi/util.rb: remove CGI::Util#_unescape

nobu	2016-04-20 17:33:33 +0900 (Wed, 20 Apr 2016)

  New Revision: 54655

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54655

  Log:
    cgi/util.rb: remove CGI::Util#_unescape
    
    * ext/cgi/escape/escape.c (cgiesc_unescape): define unescape
      method instead of _unescape, and should pass the optional
      argument to the super method.
    * lib/cgi/util.rb (CGI::Util#_unescape): remove intermediate
      method.

  Modified files:
    trunk/ChangeLog
    trunk/ext/cgi/escape/escape.c
    trunk/lib/cgi/util.rb
    trunk/test/cgi/test_cgi_util.rb
Index: lib/cgi/util.rb
===================================================================
--- lib/cgi/util.rb	(revision 54654)
+++ lib/cgi/util.rb	(revision 54655)
@@ -16,10 +16,6 @@ module CGI::Util https://github.com/ruby/ruby/blob/trunk/lib/cgi/util.rb#L16
   #   string = CGI::unescape("%27Stop%21%27+said+Fred")
   #      # => "'Stop!' said Fred"
   def unescape(string,encoding=@@accept_charset)
-    _unescape(string,encoding)
-  end
-
-  private def _unescape(string, encoding)
     str=string.tr('+', ' ').b.gsub(/((?:%[0-9a-fA-F]{2})+)/) do |m|
       [m.delete('%')].pack('H*')
     end.force_encoding(encoding)
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 54654)
+++ ChangeLog	(revision 54655)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Apr 20 17:33:31 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/cgi/escape/escape.c (cgiesc_unescape): define unescape
+	  method instead of _unescape, and should pass the optional
+	  argument to the super method.
+
+	* lib/cgi/util.rb (CGI::Util#_unescape): remove intermediate
+	  method.
+
 Wed Apr 20 15:52:28 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* error.c (syntax_error_initialize): move the default message,
Index: ext/cgi/escape/escape.c
===================================================================
--- ext/cgi/escape/escape.c	(revision 54654)
+++ ext/cgi/escape/escape.c	(revision 54655)
@@ -369,17 +369,34 @@ cgiesc_escape(VALUE self, VALUE str) https://github.com/ruby/ruby/blob/trunk/ext/cgi/escape/escape.c#L369
     }
 }
 
-/* :nodoc: */
 static VALUE
-cgiesc_unescape(VALUE self, VALUE str, VALUE enc)
+accept_charset(int argc, VALUE *argv, VALUE self)
 {
+    if (argc > 0)
+	return argv[0];
+    return rb_cvar_get(CLASS_OF(self), rb_intern("@@accept_charset"));
+}
+
+/*
+ *  call-seq:
+ *     CGI.unescape(string, encoding=@@accept_charset) -> string
+ *
+ *  Returns URL-unescaped string.
+ *
+ */
+static VALUE
+cgiesc_unescape(int argc, VALUE *argv, VALUE self)
+{
+    VALUE str = (rb_check_arity(argc, 1, 2), argv[0]);
+
     StringValue(str);
 
     if (rb_enc_str_asciicompat_p(str)) {
+	VALUE enc = accept_charset(argc-1, argv+1, self);
 	return optimized_unescape(str, enc);
     }
     else {
-	return rb_call_super(1, &str);
+	return rb_call_super(argc, argv);
     }
 }
 
@@ -392,7 +409,7 @@ Init_escape(void) https://github.com/ruby/ruby/blob/trunk/ext/cgi/escape/escape.c#L409
     rb_define_method(rb_mEscape, "escapeHTML", cgiesc_escape_html, 1);
     rb_define_method(rb_mEscape, "unescapeHTML", cgiesc_unescape_html, 1);
     rb_define_method(rb_mEscape, "escape", cgiesc_escape, 1);
-    rb_define_private_method(rb_mEscape, "_unescape", cgiesc_unescape, 2);
+    rb_define_method(rb_mEscape, "unescape", cgiesc_unescape, -1);
     rb_prepend_module(rb_mUtil, rb_mEscape);
     rb_extend_object(rb_cCGI, rb_mEscape);
 }
Index: test/cgi/test_cgi_util.rb
===================================================================
--- test/cgi/test_cgi_util.rb	(revision 54654)
+++ test/cgi/test_cgi_util.rb	(revision 54655)
@@ -118,6 +118,23 @@ class CGIUtilTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/cgi/test_cgi_util.rb#L118
     end
   end
 
+  Encoding.list.each do |enc|
+    next unless enc.ascii_compatible?
+    begin
+      escaped = "%25+%2B"
+      unescaped = "% +".encode(enc)
+    rescue Encoding::ConverterNotFoundError
+      next
+    else
+      define_method("test_cgi_escape:#{enc.name}") do
+        assert_equal(escaped, CGI::escape(unescaped))
+      end
+      define_method("test_cgi_unescape:#{enc.name}") do
+        assert_equal(unescaped, CGI::unescape(escaped, enc))
+      end
+    end
+  end
+
   def test_cgi_unescapeHTML_uppercasecharacter
     assert_equal("\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86", CGI::unescapeHTML("&#x3042;&#x3044;&#X3046;"))
   end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]