Repository navigation
Regexp/MatchData: advance MRI 4.0.6 conformance (union, quote/escape, to_s hoisting, match coercion/block, aliases, to_int/to_str, match globals) - #546
Merged
Conversation
- regexpEscapeLiteral now escapes '-', space, '#' and control chars (\n \r \f \t \v) exactly as MRI's rb_reg_quote. - Regexp.union embeds a Regexp operand as its #to_s (options ride along), accepts Symbol/#to_str/#to_regexp operands, and returns a lone Regexp verbatim. - Regexp.quote/escape accept a Symbol and share one Method record so Regexp.method(:escape) == Regexp.method(:quote). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Regexp.union("a.b", /cd/).source is "a\.b|(?-mix:cd)" in MRI, not
"a\.b|cd"; the prior expectation encoded the old source-only behavior.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
… union recursion, to_int offsets - Regexp#inspect / #to_s escape unescaped '/' in the source. - Regexp#match: nil subject resets $~ and returns nil; Symbol/#to_str subjects are coerced; a block yields the MatchData and returns its value (no yield on a failed match). - reArg raises TypeError on an uninitialized Regexp (Regexp.allocate). - MatchData#match(n|name) (Ruby 3.4): the scalar subset of #[]. - MatchData#begin/#end/#offset (+byte variants) coerce a non-name argument via #to_int (Float, custom #to_int), then range-check -> IndexError. - Regexp.union restructured to MRI's recursion: lone Regexp/#to_regexp verbatim, Array recurses, lone String/Symbol quoted, multi-arg joins #to_s / #to_str-quoted operands (a multi-arg Symbol raises TypeError). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…private initialize - Regexp#eql?, MatchData#eql?, #length, #deconstruct are now genuine built-in aliases (shared method record) of #==, #==, #size, #captures, so instance_method(:alias) == instance_method(:original), as MRI. - MatchData#match_length(n|name) (Ruby 3.4): character length of a group, nil when it did not participate. - MatchData.allocate is undefined -> NoMethodError (bug #16294). - Regexp#initialize is a private method that refuses re-initialization: FrozenError on a frozen literal, TypeError on an already-initialized non-literal or subclass instance. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ys arity - Regexp#=== coerces a string-like operand through #to_str. - MatchData#inspect shows only named groups (by name) when the pattern has any, omitting unnamed numbered groups, matching MRI. - MatchData#deconstruct_keys raises ArgumentError (given 0, expected 1) with no argument instead of an index panic. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- Regexp.new/compile coerce a non-String/Regexp source through #to_str,
raising TypeError ("can't convert X into String (X#to_str gives Y)") when
it yields a non-String. String-building factored into regexpFromString.
- Regexp.last_match coerces a non-Integer/String/Symbol index via #to_int.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
/(?i:.)/ => "(?i-mx:.)", /(?:x)/ => "(?-mix:x)", /(?mmmmix-miiiix:)/ => "(?-mix:)". A group that closes before the end, or a named / look-around / capturing group, stays wrapped, matching MRI. singleWholeGroup respects escapes and character classes; mergeInlineFlags folds the hoisted options. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add eval residual tests (union coercion, quote/escape, inspect slashes, match nil/symbol/block, MatchData#match/#match_length, aliases, initialize, === to_str, named-only inspect, deconstruct_keys arity, to_s group hoist, Regexp.new #to_str, last_match #to_int) plus a white-box internal test for the defensive branches of escapeForwardSlashes and singleWholeGroup that a valid compiled Regexp cannot reach. 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.
Advance MRI-Ruby-4.0.6 conformance for the Regexp / MatchData / match-globals cluster. Measured against the rubyspec corpus (
core/regexp/*,core/matchdata/*) and byte-for-byte against MRI; the corpus total rose from ~261 to 321 passing examples.Regexp
Regexp.unionrestructured to MRI's recursion: a lone Regexp/#to_regexpis returned verbatim, an Array recurses, a lone String/Symbol becomes one quoted pattern, and a multi-pattern union joins each Regexp's#to_s(options ride along) with#to_str-quoted operands — a multi-arg Symbol (no#to_str) raises TypeError.Regexp.quote/escapenow escape-, space,#and the control chars (\n \r \f \t \v) exactly asrb_reg_quote, accept a Symbol, and share one method record soRegexp.method(:escape) == Regexp.method(:quote).Regexp#inspect/#to_sescape unescaped/in the source;#to_shoists a single whole-pattern option/non-capturing group, e.g./(?i:.)/→"(?i-mx:.)",/(?:x)/→"(?-mix:x)".Regexp#match: nil subject resets$~and returns nil; Symbol/#to_strsubjects coerce; a block yields the MatchData and returns its value.Regexp#===coerces a string-like operand via#to_str.Regexp.new/compilecoerce a non-String/Regexp source via#to_str;Regexp.last_matchcoerces its index via#to_int.Regexp#initializeis private and refuses re-initialization (FrozenError on a literal, TypeError otherwise);Regexp#eql?is a genuine alias of#==. Uninitialized-Regexp receivers (Regexp.allocate) raise TypeError.MatchData
#match(n|name)and#match_length(n|name)(Ruby 3.4);#begin/#end/#offset(and byte variants) coerce a non-name argument via#to_int.#inspectshows only named groups when the pattern has any;#deconstruct_keysraises ArgumentError with no argument.#length,#deconstruct,#eql?are genuine aliases of#size,#captures,#==;MatchData.allocateis undefined (NoMethodError).Out of scope / not addressed
The remaining corpus failures are encoding tracking (US-ASCII / UTF-16 / ISO-8859 / EUC / SJIS), engine limits in the regexp backend (duplicate named groups,
\u/\Nescapes, error-message wording), and native-subclassnew→initializedispatch — none reachable fromregexp.goalone.Verification
go test ./internal/vm/andgo test ./...green;GOOS=wasip1 GOARCH=wasm go build ./...OK;gofmt -lclean on changed files.regexp.gois at 100% coverage (a white-box internal test covers the defensive branches a valid compiled Regexp cannot reach); repo total held at 99.9%.🤖 Generated with Claude Code