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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54655 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
86b1179a03
commit
20cd25c86f
@ -1,3 +1,12 @@
|
|||||||
|
Wed Apr 20 17:33:31 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* 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@ruby-lang.org>
|
Wed Apr 20 15:52:28 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* error.c (syntax_error_initialize): move the default message,
|
* error.c (syntax_error_initialize): move the default message,
|
||||||
|
@ -369,17 +369,34 @@ cgiesc_escape(VALUE self, VALUE str)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* :nodoc: */
|
|
||||||
static VALUE
|
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);
|
StringValue(str);
|
||||||
|
|
||||||
if (rb_enc_str_asciicompat_p(str)) {
|
if (rb_enc_str_asciicompat_p(str)) {
|
||||||
|
VALUE enc = accept_charset(argc-1, argv+1, self);
|
||||||
return optimized_unescape(str, enc);
|
return optimized_unescape(str, enc);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return rb_call_super(1, &str);
|
return rb_call_super(argc, argv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -392,7 +409,7 @@ Init_escape(void)
|
|||||||
rb_define_method(rb_mEscape, "escapeHTML", cgiesc_escape_html, 1);
|
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, "unescapeHTML", cgiesc_unescape_html, 1);
|
||||||
rb_define_method(rb_mEscape, "escape", cgiesc_escape, 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_prepend_module(rb_mUtil, rb_mEscape);
|
||||||
rb_extend_object(rb_cCGI, rb_mEscape);
|
rb_extend_object(rb_cCGI, rb_mEscape);
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,6 @@ module CGI::Util
|
|||||||
# string = CGI::unescape("%27Stop%21%27+said+Fred")
|
# string = CGI::unescape("%27Stop%21%27+said+Fred")
|
||||||
# # => "'Stop!' said Fred"
|
# # => "'Stop!' said Fred"
|
||||||
def unescape(string,encoding=@@accept_charset)
|
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|
|
str=string.tr('+', ' ').b.gsub(/((?:%[0-9a-fA-F]{2})+)/) do |m|
|
||||||
[m.delete('%')].pack('H*')
|
[m.delete('%')].pack('H*')
|
||||||
end.force_encoding(encoding)
|
end.force_encoding(encoding)
|
||||||
|
@ -118,6 +118,23 @@ class CGIUtilTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
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
|
def test_cgi_unescapeHTML_uppercasecharacter
|
||||||
assert_equal("\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86", CGI::unescapeHTML("あいう"))
|
assert_equal("\xE3\x81\x82\xE3\x81\x84\xE3\x81\x86", CGI::unescapeHTML("あいう"))
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user