diff --git a/ChangeLog b/ChangeLog index 1230329aff..57b405dee7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Sep 15 02:37:19 2008 Nobuyoshi Nakada + + * transcode.c (str_encode): returns duplicated string if nothing + changed. [ruby-core:18578] + Sun Sep 14 22:09:01 2008 Takeyuki Fujioka * lib/cgi/core.rb (CGI::parse): performance improvement diff --git a/test/ruby/test_transcode.rb b/test/ruby/test_transcode.rb index 5abeed88ee..8770328bc1 100644 --- a/test/ruby/test_transcode.rb +++ b/test/ruby/test_transcode.rb @@ -582,4 +582,10 @@ class TestTranscode < Test::Unit::TestCase assert_raise(Encoding::ConversionUndefined) { "\u203E".encode("CP51932") } end + def test_nothing_changed + a = "James".force_encoding("US-ASCII") + b = a.encode("Shift_JIS") + assert_equal(Encoding::US_ASCII, a.encoding) + assert_equal(Encoding::Shift_JIS, b.encoding) + end end diff --git a/transcode.c b/transcode.c index 4d1107ddc4..1c1126fcce 100644 --- a/transcode.c +++ b/transcode.c @@ -2570,7 +2570,12 @@ str_encode(int argc, VALUE *argv, VALUE str) int encidx = str_transcode(argc, argv, &newstr); if (encidx < 0) return rb_str_dup(str); - RBASIC(newstr)->klass = rb_obj_class(str); + if (newstr == str) { + newstr = rb_str_dup(str); + } + else { + RBASIC(newstr)->klass = rb_obj_class(str); + } return str_encode_associate(newstr, encidx); }