Skip to content

Commit cf67abf

Browse files
authored
Merge pull request #177 from segiddins/segiddins/gzreader-external-encoding
Return the external encoding from GzipReader#external_encoding when converting
2 parents ba47520 + d66c091 commit cf67abf

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎ext/zlib/zlib.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4679,7 +4679,10 @@ rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj)
46794679
static VALUE
46804680
rb_gzreader_external_encoding(VALUE self)
46814681
{
4682-
return rb_enc_from_encoding(get_gzfile(self)->enc);
4682+
struct gzfile *gz = get_gzfile(self);
4683+
/* with a conversion set up, enc is the internal encoding and enc2 the
4684+
* external one (same convention as rb_io_extract_encoding_option) */
4685+
return rb_enc_from_encoding(gz->enc2 ? gz->enc2 : gz->enc);
46834686
}
46844687

46854688
static VALUE

‎test/zlib/test_zlib.rb‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,15 @@ def test_reader_wrap
17341734
}
17351735
end
17361736

1737+
def test_external_encoding_with_internal_encoding
1738+
pend "rb_io_extract_encoding_option ignores the encoding options on TruffleRuby" if RUBY_ENGINE == 'truffleruby'
1739+
r = Zlib::GzipReader.new(StringIO.new(Zlib.gzip("caf\xE9\n".b)), external_encoding: "ISO-8859-1", internal_encoding: "UTF-8")
1740+
assert_equal(Encoding::ISO_8859_1, r.external_encoding)
1741+
assert_equal("caf\u00E9\n", r.gets)
1742+
r = Zlib::GzipReader.new(StringIO.new(Zlib.gzip("caf\xE9\n".b)), external_encoding: "ISO-8859-1")
1743+
assert_equal(Encoding::ISO_8859_1, r.external_encoding)
1744+
end
1745+
17371746
def test_gets_separator_inside_multibyte_char
17381747
pend "rb_io_extract_encoding_option ignores the encoding options on TruffleRuby" if RUBY_ENGINE == 'truffleruby'
17391748
# In Shift_JIS the second byte of a character can be an ASCII byte such

0 commit comments

Comments
 (0)