Skip to content

Two CRuby differences left over from #89: ungetc("\r") with universal newlines, unassigned GB18030 four-byte sequences #214

Closed as not planned

Description

@Largo

Two correctness differences from CRuby 4.0 that were noted in #89 (a performance issue) and never fixed. Moved here so #89 can close.

IO#ungetc("\r") on a universal-newline stream

Noted when #111 moved getc/ungetc and line reading to C#: pushing back "\r" on a stream read with universal newline conversion ("rt", universal_newline: true) does not behave as in CRuby. #89 records only that the difference exists, not a reproduction, so the first step is a script run on both and compared (ungetc("\r") followed by getc, gets and read, with "\n" and other characters next in the stream).

GB18030 four-byte sequences that .NET does not assign

Found by the differential in #207 (118,272 cases over EUC-JP, EUC-TW, GB18030, CP51932, eucJP-ms and EUC-JIS-2004): 272 GB18030 cases differ. They are four-byte sequences that are valid in CRuby's table but unassigned in .NET's (~500k codes, more than the private use planes hold, so they cannot be given UTF-16 stand-ins). IronRuby splits such a sequence into separate characters where CRuby reads one, which shows in valid_encoding?, chars, length and scrub.

Fixing it means not going through .NET's decoder for these: keeping such strings as bytes and walking characters with a GB18030 table of IronRuby's own, as was done for EUC-KR in #184.

Activity

  1. Largo commented on Nov 21, 2026

    @Largo
    OwnerAuthor

    Status of the two items after #259, and why the second is being left as it is.

    IO#ungetc("\r") on a universal-newline stream — fixed in #259

    Util/ungetc-matrix.rb (750 lines) prints the same under CRuby 4.0.6 and IronRuby on net10.0 and net11.0.

    GB18030 four-byte sequences .NET does not assign — not fixed, by decision

    Measured over all 1,587,600 four-byte shapes (81-FE 30-39 81-FE 30-39): CRuby 4.0.6 reads every one as a valid single character; IronRuby reads 499,604 of them as four invalid bytes. They are two solid ranges, counted linearly over the four bytes:

    linear index count what it is
    39,420 – 188,999 149,580 between the BMP block (ends 84 31 A4 39) and the supplementary planes (start 90 30 81 30)
    1,237,576 – 1,587,599 350,024 after U+10FFFF (E3 32 9A 35)

    For such a string IronRuby answers valid_encoding? == false, length == 4, and raises ArgumentError: invalid byte sequence in GB18030 from character operations, where CRuby answers true, 1, and treats it as an ordinary character that only fails to transcode.

    Why there is no small fix: IronRuby holds a string's characters as UTF-16, and gives a well-formed sequence that has no Unicode character a stand-in code point that writes back as the same bytes (plane 15/16 for the double-byte encodings and EUC-TW). GB18030 already maps every Unicode code point, private planes included, so no code point is free — and in total it has more characters (about 1.6 million) than UTF-16 has code points (about 1.1 million). A stand-in would have to be two units that are not a surrogate pair, and every routine that walks characters (about 45 places across the string core, regexps and formatting) would have to learn to read it as one. These sequences are reserved: no real text contains them. So the difference is left in place.

    Found on the way and fixed separately: String#ord/#codepoints of four-byte GB18030 and EUC-TW characters came out negative, and #ord of a UTF-16/UTF-32 string held as bytes answered its first byte (PR to follow in the next comment's link); Integer#chr and String#<< for multibyte encodings are being brought in line on the same branch.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions