Repository navigation
Full Unicode case mapping and options for String#upcase/downcase/swapcase/capitalize - #505
Merged
Merged
Conversation
String#upcase/#downcase/#swapcase/#capitalize (and their bang variants and the matching Symbol methods) previously delegated to Go's simple, single-rune case mapping and ignored their option arguments. This diverged from MRI in several ways: - Full Unicode case mapping was not applied, so multi-character expansions were missing: "ß".upcase gave "ß" instead of "SS", "ß".capitalize gave "ß" instead of "Ss", and "İ".downcase did not expand to "i̇" (i + U+0307). - The :ascii option was ignored, so non-ASCII characters were still mapped (e.g. "CÅR".downcase(:ascii) should keep "Å"). - The :turkic option was ignored (i <-> İ, I <-> ı dotted/dotless semantics). - The :lithuanian option was ignored (currently equivalent to full mapping, but still combinable with :turkic). - The :fold option (downcase-only full case folding, "ß" -> "ss") was ignored. - Invalid or improperly combined options did not raise ArgumentError. - Non-ASCII-compatible encodings (e.g. UTF-16LE) were mangled because the bytes were treated as UTF-8 instead of being transcoded. This adds a shared case-mapping engine (internal/vm/string_casemap.go) that parses the option symbols exactly as MRI's check_case_options does, applies per-rune up/down/title/swap mappings with the special-casing rules for ß and the dotted/dotless I, expands multi-character results, and transcodes through UTF-8 for non-ASCII-compatible encodings. All four String methods, their bang variants, and the four Symbol methods now route through it. The bang variants return nil when unchanged and raise FrozenError on a frozen receiver. Every behavior is verified byte-for-byte against MRI 4.0. rubyspec pass counts: upcase 12->32, downcase 19->32, swapcase 12->32, capitalize 15->34 (+72 total), with no regressions elsewhere in core/string. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Summary
String#upcase,#downcase,#swapcase,#capitalize(plus their!variants and the matching
Symbolmethods) delegated to Go's simple,single-rune case mapping and ignored their option arguments, diverging from
MRI 4.0 in several ways. This PR adds a shared case-mapping engine
(
internal/vm/string_casemap.go) that all of them now route through.What was fixed (all verified byte-for-byte against MRI 4.0)
"ß".upcase == "SS","ß".capitalize == "Ss","aßet".upcase == "ASSET",and
"İS".downcase == "i̇s"(i + U+0307).:asciioption — only ASCII letters are mapped(
"CÅR".downcase(:ascii) == "cÅr").:turkicoption — dotted/dotless I semantics (i↔İ,I↔ı).:lithuanianoption — currently equivalent to full mapping, stillcombinable with
:turkic.:foldoption — downcase-only full case folding(
"ß".downcase(:fold) == "ss").ArgumentErrorraised for invalid options,:foldon a non-downcasemethod,
:asciicombined with another option, an invalid second option,and more than two options — matching MRI's
check_case_options.through UTF-8 instead of being mangled, so
"äöü".encode("utf-16le").upcaseyields the correctÄÖÜbytes.nilwhen unchanged and raiseFrozenErroron afrozen receiver even when no change would occur.
ruby/spec pass-count deltas (per file, before → after, all now 100%)
core/string/upcase_spec.rbcore/string/downcase_spec.rbcore/string/swapcase_spec.rbcore/string/capitalize_spec.rb+72 examples total. A full
core/string/*_spec.rbsweep confirms noregressions (1871 → 1943 pass, 346 → 274 fail, errors unchanged).
Tests
New
internal/vm/string_case_conformance_test.go(packagevm_test) assertsevery fixed behavior and error path against MRI-verified output. New code in
string_casemap.gois 100% covered.go test ./..., thewasip1/wasmbuild,and
gofmtare all green.🤖 Generated with Claude Code