Skip to content

String methods: advance MRI 4.0.6 conformance (index/rindex/sub/gsub/scan/casecmp/chomp/partition/tr/split/…) - #535

Merged
tannevaled merged 8 commits into
mainfrom
feat/agent-strmeth
Aug 31, 2026
Merged

tannevaled merged 8 commits into
mainfrom
feat/agent-strmeth

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advances MRI-Ruby-4.0.6 conformance for the String-method (non-encoding) cluster in core/string/*_spec.rb. All work is by whole-method behaviour verified byte-for-byte against MRI 4.0.6 (result, error class/message, and post-match $~/$1 globals). Encoding negotiation (Encoding::CompatibilityError, force_encoding, transcoding) is intentionally left to the separate encoding effort.

rubyspec deltas (pass count, per file)

method before → after
index 18 → 33
rindex 14 → 27
casecmp 19 → 33
chomp 49 → 61
gsub 58 → 65
sub 59 → 64
scan 12 → 18
partition 5 → 12
rpartition 6 → 14
tr 13 → 17
tr_s 12 → 16
count 6 → 8
delete 11 → 13
squeeze 12 → 13
split 50 → 54
bytes 7 → 8
codepoints 8 → 10

Aggregate over core/string/*_spec.rb: roughly +100 examples now passing, with the no implicit conversion TypeError errors across these methods largely eliminated.

What changed

  • index/rindex: Regexp arguments (setting $~), start-offset via #to_int, first-argument #to_str coercion, String-subclass unwrapping, and correct reverse-search/offset semantics.
  • sub/gsub: \+ backreference (last participating group); a replacement argument now takes precedence over a block; #to_str coercion of pattern and replacement; enumerator #size is nil.
  • scan: sets $~ per match and to the last match afterwards; result substrings inherit the receiver's encoding; #to_str pattern coercion.
  • casecmp/casecmp?: ASCII-only fold + byte compare vs. full Unicode fold respectively; #to_str coercion, subclass unwrapping, nil on unconvertible/incompatible, TypeError when #to_str returns a non-String.
  • chomp/chomp!: honour $/ for the no-arg form, treat explicit nil as no-op, treat the "\n" separator as smart mode.
  • partition/rpartition: Regexp separators (setting $~), #to_str coercion, plain-String same-encoding results.
  • tr/tr_s/count/delete/squeeze: operate on whole code points (no partial multibyte matches) and coerce set args via #to_str.
  • split: awk mode yields one trailing empty field under a non-zero limit; a nil/absent pattern falls back to $; (String or Regexp).
  • replace #to_str; bytes/codepoints block forms; each_line $/ + nil enumerator size; next/next! true aliases of succ/succ!.

Tests

New internal/vm/string_method_residuals_test.go covers every new branch. go test ./internal/vm/ and go test ./... green; GOOS=js GOARCH=wasm build OK; gofmt clean.

🤖 Generated with Claude Code

tannevaled and others added 8 commits August 31, 2026 17:23
…s args, offsets (MRI 4.0.6)

index gains Regexp matching (sets $~), start-offset via #to_int, first-arg
#to_str coercion and String-subclass unwrapping; rindex gains proper offset
semantics, Regexp reverse search, #to_str coercion and subclass args.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…rmance

- sub/gsub: \+ backreference (last participating group), replacement takes
  precedence over a block, #to_str coercion of pattern and replacement,
  enumerator #size nil.
- scan: sets $~ per match and to the last match after, result substrings
  inherit the receiver encoding, #to_str pattern coercion.
- casecmp: ASCII-only fold then byte compare; casecmp? full Unicode fold;
  both coerce via #to_str, unwrap subclasses, and return nil on an
  unconvertible arg or incompatible encoding.
- chomp/chomp!: honour $/ for the no-arg form, treat an explicit nil as no
  chomp, treat the "\n" separator as smart mode (\r\n/\n/\r), #to_str arg.
- partition/rpartition: Regexp separators (setting $~), #to_str coercion,
  String-instance and same-encoding results.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- tr/tr_s/count/delete/squeeze: operate on whole code points (multibyte
  characters match as a unit, never partially) and coerce set args via #to_str;
  reversed ranges still raise ArgumentError.
- split: awk mode yields one trailing empty field under a non-zero limit;
  a nil/absent pattern falls back to $; (String or Regexp).
- replace: coerces the source via #to_str.
- bytes/codepoints: block form yields each element and returns the receiver.
- each_line: honours $/ for the no-arg form; its enumerator #size is nil.
- next/next! are true aliases of succ/succ!.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
casecmp/casecmp? now raise TypeError (not nil) when the argument's #to_str
yields a non-String, matching MRI; only a value with no #to_str compares as
nil. Adds unit tests for the search/coerce edge branches.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Simplify chompSep's $/ handling and add unit tests covering the no-match
Regexp paths of rindex/rpartition, gsub block-only, sub arity error, and
subclass/String pattern coercion.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Adds unit tests for gsub!/sub! change, no-change nil, ArgumentError arity,
and the no-replacement enumerator (size nil + materialization).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Exercises the in-place bang variants whose set-argument coercion was updated,
so every String closure changed in this branch is covered.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 3019e24 into main Aug 31, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-strmeth branch August 31, 2026 22:09
tannevaled added a commit that referenced this pull request Sep 1, 2026
Locks in #535 (String methods: index/casecmp/chomp/gsub/scan/tr/... ~+110),
#536 (IO/StringIO reader+writer), #538 (Array#[]= auto-grow + mutators),
#539 (Range + Comparable), #540 (Struct + Data), #541 (Time). Measured
17983 passing on the pinned corpus, up from 17654.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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