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

ruby-changes:18780

From: drbrain <ko1@a...>
Date: Sun, 6 Feb 2011 12:56:48 +0900 (JST)
Subject: [ruby-changes:18780] Ruby:r30806 (trunk): Ensure result encoding is the same as input encoding for String#gsub. [Bug #4340].

drbrain	2011-02-06 12:54:50 +0900 (Sun, 06 Feb 2011)

  New Revision: 30806

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=30806

  Log:
    Ensure result encoding is the same as input encoding for String#gsub.  [Bug #4340].

  Modified files:
    trunk/ChangeLog
    trunk/string.c
    trunk/test/ruby/test_string.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 30805)
+++ ChangeLog	(revision 30806)
@@ -1,3 +1,8 @@
+Sun Feb  6 12:46:02 2011  Eric Hodel  <drbrain@s...>
+
+	* string.c (gsub): Ensure result encoding is the same as input
+	  encoding.  [Bug #4340].
+
 Sun Feb  6 12:18:25 2011  Nobuyoshi Nakada  <nobu@r...>
 
 	* parse.y (words, qwords): dispatch array events.  based on a
Index: string.c
===================================================================
--- string.c	(revision 30805)
+++ string.c	(revision 30806)
@@ -3745,6 +3745,7 @@
     slen = RSTRING_LEN(str);
     cp = sp;
     str_enc = STR_ENC_GET(str);
+    rb_enc_associate(dest, str_enc);
 
     do {
 	n++;
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 30805)
+++ test/ruby/test_string.rb	(revision 30806)
@@ -674,6 +674,21 @@
     assert_raise(ArgumentError) { "foo".gsub }
   end
 
+  def test_gsub_encoding
+    a = S("hello world")
+    a.force_encoding Encoding::UTF_8
+
+    b = S("hi")
+    b.force_encoding Encoding::US_ASCII
+
+    assert_equal Encoding::UTF_8, a.gsub(/hello/, b).encoding
+
+    c = S("everybody")
+    c.force_encoding Encoding::US_ASCII
+
+    assert_equal Encoding::UTF_8, a.gsub(/world/, c).encoding
+  end
+
   def test_gsub!
     a = S("hello")
     b = a.dup

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

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