ruby-changes:13527
From: naruse <ko1@a...>
Date: Mon, 12 Oct 2009 03:50:44 +0900 (JST)
Subject: [ruby-changes:13527] Ruby:r25303 (trunk): * marshal.c (has_encoding): added for check the regexp
naruse 2009-10-12 03:49:41 +0900 (Mon, 12 Oct 2009) New Revision: 25303 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25303 Log: * marshal.c (has_encoding): added for check the regexp is dumped by 1.8 or 1.9. * marshal.c (r_object0): use has_encoding. Modified files: trunk/ChangeLog trunk/marshal.c trunk/test/ruby/test_marshal.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 25302) +++ ChangeLog (revision 25303) @@ -1,3 +1,10 @@ +Mon Oct 12 03:47:42 2009 NARUSE, Yui <naruse@r...> + + * marshal.c (has_encoding): added for check the regexp + is dumped by 1.8 or 1.9. + + * marshal.c (r_object0): use has_encoding. + Sun Oct 11 15:54:52 2009 Nobuyoshi Nakada <nobu@r...> * ext/gdbm/gdbm.c (fgdbm_select): fixed rdoc. a patch from Justin Index: marshal.c =================================================================== --- marshal.c (revision 25302) +++ marshal.c (revision 25303) @@ -1304,6 +1304,35 @@ #define div0(x) ruby_div0(x) +static int +has_encoding(struct load_arg *arg) +{ + int res = FALSE; + long offset = arg->offset; + r_long(arg); + switch (r_byte(arg)) { + case ':': + switch (r_byte(arg)) { + case 6: + if (r_byte(arg) == 'E') res = TRUE; + break; + case 13: + if (r_byte(arg) == 'e') res = TRUE; + break; + } + break; + case ';': + { + ID id = r_symlink(arg); + if (id == rb_intern("E") || id == rb_id_encoding()) + res = TRUE; + } + break; + } + arg->offset = offset; + return res; +} + static VALUE r_object0(struct load_arg *arg, int *ivp, VALUE extmod) { @@ -1474,21 +1503,17 @@ { volatile VALUE str = r_bytes(arg); int options = r_byte(arg); - VALUE args[2]; - args[0] = str; - args[1] = INT2FIX(options); - v = r_entry(rb_obj_alloc(rb_cRegexp), arg); - if (ivp) { - r_ivar(v, arg); - *ivp = FALSE; + + if (!ivp || !has_encoding(arg)) { + VALUE pat; + VALUE dst; + char *rsrc = + "(?<!\\\\)((?:\\\\\\\\)*)\\\\([ghijklmopquyEFHIJKLNOPQRSTUVXY])"; + pat = rb_reg_new(rsrc, strlen(rsrc), 0); + dst = rb_usascii_str_new_cstr("\\1\\2"); + rb_funcall(str, rb_intern("gsub!"), 2, pat, dst); } - if (rb_enc_get_index(v) != rb_usascii_encindex()) - rb_enc_copy(str, v); - if (rb_enc_get_index(str) != rb_utf8_encindex()) { -#define f_gsub_bang(x,y,z) rb_funcall(x, rb_intern("gsub!"), 2, y, z) - f_gsub_bang(str, rb_reg_new("\\\\u", 3, 0), rb_usascii_str_new_cstr("u")); - } - rb_obj_call_init(v, 2, args); + v = r_entry(rb_reg_new_str(str, options), arg); v = r_leave(v, arg); } break; Index: test/ruby/test_marshal.rb =================================================================== --- test/ruby/test_marshal.rb (revision 25302) +++ test/ruby/test_marshal.rb (revision 25303) @@ -206,11 +206,15 @@ end def test_regexp + assert_equal(/\\u/, Marshal.load("\004\b/\b\\\\u\000")) + assert_equal(/u/, Marshal.load("\004\b/\a\\u\000")) + assert_equal(/u/, Marshal.load("\004\bI/\a\\u\000\006:\016@encoding\"\vEUC-JP")) + bug2109 = '[ruby-core:25625]' a = "\x82\xa0".force_encoding(Encoding::Windows_31J) b = "\x82\xa2".force_encoding(Encoding::Windows_31J) c = [/#{a}/, /#{b}/] - assert_equal(c, Marshal.load(Marshal.dump(c))) + assert_equal(c, Marshal.load(Marshal.dump(c)), bug2109) end class DumpTest -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/