From 0903a251796c2b4086804a94420c231c04e3cea1 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Tue, 23 Aug 2022 12:22:24 -0700 Subject: Make IO#set_encoding with binary external encoding use nil internal encoding This was already the behavior when a single `'external:internal'` encoding specifier string was passed. This makes the behavior consistent for the case where separate external and internal encoding specifiers are provided. While here, fix the IO#set_encoding method documentation to state that either the first or second argument can be a string with an encoding name, and describe the behavior when the external encoding is binary. Fixes [Bug #18899] --- io.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'io.c') diff --git a/io.c b/io.c index 2e51a46635..dbcfcf3fc0 100644 --- a/io.c +++ b/io.c @@ -11573,6 +11573,11 @@ io_encoding_set(rb_io_t *fptr, VALUE v1, VALUE v2, VALUE opt) enc2 = NULL; } } + if (enc2 == rb_ascii8bit_encoding()) { + /* If external is ASCII-8BIT, no transcoding */ + enc = enc2; + enc2 = NULL; + } SET_UNIVERSAL_NEWLINE_DECORATOR_IF_ENC2(enc2, ecflags); ecflags = rb_econv_prepare_options(opt, &ecopts, ecflags); } @@ -13393,10 +13398,12 @@ rb_io_internal_encoding(VALUE io) * * See {Encodings}[rdoc-ref:File@Encodings]. * - * Argument +ext_enc+, if given, must be an Encoding object; + * Argument +ext_enc+, if given, must be an Encoding object + * or a String with the encoding name; * it is assigned as the encoding for the stream. * - * Argument +int_enc+, if given, must be an Encoding object; + * Argument +int_enc+, if given, must be an Encoding object + * or a String with the encoding name; * it is assigned as the encoding for the internal string. * * Argument 'ext_enc:int_enc', if given, is a string @@ -13404,6 +13411,10 @@ rb_io_internal_encoding(VALUE io) * corresponding Encoding objects are assigned as the external * and internal encodings for the stream. * + * If the external encoding of a string is binary/ASCII-8BIT, + * the internal encoding of the string is set to nil, since no + * transcoding is needed. + * * Optional keyword arguments +enc_opts+ specify * {Encoding options}[rdoc-ref:encodings.rdoc@Encoding+Options]. * -- cgit v1.2.1