[Bug:trunk] String#dump with non-ascii-compatible string provides invalid code


#1

e$B1sF#$G$9!#e(B

e$B%"%9%-!<Hs8_49$JJ8;zNs$re(B String#dump e$B$9$k$He(B eval
e$B$G$-$J$$J8;zNs$,e(B
e$BF@$i$l$^$7$?!#e(B

$ ./ruby -e ‘p “\u3042”.encode(“UTF-16LE”).dump’
““B0”.force_encoding(“UTF-16LE”\x00”

Index: string.c

— string.c (revision 23159)
+++ string.c (working copy)
@@ -4291,7 +4291,7 @@

 result = rb_str_new5(str, 0, len);
 p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str);
  • q = RSTRING_PTR(result); qend = q + len;
  • q = RSTRING_PTR(result); qend = q + len + 1;

    *q++ = ‘"’;
    while (p < pend) {
    Index: test/ruby/test_m17n.rb
    ===================================================================
    — test/ruby/test_m17n.rb (revision 23159)
    +++ test/ruby/test_m17n.rb (working copy)
    @@ -241,6 +241,9 @@
    u("\xfc"),
    “\u3042”,
    “ascii”,

  •  "\u3042".encode("UTF-16LE"),
    
  •  "\u3042".encode("UTF-16BE"),
    

    ].each do |str|
    assert_equal(str, eval(str.dump), “[ruby-dev:33142]”)
    end


#2

e$B%A%1%C%He(B #1365 e$B$,99?7$5$l$^$7$?!#e(B (by Yusuke E.)

e$B%9%F!<%?%9e(B Opene$B$+$ie(BClosede$B$KJQ99e(B
e$B?JD=e(B % 0e$B$+$ie(B100e$B$KJQ99e(B

Applied in changeset r23160.

http://redmine.ruby-lang.org/issues/show/1365