diff options
-rw-r--r-- | string.c | 18 | ||||
-rw-r--r-- | test/ruby/test_m17n.rb | 9 | ||||
-rw-r--r-- | version.h | 2 |
3 files changed, 22 insertions, 7 deletions
@@ -9187,9 +9187,15 @@ rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl) int encidx; VALUE buf = Qnil; const char *rep; - long replen; + long replen = -1; int tainted = 0; + if (rb_block_given_p()) { + if (!NIL_P(repl)) + rb_raise(rb_eArgError, "both of block and replacement given"); + replen = 0; + } + if (ENC_CODERANGE_CLEAN_P(cr)) return Qnil; @@ -9213,9 +9219,8 @@ rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl) const char *e = RSTRING_END(str); const char *p1 = p; int rep7bit_p; - if (rb_block_given_p()) { + if (!replen) { rep = NULL; - replen = 0; rep7bit_p = FALSE; } else if (!NIL_P(repl)) { @@ -9326,7 +9331,10 @@ rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl) const char *e = RSTRING_END(str); const char *p1 = p; long mbminlen = rb_enc_mbminlen(enc); - if (!NIL_P(repl)) { + if (!replen) { + rep = NULL; + } + else if (!NIL_P(repl)) { rep = RSTRING_PTR(repl); replen = RSTRING_LEN(repl); } @@ -9377,7 +9385,7 @@ rb_enc_str_scrub(rb_encoding *enc, VALUE str, VALUE repl) rb_str_buf_cat(buf, rep, replen); } else { - repl = rb_yield(rb_enc_str_new(p, e-p, enc)); + repl = rb_yield(rb_enc_str_new(p, clen, enc)); repl = str_compat_and_valid(repl, enc); tainted |= OBJ_TAINTED_RAW(repl); rb_str_buf_cat(buf, RSTRING_PTR(repl), RSTRING_LEN(repl)); diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb index 8b7e7e6b4a..a16e49c27a 100644 --- a/test/ruby/test_m17n.rb +++ b/test/ruby/test_m17n.rb @@ -1623,8 +1623,9 @@ class TestM17N < Test::Unit::TestCase assert_raise(ArgumentError){ u("\xE3\x81\x82\xE3\x81\x82\xE3\x81").scrub{u("\x81")} } assert_equal(e("\xA4\xA2\xA2\xAE"), e("\xA4\xA2\xA4").scrub{e("\xA2\xAE")}) - assert_equal(u("\x81"), u("a\x81").scrub {|c| break c}) + assert_equal(u("\x81"), u("a\x81c").scrub {|c| break c}) assert_raise(ArgumentError) {u("a\x81").scrub {|c| c}} + assert_raise(ArgumentError) {u("a").scrub("?") {|c| c}} end def test_scrub_widechar @@ -1640,6 +1641,12 @@ class TestM17N < Test::Unit::TestCase assert_equal("\uFFFD".encode("UTF-32LE"), "\xff".force_encoding(Encoding::UTF_32LE). scrub) + c = nil + assert_equal("?\u3042".encode(Encoding::UTF_16LE), + "\x00\xD8\x42\x30".force_encoding(Encoding::UTF_16LE). + scrub {|e| c = e; "?".encode(Encoding::UTF_16LE)}) + assert_equal("\x00\xD8".force_encoding(Encoding::UTF_16LE), c) + assert_raise(ArgumentError) {"\uFFFD\u3042".encode("UTF-16BE").scrub("") {}} end def test_scrub_dummy_encoding @@ -1,6 +1,6 @@ #define RUBY_VERSION "2.4.0" #define RUBY_RELEASE_DATE "2017-03-12" -#define RUBY_PATCHLEVEL 20 +#define RUBY_PATCHLEVEL 21 #define RUBY_RELEASE_YEAR 2017 #define RUBY_RELEASE_MONTH 3 |