fix(chr): byte-writing inverse of ord (1–255, loud errors); utf8_encode lands (#435)#450
Merged
Merged
Conversation
…eigs gains utf8_encode (#435) chr silently returned "" for anything outside 0-127 and silently truncated fractions — the silent-tolerance class. It is now the byte-writing partner of ord under the bytes-forever position (#416): any integer 1-255 emits that raw byte (chr of n == str_from_bytes of [n]); 0 raises (strings are NUL-terminated), as do negatives, fractions, >255 (message points at utf8_encode), and non-numbers. #435's premise was a misdiagnosis: high bytes were already constructible via str_from_bytes (#248). utf8_encode + utf8_from_codepoints land in lib/utf8.eigs on top of it, tested against the published vectors, every width boundary, and a from_codepoints∘codepoints round-trip. The false doc claims (chr 'emits the UTF-8 of a codepoint' in BUILTINS.md and the str_from_bytes C comment; 'decode-only' in STDLIB.md and the module header) are corrected. Both suites green: release + ASan/UBSan detect_leaks=1, 2519/2519, leak tally 0. Closes #435 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
55ec77e to
f025bf7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Correcting the record first: the premise was a misdiagnosis. High bytes have been constructible since #248 —
str_from_bytes of [195, 169]yields the two raw bytes ofétoday, on v0.26.0, unchanged.utf8_encodewas never actually blocked; it was omitted on a mistaken belief. (The confusion had help: thestr_from_bytesC comment and BUILTINS.md both describedchras a codepoint→UTF-8 encoder, which it has never been.)What WAS real, and is now fixed:
chrsilently returned""for anything outside 0–127 and silently truncated fractions (chr of 65.5→"A") — the silent-tolerance class.chris now the byte-writing inverse oford: any integer 1–255 emits that raw byte (chr of n==str_from_bytes of [n]); everything else raises loudly.chr of 0raises too — strings are NUL-terminated and cannot hold a NUL, so returning a 0-length string was a lie; the >255 error message points atutf8_encode.utf8_encode+utf8_from_codepointslanded inlib/utf8.eigs, built onstr_from_bytes, tested against the published vectors (U+00E9 →C3 A9, U+20AC → 3B, U+4E2D → 3B, U+10348 → 4B) plus every width boundary (0x7F/0x80, 0x7FF/0x800, 0xFFFF/0x10000, 0x10FFFF ceiling) and a fullutf8_from_codepoints ∘ utf8_codepointsround-trip.str_from_bytesC comment, the lib/utf8.eigs header).Consumer note for the next bump train: DMG's serial path (
src/memory.eigs:340) callschr of chunguarded — a ROM writing byte0x00to serial would now raise instead of silently appending nothing. Blargg ROMs write ASCII so the twin gates are unaffected, but DMG should guardch != 0when it takes the pin.Neither option in the issue was taken as written: option 1 said "0..255", but 0 is unrepresentable in a NUL-terminated string and silently returning
""for it is the same tolerance bug this issue is about. 1–255 loud is the honest contract.Closes #435
🤖 Generated with Claude Code