From 66bae8ad6df90c83b34908057afd991ffc9492c7 Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 13 Dec 2007 09:05:49 +0000 Subject: [PATCH] * string.c (str_gsub): should copy encoding to the result. * sprintf.c (rb_str_format): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14212 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 +++++- bootstraptest/test_knownbug.rb | 2 +- sprintf.c | 1 + string.c | 8 +++++--- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 397b580379..4c66911000 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,4 @@ -Thu Dec 13 17:26:17 2007 Yukihiro Matsumoto +Thu Dec 13 17:51:54 2007 Yukihiro Matsumoto * string.c (rb_str_split_m): need not to check encoding if regexp is empty. @@ -9,6 +9,10 @@ Thu Dec 13 17:26:17 2007 Yukihiro Matsumoto * string.c (rb_str_chomp_bang): need to check encoding of record separator. + * string.c (str_gsub): should copy encoding to the result. + + * sprintf.c (rb_str_format): ditto. + Thu Dec 13 17:03:29 2007 Nobuyoshi Nakada * encoding.c (rb_enc_compatible): should swap encoding indexes too. diff --git a/bootstraptest/test_knownbug.rb b/bootstraptest/test_knownbug.rb index 73829fb0aa..5bc093ecb1 100644 --- a/bootstraptest/test_knownbug.rb +++ b/bootstraptest/test_knownbug.rb @@ -70,7 +70,7 @@ assert_equal 'ok', %q{ begin s["\xb0\xa3"] = "foo" :ng - rescue IndexError + rescue ArgumentError :ok end } diff --git a/sprintf.c b/sprintf.c index ed02e01aeb..0835dab3d1 100644 --- a/sprintf.c +++ b/sprintf.c @@ -848,6 +848,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) rb_str_resize(result, blen); if (tainted) OBJ_TAINT(result); + rb_enc_associate(result, enc); return result; } diff --git a/string.c b/string.c index bacb18026c..bb12d50619 100644 --- a/string.c +++ b/string.c @@ -2490,11 +2490,12 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang) rb_str_locktmp(dest); do { + rb_encoding *enc; + n++; match = rb_backref_get(); regs = RMATCH(match)->regs; if (iter) { - rb_encoding *enc; rb_match_busy(match); val = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match))); @@ -2505,12 +2506,12 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang) rb_raise(rb_eRuntimeError, "block should not cheat"); } rb_backref_set(match); - rb_enc_associate(str, enc); } else { val = rb_reg_regsub(repl, str, regs, pat); - rb_enc_copy(str, val); + enc = rb_enc_check(str, val); } + rb_enc_associate(str, enc); if (OBJ_TAINTED(val)) tainted = 1; len = (bp - buf) + (beg - offset) + RSTRING_LEN(val) + 3; if (blen < len) { @@ -2570,6 +2571,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang) else { RBASIC(dest)->klass = rb_obj_class(str); OBJ_INFECT(dest, str); + rb_enc_copy(dest, str); str = dest; } STR_SET_LEN(str, bp - buf);