summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-02-06 03:54:50 +0000
committerdrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-02-06 03:54:50 +0000
commitedaf78dfb69db9794e4ddf4f0a658c374e857924 (patch)
treed11c28c6024f44ff97f83f4e418dd2abb8668c91
parentb1986003bdcee30cf4e52c5638d3bc197195952b (diff)
Ensure result encoding is the same as input encoding for String#gsub. [Bug #4340].
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30806 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--string.c1
-rw-r--r--test/ruby/test_string.rb15
3 files changed, 21 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 541e371c72..b5931cf4b0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sun Feb 6 12:46:02 2011 Eric Hodel <drbrain@segment7.net>
+
+ * string.c (gsub): Ensure result encoding is the same as input
+ encoding. [Bug #4340].
+
Sun Feb 6 12:18:25 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (words, qwords): dispatch array events. based on a
diff --git a/string.c b/string.c
index 1270f502a8..bef9cb93fd 100644
--- a/string.c
+++ b/string.c
@@ -3745,6 +3745,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
slen = RSTRING_LEN(str);
cp = sp;
str_enc = STR_ENC_GET(str);
+ rb_enc_associate(dest, str_enc);
do {
n++;
diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb
index 972f326ab2..9440f93ed4 100644
--- a/test/ruby/test_string.rb
+++ b/test/ruby/test_string.rb
@@ -674,6 +674,21 @@ class TestString < Test::Unit::TestCase
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