Issue #13119 has been updated by Nobuyoshi Nakada.

Status changed from Open to Closed
Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED

----------------------------------------
Bug #13119: String#scrub ignores the block if the string encoding is not ASCII-compatible
https://bugs.ruby-lang.org/issues/13119#change-62453

* Author: Benoit Daloze
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 
* Backport: 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED
----------------------------------------
String#scrub completely ignores the block if the string encoding is not ASCII-compatible.
This does not seem intended and is counter-intuitive as ASCII-compatible strings use it.

    "\x00\xD8\x42\x30".force_encoding(Encoding::UTF_16LE).scrub { |e| p e; "?".encode(Encoding::UTF_16LE) }

Gives

    "\uFFFD\u3042"

But it should be

    "\x0\xd8"
    "?\u3042"

Moreover, there is a bug in the String to be yielded to the block, string.c:9399:

    repl = rb_yield(rb_enc_str_new(p, e-p, enc));

should be

    repl = rb_yield(rb_enc_str_new(p, clen, enc));

So it does not yield all the remaining string but only the invalid part.

And finally, it should probably be an error if both a block and a replacement string are given.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>