Skip to content

Full Unicode case mapping and options for String#upcase/downcase/swapcase/capitalize - #505

Merged
tannevaled merged 1 commit into
mainfrom
feat/agent-strcase
Aug 30, 2026
Merged

tannevaled merged 1 commit into
mainfrom
feat/agent-strcase

Conversation

@tannevaled

@tannevaled tannevaled commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

String#upcase, #downcase, #swapcase, #capitalize (plus their !
variants and the matching Symbol methods) 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)

  • Full Unicode case mapping with multi-character expansion:
    "ß".upcase == "SS", "ß".capitalize == "Ss", "aßet".upcase == "ASSET",
    and "İS".downcase == "i̇s" (i + U+0307).
  • :ascii option — only ASCII letters are mapped
    ("CÅR".downcase(:ascii) == "cÅr").
  • :turkic option — dotted/dotless I semantics (i↔İ, I↔ı).
  • :lithuanian option — currently equivalent to full mapping, still
    combinable with :turkic.
  • :fold option — downcase-only full case folding
    ("ß".downcase(:fold) == "ss").
  • ArgumentError raised for invalid options, :fold on a non-downcase
    method, :ascii combined with another option, an invalid second option,
    and more than two options — matching MRI's check_case_options.
  • Non-ASCII-compatible encodings (e.g. UTF-16LE) are now transcoded
    through UTF-8 instead of being mangled, so
    "äöü".encode("utf-16le").upcase yields the correct ÄÖÜ bytes.
  • Bang variants return nil when unchanged and raise FrozenError on a
    frozen receiver even when no change would occur.

ruby/spec pass-count deltas (per file, before → after, all now 100%)

spec file before after
core/string/upcase_spec.rb 12 32
core/string/downcase_spec.rb 19 32
core/string/swapcase_spec.rb 12 32
core/string/capitalize_spec.rb 15 34

+72 examples total. A full core/string/*_spec.rb sweep confirms no
regressions (1871 → 1943 pass, 346 → 274 fail, errors unchanged).

Tests

New internal/vm/string_case_conformance_test.go (package vm_test) asserts
every fixed behavior and error path against MRI-verified output. New code in
string_casemap.go is 100% covered. go test ./..., the wasip1/wasm build,
and gofmt are all green.

🤖 Generated with Claude Code

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>
@tannevaled
tannevaled merged commit e3b7196 into main Aug 30, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-strcase branch August 30, 2026 14:15
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.

1 participant