Skip to content

Use the external encoding for character boundaries in GzipReader - #163

Merged
hsbt merged 3 commits into
ruby:masterfrom
mame:zlib-gzreader-char-encoding
Nov 26, 2026
Merged

hsbt merged 3 commits into
ruby:masterfrom
mame:zlib-gzreader-char-encoding

Conversation

@mame

@mame mame commented Oct 24, 2026

Copy link
Copy Markdown
Member

GzipReader#getc and #gets with a limit split the data before it is converted to the internal encoding, but measured characters in the internal encoding.

  gz = Zlib.gzip("\x82\xa0\x82\xa2".b) # "あい" in Shift_JIS
  Zlib::GzipReader.new(StringIO.new(gz), external_encoding: "Shift_JIS",
                       internal_encoding: "UTF-8").each_char.to_a
  # expected: ["あ", "い"]
  # actual:   ["\x82", "\xA0", "\x82", "「"]

@mame
mame requested a review from nobu October 24, 2026 20:58
mame and others added 2 commits November 26, 2026 17:11
GzipReader#getc and #gets with a limit split the data before it is
converted to the internal encoding, but measured characters in the
internal encoding.

  gz = Zlib.gzip("\x82\xa0\x82\xa2".b) # "あい" in Shift_JIS
  Zlib::GzipReader.new(StringIO.new(gz), external_encoding: "Shift_JIS",
                       internal_encoding: "UTF-8").each_char.to_a
  # expected: ["あ", "い"]
  # actual:   ["\x82", "\xA0", "\x82", "「"]

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The boundary check from ruby#180 used `enc`, which this branch removed. It looks at the unconverted buffer, so it takes GZFILE_RAW_ENC like the other checks here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hsbt
hsbt force-pushed the zlib-gzreader-char-encoding branch from 3a6b5d4 to 5a93626 Compare November 26, 2026 08:14
TruffleRuby ignores the encoding options of GzipReader, as test_gets_separator_inside_multibyte_char already notes, so each_char returns the unconverted bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hsbt
hsbt merged commit 718c0b4 into ruby:master Nov 26, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants