From a7d5be3f529b3819a864644062a81eaa3b746bf1 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 17:23:49 +0200 Subject: [PATCH 1/7] String#index/#rindex: Regexp support, to_str/to_int coercion, subclass 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 --- internal/vm/builtins.go | 123 ++++++++++++++++++-- internal/vm/regexp.go | 32 +++++ internal/vm/string_method_residuals_test.go | 66 +++++++++++ 3 files changed, 211 insertions(+), 10 deletions(-) create mode 100644 internal/vm/string_method_residuals_test.go diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 8395e8f2..9b4b312f 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -1836,22 +1836,32 @@ func (vm *VM) bootstrap() { return s }) vm.cString.define("index", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - s, needle := strOf(self), vm.strPatternCompat(self, args[0]) - r := []rune(s) + s := strOf(self) + needle, re, isRe := vm.strSearchArg(self, args[0]) + nChars := utf8.RuneCountInString(s) start := 0 if len(args) > 1 { // optional character offset (negative counts from the end) - start = int(intArg(args[1])) + start = int(vm.repeatLong(args[1])) if start < 0 { - start += len(r) + start += nChars } if start < 0 { + if isRe { // a Regexp search always clears $~, even out of range + vm.lastMatch = object.NilV + } return object.NilV } } - if start > len(r) { + if start > nChars { + if isRe { + vm.lastMatch = object.NilV + } return object.NilV } - byteStart := len(string(r[:start])) + if isRe { + return vm.strIndexRegexp(re, s, start) + } + byteStart := charToByte(s, start) byteIdx := strings.Index(s[byteStart:], needle) if byteIdx < 0 { return object.NilV @@ -1859,11 +1869,26 @@ func (vm *VM) bootstrap() { return object.IntValue(int64(utf8.RuneCountInString(s[:byteStart+byteIdx]))) }) vm.cString.define("rindex", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - byteIdx := strings.LastIndex(strOf(self), vm.strPatternCompat(self, args[0])) - if byteIdx < 0 { - return object.NilV + s := strOf(self) + needle, re, isRe := vm.strSearchArg(self, args[0]) + nChars := utf8.RuneCountInString(s) + limit := nChars + if len(args) > 1 { // optional character offset (negative counts from the end) + limit = int(vm.repeatLong(args[1])) + if limit < 0 { + limit += nChars + } + if limit < 0 { + return object.NilV + } + if limit > nChars { + limit = nChars + } } - return object.IntValue(int64(utf8.RuneCountInString(strOf(self)[:byteIdx]))) + if isRe { + return vm.strRindexRegexp(re, s, limit) + } + return strRindexString(s, needle, limit) }) vm.cString.define("=~", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { re, ok := args[0].(*Regexp) @@ -6221,6 +6246,84 @@ func (vm *VM) strPatternCompat(self, pat object.Value) string { return strArg(pat) } +// strCoerceArg coerces v to a Go string the way String search/replace methods +// accept their argument: a String (or a String subclass instance, unwrapped) +// is taken directly; any other value is converted with #to_str. A value that is +// neither a String nor defines #to_str raises TypeError with MRI's message. The +// second result is the String object the bytes came from, for callers that need +// its encoding. +func (vm *VM) strCoerceArg(v object.Value) (string, *object.String) { + if u, ok := v.(object.KeyUnwrapper); ok { // a String subclass instance + if w, wrapped := u.HashUnwrap(); wrapped { + v = w + } + } + if s, ok := v.(*object.String); ok { + return s.Str(), s + } + if vm.respondsToDynamic(v, "to_str") { + r := vm.send(v, "to_str", nil, nil) + if s, ok := r.(*object.String); ok { + return s.Str(), s + } + raise("TypeError", "can't convert %s to String (%s#to_str gives %s)", + vm.classOf(v).name, vm.classOf(v).name, vm.classOf(r).name) + } + raise("TypeError", "no implicit conversion of %s into String", vm.classOf(v).name) + return "", nil +} + +// strSearchArg coerces the search argument of String#index / String#rindex. A +// Regexp (or a Regexp reached after unwrapping a subclass) passes through as +// (_, re, true). Otherwise the value is coerced to a String with #to_str, its +// encoding negotiated with self (raising Encoding::CompatibilityError when the +// two are incompatible), and returned as the needle. +func (vm *VM) strSearchArg(self, pat object.Value) (needle string, re *Regexp, isRe bool) { + if u, ok := pat.(object.KeyUnwrapper); ok { + if w, wrapped := u.HashUnwrap(); wrapped { + pat = w + } + } + if r, ok := pat.(*Regexp); ok { + return "", r, true + } + s, so := vm.strCoerceArg(pat) + vm.combinedEncName(self.(*object.String), so) // raises on incompatible encodings + return s, nil, false +} + +// runeMatchAt reports whether needle occurs in runes starting exactly at rune +// index p. +func runeMatchAt(runes, needle []rune, p int) bool { + if p < 0 || p+len(needle) > len(runes) { + return false + } + for i, r := range needle { + if runes[p+i] != r { + return false + } + } + return true +} + +// strRindexString returns the largest rune index p <= limit at which needle +// occurs in s (nil when there is none). limit has been clamped to the rune count +// by the caller. An empty needle matches at limit itself. +func strRindexString(s, needle string, limit int) object.Value { + runes := []rune(s) + needleRunes := []rune(needle) + maxStart := limit + if len(needleRunes) > 0 && maxStart > len(runes)-len(needleRunes) { + maxStart = len(runes) - len(needleRunes) + } + for p := maxStart; p >= 0; p-- { + if runeMatchAt(runes, needleRunes, p) { + return object.IntValue(int64(p)) + } + } + return object.NilV +} + func (vm *VM) stringIndexAssign(s *object.String, args []object.Value) object.Value { vm.checkFrozen(s) rhs := args[len(args)-1] diff --git a/internal/vm/regexp.go b/internal/vm/regexp.go index 577a6aa2..3eaa83eb 100644 --- a/internal/vm/regexp.go +++ b/internal/vm/regexp.go @@ -429,6 +429,38 @@ func (vm *VM) runMatchFrom(re *Regexp, subject string, pos int64) object.Value { return m } +// strIndexRegexp implements String#index with a Regexp: it finds the leftmost +// match at or after character offset off (matching the full subject so anchors +// and \G honour the offset), sets $~ (nil on no match) and returns the character +// index of the match start, or nil. off may equal the character count. +func (vm *VM) strIndexRegexp(re *Regexp, subject string, off int) object.Value { + byteOff := charToByte(subject, off) + md := re.matcher().Match(subject[byteOff:]) // leftmost match in the tail; \G pins to its start + if md == nil { + vm.lastMatch = object.NilV + return object.NilV + } + vm.lastMatch = &MatchData{md: md, subject: subject, re: re, byteOff: byteOff} + return object.IntValue(int64(byteToChar(subject, byteOff+md.Begin(0)))) +} + +// strRindexRegexp implements String#rindex with a Regexp: it returns the largest +// character index p (p <= limit) at which re matches starting exactly at p, +// setting $~ (nil when there is no such match). limit has been clamped to the +// character count by the caller. +func (vm *VM) strRindexRegexp(re *Regexp, subject string, limit int) object.Value { + for p := limit; p >= 0; p-- { + bytep := charToByte(subject, p) + md := re.matcher().MatchAt(subject, bytep) + if md != nil && md.Begin(0) == bytep { + vm.lastMatch = &MatchData{md: md, subject: subject, re: re} + return object.IntValue(int64(p)) + } + } + vm.lastMatch = object.NilV + return object.NilV +} + // gvar reads a global variable. The match-data specials derive from $~ (the // last match); any other name reads as nil (uninitialised global). func (vm *VM) gvar(name string) object.Value { diff --git a/internal/vm/string_method_residuals_test.go b/internal/vm/string_method_residuals_test.go new file mode 100644 index 00000000..423625b7 --- /dev/null +++ b/internal/vm/string_method_residuals_test.go @@ -0,0 +1,66 @@ +package vm_test + +import ( + "strings" + "testing" +) + +// evalCases runs a table of Ruby snippets, each expected to print want. +func evalCases(t *testing.T, cases []struct{ name, src, want string }) { + t.Helper() + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := eval(t, tc.src); got != tc.want { + t.Errorf("src=%q\n got=%q\nwant=%q", tc.src, got, tc.want) + } + }) + } +} + +func TestStringIndexResiduals(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"regexp_basic", `p "hello".index(/l/)`, "2\n"}, + {"regexp_none", `p "hello".index(/z/)`, "nil\n"}, + {"regexp_offset", `p "blablabla".index(/bla/, 1)`, "3\n"}, + {"regexp_sets_match", `"hello".index(/l/); p $~[0]`, "\"l\"\n"}, + {"regexp_multibyte", `p "ちがう".index(/が/)`, "1\n"}, + {"to_str_arg", `class C; def to_str; "b"; end; end; p "abc".index(C.new)`, "1\n"}, + {"to_int_offset", `class O; def to_int; 1; end; end; p "abc".index("c", O.new)`, "2\n"}, + {"subclass_arg", `class S < String; end; p "blablabla".index(S.new("bla"))`, "0\n"}, + {"empty_at_len", `p "abc".index("", 3)`, "3\n"}, + {"neg_offset", `p "blablabla".index("bl", -3)`, "6\n"}, + {"nil_typeerror", `begin; "abc".index(nil); rescue TypeError; puts "te"; end`, "te\n"}, + {"symbol_typeerror", `begin; "abc".index(:a); rescue TypeError; puts "te"; end`, "te\n"}, + {"regexp_clears_match_out_of_range", `"a".index(/a/); "bla".index(/bla/, 99); p $~`, "nil\n"}, + {"string_form_keeps_match", `$~ = nil; "hello".index("ll"); p $~`, "nil\n"}, + }) +} + +func TestStringRindexResiduals(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"string_last", `p "blablabla".rindex("bla")`, "6\n"}, + {"string_offset", `p "blablabla".rindex("bl", 2)`, "0\n"}, + {"string_none", `p "blablabla".rindex("z")`, "nil\n"}, + {"empty_clamped", `p "blablabla".rindex("", 10)`, "9\n"}, + {"empty_neg", `p "hello".rindex("", -6)`, "nil\n"}, + {"regexp_last", `p "blablabla".rindex(/bla/)`, "6\n"}, + {"regexp_dotstar", `p "blablabla".rindex(/.*/)`, "9\n"}, + {"regexp_dotplus", `p "blablabla".rindex(/.+/)`, "8\n"}, + {"regexp_caret", `p "\nblablabla".rindex(/^/)`, "1\n"}, + {"regexp_sets_match", `"blablabla".rindex(/bla/); p $~.begin(0)`, "6\n"}, + {"to_str_arg", `class C2; def to_str; "lo"; end; end; p "hello".rindex(C2.new)`, "3\n"}, + {"subclass_arg", `class S2 < String; end; p "blablabla".rindex(S2.new("bla"))`, "6\n"}, + {"nil_offset_typeerror", `begin; "str".rindex("st", nil); rescue TypeError; puts "te"; end`, "te\n"}, + {"integer_typeerror", `begin; "hello".rindex(42); rescue TypeError; puts "te"; end`, "te\n"}, + {"string_form_keeps_match", `$~ = nil; "hello".rindex("ll"); p $~`, "nil\n"}, + }) +} + +func TestStringIndexRindexErrorMessages(t *testing.T) { + if err := runErr(t, `"abc".index(nil)`); err == nil || !strings.Contains(err.Error(), "no implicit conversion") { + t.Fatalf("index(nil): %v", err) + } + if err := runErr(t, `"abc".rindex(:x)`); err == nil || !strings.Contains(err.Error(), "no implicit conversion") { + t.Fatalf("rindex(:x): %v", err) + } +} From 37fbfd4cea8d521fd3be114c42cd58dd51f3531e Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 17:39:21 +0200 Subject: [PATCH 2/7] String sub/gsub/scan/casecmp/chomp/partition: advance MRI 4.0.6 conformance - 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 --- internal/vm/builtins.go | 158 ++++++++++++++++---- internal/vm/regexp.go | 96 ++++++++++-- internal/vm/string_method_residuals_test.go | 68 +++++++++ 3 files changed, 280 insertions(+), 42 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index 9b4b312f..f856e3e9 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -1564,19 +1564,25 @@ func (vm *VM) bootstrap() { vm.cString.define("downcase", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return vm.stringCaseMap(self, caseDowncase, args) }) - vm.cString.define("casecmp", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - o, ok := args[0].(*object.String) - if !ok { // like <=>, a non-String operand compares to nil + vm.cString.define("casecmp", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + o, ok := vm.casecmpOther(self, args[0]) + if !ok { // a non-String (no #to_str) or an incompatible encoding compares to nil return object.NilV } - return object.IntValue(int64(strings.Compare(strings.ToLower(strOf(self)), strings.ToLower(o.Str())))) + // casecmp folds ASCII A-Z only, then compares byte for byte (non-ASCII bytes + // are never folded), matching MRI. + return object.IntValue(int64(strings.Compare(asciiDowncase(strOf(self)), asciiDowncase(o)))) }) - vm.cString.define("casecmp?", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - o, ok := args[0].(*object.String) + vm.cString.define("casecmp?", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + o, ok := vm.casecmpOther(self, args[0]) if !ok { return object.NilV } - return object.Bool(strings.EqualFold(strOf(self), o.Str())) + // casecmp? applies full Unicode case folding (so ß equals "ss" and ÄÖÜ equals + // äöü), unlike casecmp's ASCII-only fold. + a := caseMapUTF8(strOf(self), caseDowncase, caseFlags{fold: true}) + b := caseMapUTF8(o, caseDowncase, caseFlags{fold: true}) + return object.Bool(a == b) }) vm.cString.define("capitalize", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return vm.stringCaseMap(self, caseCapitalize, args) @@ -1643,8 +1649,8 @@ func (vm *VM) bootstrap() { vm.cString.define("rstrip", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return strEncOf(self, strings.TrimRight(strOf(self), wsCutset)) }) - vm.cString.define("chomp", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return strEncOf(self, chompSep(strOf(self), args)) + vm.cString.define("chomp", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return strEncOf(self, vm.chompSep(strOf(self), args)) }) vm.cString.define("chop", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return strEncOf(self, chopStr(strOf(self))) @@ -1908,7 +1914,7 @@ func (vm *VM) bootstrap() { return vm.runMatch(strMatchRegexp(args[0]), strOf(self)) }) vm.cString.define("scan", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { - return vm.scan(scanRegexp(args[0]), strOf(self), self, blk) + return vm.scan(vm.subRegexp(args[0]), strOf(self), self, blk) }) vm.cString.define("sub", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { return vm.stringSub(strOf(self), args, blk, false) @@ -2061,19 +2067,47 @@ func (vm *VM) bootstrap() { } return object.IntValue(int64([]rune(s)[0])) }) - vm.cString.define("partition", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - s, sep := strOf(self), strArg(args[0]) + vm.cString.define("partition", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + s := strOf(self) + enc := self.(*object.String).Enc + if re, ok := regexpSep(args[0]); ok { + md := re.matcher().Match(s) + if md == nil { + vm.lastMatch = object.NilV + return object.NewArray(strEncOf(self, s), strEncOf(self, ""), strEncOf(self, "")) + } + vm.lastMatch = &MatchData{md: md, subject: s, re: re} + b, e := md.Begin(0), md.End(0) + return object.NewArray(object.NewStringBytesEnc([]byte(s[:b]), enc), + object.NewStringBytesEnc([]byte(s[b:e]), enc), object.NewStringBytesEnc([]byte(s[e:]), enc)) + } + sep, sepObj := vm.strCoerceArg(args[0]) if i := strings.Index(s, sep); i >= 0 { - return object.NewArray(object.NewString(s[:i]), object.NewString(sep), object.NewString(s[i+len(sep):])) + return object.NewArray(object.NewStringBytesEnc([]byte(s[:i]), enc), + object.NewStringBytesEnc([]byte(sep), sepObj.Enc), object.NewStringBytesEnc([]byte(s[i+len(sep):]), enc)) } - return object.NewArray(object.NewString(s), object.NewString(""), object.NewString("")) + return object.NewArray(strEncOf(self, s), strEncOf(self, ""), strEncOf(self, "")) }) - vm.cString.define("rpartition", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - s, sep := strOf(self), strArg(args[0]) + vm.cString.define("rpartition", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + s := strOf(self) + enc := self.(*object.String).Enc + if re, ok := regexpSep(args[0]); ok { + m := vm.lastRegexpMatch(re, s) + if m == nil { + vm.lastMatch = object.NilV + return object.NewArray(strEncOf(self, ""), strEncOf(self, ""), strEncOf(self, s)) + } + vm.lastMatch = m + b, e := m.byteOff+m.md.Begin(0), m.byteOff+m.md.End(0) + return object.NewArray(object.NewStringBytesEnc([]byte(s[:b]), enc), + object.NewStringBytesEnc([]byte(s[b:e]), enc), object.NewStringBytesEnc([]byte(s[e:]), enc)) + } + sep, sepObj := vm.strCoerceArg(args[0]) if i := strings.LastIndex(s, sep); i >= 0 { - return object.NewArray(object.NewString(s[:i]), object.NewString(sep), object.NewString(s[i+len(sep):])) + return object.NewArray(object.NewStringBytesEnc([]byte(s[:i]), enc), + object.NewStringBytesEnc([]byte(sep), sepObj.Enc), object.NewStringBytesEnc([]byte(s[i+len(sep):]), enc)) } - return object.NewArray(object.NewString(""), object.NewString(""), object.NewString(s)) + return object.NewArray(strEncOf(self, ""), strEncOf(self, ""), strEncOf(self, s)) }) // String mutation (in-place). Every mutator guards against a frozen receiver. @@ -2201,7 +2235,7 @@ func (vm *VM) bootstrap() { return vm.strBang(self, func(x string) string { return strings.TrimRight(x, wsCutset) }) }) vm.cString.define("chomp!", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return vm.strBang(self, func(s string) string { return chompSep(s, args) }) + return vm.strBang(self, func(s string) string { return vm.chompSep(s, args) }) }) vm.cString.define("chop!", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return vm.strBang(self, chopStr) @@ -5660,14 +5694,33 @@ func reverseStr(s string) string { } // chompSep implements String#chomp's optional separator argument, matching MRI: -// - no argument (or an explicit nil): remove one trailing line ending. +// - no argument: use $/ (the record separator, default "\n"); a nil $/ disables +// chomping. +// - an explicit nil argument: no chomping (returns self unchanged). +// - the default separator "\n" (from $/ or given explicitly): smart mode — +// remove one trailing \r\n, \n, or \r. // - "" (empty): paragraph mode — remove ALL trailing newlines (\r\n / \n). -// - any other string: remove that exact suffix once, if present. -func chompSep(s string, args []object.Value) string { - if len(args) == 0 || args[0] == object.NilV { - return chompStr(s) +// - any other string (converted with #to_str): remove that exact suffix once. +func (vm *VM) chompSep(s string, args []object.Value) string { + var sep string + if len(args) == 0 { + rs := vm.gvar("$/") + if object.IsNil(rs) { // $/ = nil (or unset) means the default record separator + sep = "\n" + } else if rss, ok := rs.(*object.String); ok { + sep = rss.Str() + } else { + sep = "\n" + } + } else { + if object.IsNil(args[0]) { // chomp(nil): do not chomp at all + return s + } + sep, _ = vm.strCoerceArg(args[0]) + } + if sep == "\n" { + return chompStr(s) // smart mode: one trailing \r\n, \n, or \r } - sep := strArg(args[0]) if sep == "" { // Paragraph mode: strip every trailing \n (treating \r\n as one), so a run // of blank lines at the end collapses away. @@ -6222,7 +6275,8 @@ func (vm *VM) strSubBang(self object.Value, args []object.Value, blk *Proc, glob if !global { raise("ArgumentError", "wrong number of arguments (given 1, expected 2)") } - return enumFor(s, "gsub!", args[0]) + // MRI reports this enumerator's #size as nil (the match count is unknown). + return enumForSized(s, "gsub!", func(*VM) object.Value { return object.NilV }, args[0]) } res := vm.stringSub(s.Str(), args, blk, global).(*object.String) if res.Str() == s.Str() { @@ -6273,6 +6327,58 @@ func (vm *VM) strCoerceArg(v object.Value) (string, *object.String) { return "", nil } +// casecmpOther coerces the argument of String#casecmp / #casecmp? to a Go +// string: a String (or a String subclass, unwrapped) or a value with #to_str +// yields (str, true); a value that cannot become a String, or one whose encoding +// is incompatible with self, yields ("", false) so the caller returns nil — +// both methods answer nil in those cases rather than raising. +func (vm *VM) casecmpOther(self, other object.Value) (string, bool) { + if u, ok := other.(object.KeyUnwrapper); ok { + if w, wrapped := u.HashUnwrap(); wrapped { + other = w + } + } + os, ok := other.(*object.String) + if !ok { + if !vm.respondsToDynamic(other, "to_str") { + return "", false + } + r := vm.send(other, "to_str", nil, nil) + if os, ok = r.(*object.String); !ok { + return "", false + } + } + if object.IsNil(vm.encodingCompatible(self.(*object.String), os)) { + return "", false + } + return os.Str(), true +} + +// regexpSep reports whether v is a Regexp (after unwrapping a subclass wrapper), +// used by the separator methods that accept either a String or a Regexp. +func regexpSep(v object.Value) (*Regexp, bool) { + if u, ok := v.(object.KeyUnwrapper); ok { + if w, wrapped := u.HashUnwrap(); wrapped { + v = w + } + } + r, ok := v.(*Regexp) + return r, ok +} + +// asciiDowncase lowercases only the ASCII letters A-Z of s, leaving every other +// byte (including non-ASCII bytes of a multibyte character) untouched — the fold +// String#casecmp applies before its byte comparison. +func asciiDowncase(s string) string { + b := []byte(s) + for i := 0; i < len(b); i++ { + if b[i] >= 'A' && b[i] <= 'Z' { + b[i] += 'a' - 'A' + } + } + return string(b) +} + // strSearchArg coerces the search argument of String#index / String#rindex. A // Regexp (or a Regexp reached after unwrapping a subclass) passes through as // (_, re, true). Otherwise the value is coerced to a String with #to_str, its diff --git a/internal/vm/regexp.go b/internal/vm/regexp.go index 3eaa83eb..1695f2d1 100644 --- a/internal/vm/regexp.go +++ b/internal/vm/regexp.go @@ -444,6 +444,20 @@ func (vm *VM) strIndexRegexp(re *Regexp, subject string, off int) object.Value { return object.IntValue(int64(byteToChar(subject, byteOff+md.Begin(0)))) } +// lastRegexpMatch returns the match of re that begins at the largest byte offset +// in subject (the rightmost match, as String#rpartition selects it), or nil when +// there is none. It probes anchored matches from the end toward the start, so the +// whole string stays visible to anchors and lookbehind. +func (vm *VM) lastRegexpMatch(re *Regexp, subject string) *MatchData { + for p := len(subject); p >= 0; p-- { + md := re.matcher().MatchAt(subject, p) + if md != nil && md.Begin(0) == p { + return &MatchData{md: md, subject: subject, re: re} + } + } + return nil +} + // strRindexRegexp implements String#rindex with a Regexp: it returns the largest // character index p (p <= limit) at which re matches starting exactly at p, // setting $~ (nil when there is no such match). limit has been clamped to the @@ -597,15 +611,28 @@ func regexpEscapeLiteral(s string) string { // it terminates; a non-empty match advances past its end. func (vm *VM) scan(re *Regexp, subject string, self object.Value, blk *Proc) object.Value { var results []object.Value + enc := "" // result substrings inherit the receiver's encoding + if s, ok := self.(*object.String); ok { + enc = s.Enc + } + last := object.Value(object.NilV) // $~ after the call: last match, or nil when none pos := 0 for pos <= len(subject) { md := re.matcher().Match(subject[pos:]) if md == nil { break } - elem := scanElement(md) + elem := scanElement(md, enc) + // Expose this match through $~ / $1.. (with the whole subject and an + // absolute byteOff so MatchData#string, #begin and #offset are correct). + // MRI leaves $~ set to the last match after scan returns, even if a block + // reassigned it, so re-set it after the block runs. + cur := &MatchData{md: md, subject: subject, re: re, byteOff: pos} + vm.lastMatch = cur + last = cur if blk != nil { vm.callBlock(blk, []object.Value{elem}) + vm.lastMatch = cur } else { results = append(results, elem) } @@ -622,6 +649,8 @@ func (vm *VM) scan(re *Regexp, subject string, self object.Value, blk *Proc) obj pos += matchEnd } } + // $~ is the last match (or nil when there was none), as MRI leaves it. + vm.lastMatch = last if blk != nil { return self } @@ -803,23 +832,46 @@ func captureFields(md *onig.MatchData, enc string) []object.Value { // gsub returns an Enumerator over the matches; sub raises ArgumentError, as MRI // does. func (vm *VM) stringSub(subject string, args []object.Value, blk *Proc, global bool) object.Value { - re := scanRegexp(args[0]) + re := vm.subRegexp(args[0]) + // A replacement argument takes precedence over a block: MRI ignores the block + // when a String or Hash replacement is also supplied. + if len(args) >= 2 { + if h, ok := args[1].(*object.Hash); ok { + return vm.gsubHash(re, subject, h, global) + } + repl, _ := vm.strCoerceArg(args[1]) // a non-String replacement converts via #to_str + return vm.gsub(re, subject, repl, nil, global) + } if blk != nil { return vm.gsub(re, subject, "", blk, global) } - if len(args) < 2 { - if !global { - raise("ArgumentError", "wrong number of arguments (given 1, expected 2)") + if !global { + raise("ArgumentError", "wrong number of arguments (given 1, expected 2)") + } + // gsub(pattern) with no replacement and no block → an Enumerator yielding + // the matched substrings; supports #with_index, #to_a, etc. via the + // receiver+method form, replaying gsub with the enumerator's block. MRI reports + // this enumerator's #size as nil (the match count is not known ahead of time). + return enumForSized(object.NewString(subject), "gsub", + func(*VM) object.Value { return object.NilV }, args[0]) +} + +// subRegexp coerces the pattern argument of String#sub/#gsub into a Regexp: a +// Regexp passes through; anything else is coerced to a String (via #to_str, +// unwrapping a String subclass) and matched literally, exactly as MRI does. +func (vm *VM) subRegexp(v object.Value) *Regexp { + if u, ok := v.(object.KeyUnwrapper); ok { + if w, wrapped := u.HashUnwrap(); wrapped { + v = w } - // gsub(pattern) with no replacement and no block → an Enumerator yielding - // the matched substrings; supports #with_index, #to_a, etc. via the - // receiver+method form, replaying gsub with the enumerator's block. - return enumFor(object.NewString(subject), "gsub", args[0]) } - if h, ok := args[1].(*object.Hash); ok { - return vm.gsubHash(re, subject, h, global) + if r, ok := v.(*Regexp); ok { + return r } - return vm.gsub(re, subject, strArg(args[1]), nil, global) + s, _ := vm.strCoerceArg(v) + src := regexpEscapeLiteral(s) + re, _ := onig.Compile(src) + return &Regexp{re: re, source: src} } // gsub implements String#sub (global=false) and String#gsub (global=true) over @@ -958,6 +1010,16 @@ func expandReplacement(tmpl string, md *onig.MatchData, pre, post string) string case n == '&': b.WriteString(md.Str(0)) i++ + case n == '+': + // \+ inserts the last capture group that participated (highest-numbered), + // or nothing when there were no captures or none participated. + for gi := md.NGroups(); gi >= 1; gi-- { + if md.Begin(gi) >= 0 { + b.WriteString(md.Str(gi)) + break + } + } + i++ case n == '`': b.WriteString(pre) i++ @@ -992,18 +1054,20 @@ func expandReplacement(tmpl string, md *onig.MatchData, pre, post string) string } // scanElement builds one String#scan result element from a match: the whole -// match (no groups) or the array of captures (one or more groups). -func scanElement(md *onig.MatchData) object.Value { +// match (no groups) or the array of captures (one or more groups). Every result +// substring inherits enc, the receiver's encoding (MRI keeps scan pieces in the +// same encoding as self). +func scanElement(md *onig.MatchData, enc string) object.Value { n := md.NGroups() if n == 0 { - return object.NewString(md.Str(0)) + return object.NewStringViewEnc(md.Str(0), enc) } caps := make([]object.Value, n) for i := 1; i <= n; i++ { if md.Begin(i) < 0 { caps[i-1] = object.NilV } else { - caps[i-1] = object.NewStringView(md.Str(i)) + caps[i-1] = object.NewStringViewEnc(md.Str(i), enc) } } return object.NewArrayFromSlice(caps) diff --git a/internal/vm/string_method_residuals_test.go b/internal/vm/string_method_residuals_test.go index 423625b7..bf2cd596 100644 --- a/internal/vm/string_method_residuals_test.go +++ b/internal/vm/string_method_residuals_test.go @@ -56,6 +56,74 @@ func TestStringRindexResiduals(t *testing.T) { }) } +func TestStringSubGsubResiduals(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"backref_plus_last_paren", `p "hello".gsub(/(l)o/, '<\+>')`, "\"hel\"\n"}, + {"backref_plus_no_capture", `p "hello".sub(/l/, '<\+>')`, "\"he<>lo\"\n"}, + {"sub_ignores_block", `p "food".sub(/f/, "g"){"w"}`, "\"good\"\n"}, + {"gsub_ignores_block", `p "food".gsub(/o/, "0"){"w"}`, "\"f00d\"\n"}, + {"sub_to_str_replacement", `class R; def to_str; "X"; end; end; p "abc".sub(/b/, R.new)`, "\"aXc\"\n"}, + {"sub_to_str_pattern", `class P; def to_str; "b"; end; end; p "abc".sub(P.new, "X")`, "\"aXc\"\n"}, + {"gsub_hash", `p "hello".gsub(/[el]/, "e" => "3", "l" => "1")`, "\"h311o\"\n"}, + {"gsub_enum_size_nil", `p "hello".gsub(/l/).size`, "nil\n"}, + }) +} + +func TestStringScanResiduals(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"basic", `p "cruel world".scan(/\w+/)`, "[\"cruel\", \"world\"]\n"}, + {"sets_match", `"hello.".scan(/.(.)/); p $~[0]`, "\"o.\"\n"}, + {"match_nil_after_none", `"hello.".scan(/xyz/); p $~`, "nil\n"}, + {"block_sees_match", `m=[]; "hello".scan(/([aeiou])/){ m << $~.offset(0) }; p m`, "[[1, 2], [4, 5]]\n"}, + {"restores_after_block", `"hello".scan(/./){ "ok".match(/./) }; p $~[0]`, "\"o\"\n"}, + {"to_str_pattern", `class SP; def to_str; "o"; end; end; p "o_o".scan(SP.new)`, "[\"o\", \"o\"]\n"}, + }) +} + +func TestStringCasecmpResiduals(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"ascii", `p "A".casecmp("a")`, "0\n"}, + {"nonascii_no_fold", `p "Ã".casecmp("ã")`, "-1\n"}, + {"nonascii_umlaut", `p "Ã".casecmp("Ä")`, "-1\n"}, + {"no_fold_sharp_s", `p "ß".casecmp("ss")`, "1\n"}, + {"to_str", `class CO; def to_str; "abc"; end; end; p "abc".casecmp(CO.new)`, "0\n"}, + {"nil_when_unconvertible", `p "abc".casecmp(123)`, "nil\n"}, + {"subclass", `class MS < String; end; p "a".casecmp(MS.new("b"))`, "-1\n"}, + {"q_equal", `p "abc".casecmp?("ABC")`, "true\n"}, + {"q_unicode_fold", `p "äöü".casecmp?("ÄÖÜ")`, "true\n"}, + {"q_sharp_s", `p "ß".casecmp?("ss")`, "true\n"}, + {"q_unrelated", `p "Ã".casecmp?("Ä")`, "false\n"}, + {"q_nil_unconvertible", `p "abc".casecmp?(123)`, "nil\n"}, + }) +} + +func TestStringChompResiduals(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"newline_sep_removes_cr", `p "abc\r\r".chomp("\n")`, "\"abc\\r\"\n"}, + {"newline_sep_crlf", `p "abc\r\n\r\n".chomp("\n")`, "\"abc\\r\\n\"\n"}, + {"nil_no_chomp", `p "abc\r\n".chomp(nil)`, "\"abc\\r\\n\"\n"}, + {"custom_dollar_slash", `$/ = "cdef"; p "abcdef".chomp`, "\"ab\"\n"}, + {"default_smart", `$/ = "\n"; p "abc\r\r".chomp`, "\"abc\\r\"\n"}, + {"to_str_arg", `class ChA; def to_str; "bc"; end; end; p "abc".chomp(ChA.new)`, "\"a\"\n"}, + {"paragraph_mode", `p "abc\n\n\n".chomp("")`, "\"abc\"\n"}, + {"bang_nil_returns_nil", `p "abc".chomp!(nil)`, "nil\n"}, + }) +} + +func TestStringPartitionResiduals(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"partition_regexp", `p "hello!".partition(/l./)`, "[\"he\", \"ll\", \"o!\"]\n"}, + {"rpartition_regexp", `p "hello!".rpartition(/l./)`, "[\"hel\", \"lo\", \"!\"]\n"}, + {"partition_sets_globals", `"hello!".partition(/(.l)(.o)/); p [$1, $2]`, "[\"el\", \"lo\"]\n"}, + {"partition_string", `p "hello".partition("l")`, "[\"he\", \"l\", \"lo\"]\n"}, + {"rpartition_string", `p "hello".rpartition("l")`, "[\"hel\", \"l\", \"o\"]\n"}, + {"partition_no_match", `p "hello".partition("x")`, "[\"hello\", \"\", \"\"]\n"}, + {"rpartition_no_match", `p "hello".rpartition("x")`, "[\"\", \"\", \"hello\"]\n"}, + {"partition_to_str", `class LP; def to_str; "l"; end; end; p "hello".partition(LP.new)`, "[\"he\", \"l\", \"lo\"]\n"}, + {"partition_typeerror", `begin; "hello".partition(5); rescue TypeError; puts "te"; end`, "te\n"}, + }) +} + func TestStringIndexRindexErrorMessages(t *testing.T) { if err := runErr(t, `"abc".index(nil)`); err == nil || !strings.Contains(err.Error(), "no implicit conversion") { t.Fatalf("index(nil): %v", err) From a685e69c631fe7db5f6cb65f2a2724b418e32e22 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 17:58:20 +0200 Subject: [PATCH 3/7] String tr/count/delete/squeeze/split/replace/bytes/codepoints: MRI 4.0.6 - 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 --- internal/vm/builtins.go | 86 ++++++--- internal/vm/regexp.go | 27 +++ internal/vm/string_charset.go | 196 ++++++++++++-------- internal/vm/string_method_residuals_test.go | 59 ++++++ 4 files changed, 263 insertions(+), 105 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index f856e3e9..d4bfc234 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -1597,7 +1597,6 @@ func (vm *VM) bootstrap() { return object.NewString(succString(strOf(self))) } vm.cString.define("succ", succStr) - vm.cString.define("next", succStr) succBang := func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { s := self.(*object.String) vm.checkFrozen(s) @@ -1605,7 +1604,10 @@ func (vm *VM) bootstrap() { return s } vm.cString.define("succ!", succBang) - vm.cString.define("next!", succBang) + // #next and #next! are true aliases of #succ / #succ! (they share the same + // Method object, so instance_method(:next) == instance_method(:succ)). + vm.aliasMethod(vm.cString, "next", "succ") + vm.aliasMethod(vm.cString, "next!", "succ!") vm.cString.define("chr", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return object.NewString(stringChr(strOf(self))) }) @@ -1670,8 +1672,14 @@ func (vm *VM) bootstrap() { } return object.NewArrayFromSlice(out) }) - vm.cString.define("bytes", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + vm.cString.define("bytes", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { s := strOf(self) + if blk != nil { // the block form yields each byte and returns the receiver (MRI) + for i := 0; i < len(s); i++ { + vm.callBlock(blk, []object.Value{object.IntValue(int64(s[i]))}) + } + return self + } out := make([]object.Value, len(s)) for i := 0; i < len(s); i++ { out[i] = object.IntValue(int64(s[i])) @@ -1704,7 +1712,8 @@ func (vm *VM) bootstrap() { }) vm.cString.define("each_line", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { if blk == nil { - return enumFor(self, "each_line", args...) + // MRI reports this enumerator's #size as nil (the line count is unknown). + return enumForSized(self, "each_line", func(*VM) object.Value { return object.NilV }, args...) } for _, seg := range vm.stringLineSegs(self, args) { vm.callBlock(blk, []object.Value{seg}) @@ -1768,7 +1777,13 @@ func (vm *VM) bootstrap() { } return self }) - vm.cString.define("codepoints", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + vm.cString.define("codepoints", func(vm *VM, self object.Value, _ []object.Value, blk *Proc) object.Value { + if blk != nil { // the block form yields each codepoint and returns the receiver (MRI) + for _, r := range strOf(self) { + vm.callBlock(blk, []object.Value{object.IntValue(int64(r))}) + } + return self + } var out []object.Value for _, r := range strOf(self) { out = append(out, object.IntValue(int64(r))) @@ -1985,31 +2000,34 @@ func (vm *VM) bootstrap() { vm.cString.define("center", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return strEncOf(self, vm.padString(strOf(self), args, 'c')) }) - trFn := func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return strEncOf(self, trString(strOf(self), strArg(args[0]), strArg(args[1]), false)) + trFn := func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return strEncOf(self, trString(strOf(self), vm.strTrArg(args[0]), vm.strTrArg(args[1]), false)) } vm.cString.define("tr", trFn) - trSFn := func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return strEncOf(self, trString(strOf(self), strArg(args[0]), strArg(args[1]), true)) + trSFn := func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return strEncOf(self, trString(strOf(self), vm.strTrArg(args[0]), vm.strTrArg(args[1]), true)) } vm.cString.define("tr_s", trSFn) - vm.cString.define("tr!", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return vm.strBang(self, func(s string) string { return trString(s, strArg(args[0]), strArg(args[1]), false) }) + vm.cString.define("tr!", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + from, to := vm.strTrArg(args[0]), vm.strTrArg(args[1]) + return vm.strBang(self, func(s string) string { return trString(s, from, to, false) }) }) - vm.cString.define("tr_s!", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return vm.strBang(self, func(s string) string { return trString(s, strArg(args[0]), strArg(args[1]), true) }) + vm.cString.define("tr_s!", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + from, to := vm.strTrArg(args[0]), vm.strTrArg(args[1]) + return vm.strBang(self, func(s string) string { return trString(s, from, to, true) }) }) - vm.cString.define("count", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return object.IntValue(int64(stringCount(strOf(self), args))) + vm.cString.define("count", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return object.IntValue(int64(stringCount(strOf(self), vm.coerceSetArgs(args)))) }) - vm.cString.define("delete", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return strEncOf(self, stringDelete(strOf(self), args)) + vm.cString.define("delete", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return strEncOf(self, stringDelete(strOf(self), vm.coerceSetArgs(args))) }) - vm.cString.define("delete!", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return vm.strBang(self, func(s string) string { return stringDelete(s, args) }) + vm.cString.define("delete!", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + sets := vm.coerceSetArgs(args) + return vm.strBang(self, func(s string) string { return stringDelete(s, sets) }) }) - vm.cString.define("squeeze", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return strEncOf(self, stringSqueeze(strOf(self), args)) + vm.cString.define("squeeze", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return strEncOf(self, stringSqueeze(strOf(self), vm.coerceSetArgs(args))) }) strIndexFn := func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { if len(args) < 1 || len(args) > 2 { @@ -2165,10 +2183,11 @@ func (vm *VM) bootstrap() { return strConcatFn(vm, self, args, blk) }) vm.cString.define("concat", strConcatFn) - vm.cString.define("replace", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cString.define("replace", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { s := self.(*object.String) vm.checkFrozen(s) - s.SetBytes([]byte(strArg(args[0]))) + repl, _ := vm.strCoerceArg(args[0]) // a non-String source converts via #to_str + s.SetBytes([]byte(repl)) return s }) vm.cString.define("prepend", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { @@ -6354,6 +6373,24 @@ func (vm *VM) casecmpOther(self, other object.Value) (string, bool) { return os.Str(), true } +// strTrArg coerces one character-set argument of tr/tr_s/count/delete/squeeze to +// a Go string, converting a non-String via #to_str (unwrapping a String +// subclass), exactly as MRI does. +func (vm *VM) strTrArg(v object.Value) string { + s, _ := vm.strCoerceArg(v) + return s +} + +// coerceSetArgs replaces each character-set argument with the String its #to_str +// yields, so the charset routines that read them see real Strings. +func (vm *VM) coerceSetArgs(args []object.Value) []object.Value { + out := make([]object.Value, len(args)) + for i, a := range args { + out[i] = object.NewString(vm.strTrArg(a)) + } + return out +} + // regexpSep reports whether v is a Regexp (after unwrapping a subclass wrapper), // used by the separator methods that accept either a String or a Regexp. func regexpSep(v object.Value) (*Regexp, bool) { @@ -8093,6 +8130,9 @@ func (vm *VM) stringLineSegs(self object.Value, args []object.Value) []object.Va sep := "\n" if len(pos) > 0 { sep = vm.coerceFormatString(pos[0]) + } else if rs, ok := vm.gvar("$/").(*object.String); ok { + // With no separator argument the record separator $/ is used (default "\n"). + sep = rs.Str() } if sep == "" { segs = splitParagraphs(s, chomp) diff --git a/internal/vm/regexp.go b/internal/vm/regexp.go index 1695f2d1..07803cfa 100644 --- a/internal/vm/regexp.go +++ b/internal/vm/regexp.go @@ -671,6 +671,16 @@ func (vm *VM) scan(re *Regexp, subject string, self object.Value, blk *Proc) obj // every result substring inherits (MRI keeps split pieces in the same encoding // as self). func (vm *VM) stringSplit(subject, enc string, args []object.Value) object.Value { + // A nil or absent pattern falls back to $; (the field separator): when $; is a + // String it becomes the pattern; a nil $; keeps awk-style whitespace mode. + if len(args) == 0 || object.IsNil(args[0]) { + switch fs := vm.gvar("$;").(type) { + case *object.String: + args = replaceSplitPattern(args, fs) + case *Regexp: + args = replaceSplitPattern(args, fs) + } + } // A pattern that is neither a String, a Regexp nor nil is converted with // #to_str, and a non-Integer limit with #to_int, exactly as MRI does. Copy the // slice before rewriting the pattern so the caller's arguments are untouched. @@ -697,6 +707,17 @@ func (vm *VM) stringSplit(subject, enc string, args []object.Value) object.Value return splitRegexp(re, subject, limit, enc) } +// replaceSplitPattern returns args with the pattern (args[0]) set to pat, +// appending it when args was empty. The input slice is never mutated. +func replaceSplitPattern(args []object.Value, pat object.Value) []object.Value { + if len(args) == 0 { + return []object.Value{pat} + } + out := append([]object.Value(nil), args...) + out[0] = pat + return out +} + // splitOnWhitespace reports whether the split should use awk-style whitespace // mode: no pattern, a nil pattern, or the literal single space " ". func splitOnWhitespace(args []object.Value) bool { @@ -736,6 +757,12 @@ func splitWhitespace(subject string, limit int, enc string) object.Value { } out = append(out, object.NewStringViewEnc(subject[start:i], enc)) } + // With an explicit non-zero limit, a run of trailing whitespace yields one + // trailing empty field (awk mode collapses it to a single ""); the default + // (limit 0) suppresses trailing empty fields. + if limit != 0 && n > 0 && isASCIISpace(subject[n-1]) { + out = append(out, object.NewStringViewEnc("", enc)) + } return object.NewArrayFromSlice(out) } diff --git a/internal/vm/string_charset.go b/internal/vm/string_charset.go index 1dd2a90a..1db9becf 100644 --- a/internal/vm/string_charset.go +++ b/internal/vm/string_charset.go @@ -8,20 +8,36 @@ import ( "github.com/go-embedded-ruby/ruby/internal/object" ) -// trSet is a parsed tr/count/delete/squeeze character selector: a 256-entry -// byte-membership table plus a negation flag for a leading '^'. It backs the -// intersection semantics shared by String#count, #delete(!) and #squeeze(!). +// trSet is a parsed tr/count/delete/squeeze character selector: a set of member +// characters (each a whole UTF-8 code point, or a single invalid byte) plus a +// negation flag for a leading '^'. It backs the intersection semantics shared by +// String#count, #delete(!) and #squeeze(!). Characters are keyed by their raw +// bytes, so multibyte code points match as a unit and never partially. type trSet struct { - in [256]bool + in map[string]bool neg bool } -// parseTrList expands a tr-style selector into its ordered member bytes and a -// negation flag. It honours "a-z" ranges and '\' escapes (so "\\^", "\\-" and -// "\\\\" are literals). When allowNeg is true a leading '^' negates membership, -// unless it is the selector's only character, in which case it is literal. A -// reversed range such as "z-a" raises ArgumentError exactly as MRI does. -func parseTrList(sel string, allowNeg bool) ([]byte, bool) { +// charUnits splits s into its character units: each is a whole UTF-8 code point, +// or a single byte for an invalid sequence (so byte-oriented behaviour survives +// on ill-formed input). +func charUnits(s string) []string { + var out []string + for i := 0; i < len(s); { + _, size := utf8.DecodeRuneInString(s[i:]) + out = append(out, s[i:i+size]) + i += size + } + return out +} + +// parseTrList expands a tr-style selector into its ordered member characters and +// a negation flag. It honours "a-z" ranges (expanded by code point) and '\' +// escapes (so "\\^", "\\-" and "\\\\" are literals). When allowNeg is true a +// leading '^' negates membership, unless it is the selector's only character, in +// which case it is literal. A reversed range such as "z-a" raises ArgumentError, +// exactly as MRI does. +func parseTrList(sel string, allowNeg bool) ([]string, bool) { b := []byte(sel) neg := false i := 0 @@ -29,23 +45,27 @@ func parseTrList(sel string, allowNeg bool) ([]byte, bool) { neg = true i = 1 } - // readByte reads one selector byte at p, consuming a '\' escape when present. - readByte := func(p int) (byte, int) { + // readUnit reads one selector character at p, consuming a '\' escape when + // present; the returned unit is a whole code point (or one invalid byte). + readUnit := func(p int) (string, int) { if b[p] == '\\' && p+1 < len(b) { - return b[p+1], p + 2 + p++ } - return b[p], p + 1 + _, size := utf8.DecodeRune(b[p:]) + return string(b[p : p+size]), p + size } - var out []byte + var out []string for i < len(b) { - lo, next := readByte(i) + lo, next := readUnit(i) if next < len(b) && b[next] == '-' && next+1 < len(b) { - hi, after := readByte(next + 1) - if lo > hi { - raise("ArgumentError", "invalid range \"%c-%c\" in string transliteration", lo, hi) + hi, after := readUnit(next + 1) + loR, _ := utf8.DecodeRuneInString(lo) + hiR, _ := utf8.DecodeRuneInString(hi) + if loR > hiR { + raise("ArgumentError", "invalid range \"%s-%s\" in string transliteration", lo, hi) } - for c := int(lo); c <= int(hi); c++ { - out = append(out, byte(c)) + for c := loR; c <= hiR; c++ { + out = append(out, string(c)) } i = after continue @@ -59,26 +79,26 @@ func parseTrList(sel string, allowNeg bool) ([]byte, bool) { // newTrSet builds a membership set from a single selector argument. func newTrSet(sel string) *trSet { chars, neg := parseTrList(sel, true) - ts := &trSet{neg: neg} + ts := &trSet{in: make(map[string]bool, len(chars)), neg: neg} for _, c := range chars { ts.in[c] = true } return ts } -// has reports membership, applying negation. -func (t *trSet) has(b byte) bool { +// has reports membership of character unit c, applying negation. +func (t *trSet) has(c string) bool { if t.neg { - return !t.in[b] + return !t.in[c] } - return t.in[b] + return t.in[c] } -// trMatchAll reports whether b belongs to every selector — the intersection -// semantics of count/delete/squeeze with multiple arguments. -func trMatchAll(sets []*trSet, b byte) bool { +// trMatchAll reports whether character unit c belongs to every selector — the +// intersection semantics of count/delete/squeeze with multiple arguments. +func trMatchAll(sets []*trSet, c string) bool { for _, s := range sets { - if !s.has(b) { + if !s.has(c) { return false } } @@ -102,75 +122,71 @@ func parseTrSets(args []object.Value) []*trSet { func stringCount(s string, args []object.Value) int { sets := parseTrSets(args) n := 0 - for i := 0; i < len(s); i++ { - if trMatchAll(sets, s[i]) { + for _, c := range charUnits(s) { + if trMatchAll(sets, c) { n++ } } return n } -// stringDelete implements String#delete over one or more selectors: a byte is -// removed only when it belongs to every selector. +// stringDelete implements String#delete over one or more selectors: a character +// is removed only when it belongs to every selector. func stringDelete(s string, args []object.Value) string { sets := parseTrSets(args) - out := make([]byte, 0, len(s)) - for i := 0; i < len(s); i++ { - if !trMatchAll(sets, s[i]) { - out = append(out, s[i]) + var out strings.Builder + for _, c := range charUnits(s) { + if !trMatchAll(sets, c) { + out.WriteString(c) } } - return string(out) + return out.String() } // stringSqueeze implements String#squeeze: collapse each run of an identical -// byte to one. With no selector every run collapses; with selectors only runs -// of a byte in the intersection collapse. +// character to one. With no selector every run collapses; with selectors only +// runs of a character in the intersection collapse. func stringSqueeze(s string, args []object.Value) string { var sets []*trSet for _, a := range args { sets = append(sets, newTrSet(strArg(a))) } - out := make([]byte, 0, len(s)) - for i := 0; i < len(s); i++ { - if i > 0 && s[i] == s[i-1] && (len(sets) == 0 || trMatchAll(sets, s[i])) { + var out strings.Builder + prev := "" + first := true + for _, c := range charUnits(s) { + if !first && c == prev && (len(sets) == 0 || trMatchAll(sets, c)) { continue } - out = append(out, s[i]) + out.WriteString(c) + prev = c + first = false } - return string(out) + return out.String() } // trString implements String#tr and (when squeeze is true) String#tr_s. The -// from-selector may be negated with a leading '^', in which case every byte not -// in the set maps to the last byte of to (or is deleted when to is empty). For a -// plain from-selector each source byte maps to the to-byte at the same position, -// the last to-byte repeating when to is shorter; an empty to deletes. When a -// source byte appears more than once in from, the last occurrence wins. tr_s -// additionally collapses adjacent runs of an identical translated byte. +// from-selector may be negated with a leading '^', in which case every character +// not in the set maps to the last character of to (or is deleted when to is +// empty). For a plain from-selector each source character maps to the to-character +// at the same position, the last to-character repeating when to is shorter; an +// empty to deletes. When a source character appears more than once in from, the +// last occurrence wins. tr_s additionally collapses adjacent runs of an identical +// translated character. All matching is by whole code point, so a multibyte +// character never matches on part of its bytes. func trString(s, from, to string, squeeze bool) string { fromChars, neg := parseTrList(from, true) toChars, _ := parseTrList(to, false) - const unchanged = -2 - const del = -1 - var repl [256]int - for i := range repl { - repl[i] = unchanged - } + const del = "\x00del" // sentinel: delete this character + repl := map[string]string{} // source char -> replacement char, or del + var negRepl string // replacement for the negated case + negDelete := false if neg { - last := del if len(toChars) > 0 { - last = int(toChars[len(toChars)-1]) - } - var inFrom [256]bool - for _, c := range fromChars { - inFrom[c] = true - } - for c := 0; c < 256; c++ { - if !inFrom[c] { - repl[c] = last - } + negRepl = toChars[len(toChars)-1] + } else { + negDelete = true } } else { for i, c := range fromChars { @@ -178,35 +194,51 @@ func trString(s, from, to string, squeeze bool) string { case len(toChars) == 0: repl[c] = del case i < len(toChars): - repl[c] = int(toChars[i]) + repl[c] = toChars[i] default: - repl[c] = int(toChars[len(toChars)-1]) + repl[c] = toChars[len(toChars)-1] } } } + inFrom := make(map[string]bool, len(fromChars)) + for _, c := range fromChars { + inFrom[c] = true + } - out := make([]byte, 0, len(s)) - lastOut := -1 + var out strings.Builder + lastOut := "" lastTranslated := false - for i := 0; i < len(s); i++ { - r := repl[s[i]] - if r == unchanged { - out = append(out, s[i]) - lastTranslated = false - continue + for _, c := range charUnits(s) { + var r string + translate := false + if neg { + if !inFrom[c] { + if negDelete { + lastTranslated = false + continue + } + r, translate = negRepl, true + } + } else if v, ok := repl[c]; ok { + if v == del { + lastTranslated = false + continue + } + r, translate = v, true } - if r == del { + if !translate { + out.WriteString(c) lastTranslated = false continue } if squeeze && lastTranslated && lastOut == r { continue } - out = append(out, byte(r)) + out.WriteString(r) lastOut = r lastTranslated = true } - return string(out) + return out.String() } // stringChr returns the first character (a whole UTF-8 code point, or the first diff --git a/internal/vm/string_method_residuals_test.go b/internal/vm/string_method_residuals_test.go index bf2cd596..8ef9d45f 100644 --- a/internal/vm/string_method_residuals_test.go +++ b/internal/vm/string_method_residuals_test.go @@ -94,9 +94,19 @@ func TestStringCasecmpResiduals(t *testing.T) { {"q_sharp_s", `p "ß".casecmp?("ss")`, "true\n"}, {"q_unrelated", `p "Ã".casecmp?("Ä")`, "false\n"}, {"q_nil_unconvertible", `p "abc".casecmp?(123)`, "nil\n"}, + {"incompatible_encoding_nil", `p "あれ".casecmp("れ".encode("EUC-JP"))`, "nil\n"}, + {"q_incompatible_encoding_nil", `p "あれ".casecmp?("れ".encode("EUC-JP"))`, "nil\n"}, }) } +func TestStringCoerceArgNonStringToStr(t *testing.T) { + // #to_str returning a non-String raises TypeError with MRI's message. + err := runErr(t, `class B; def to_str; 5; end; end; "x".chomp(B.new)`) + if err == nil || !strings.Contains(err.Error(), "can't convert B to String (B#to_str gives Integer)") { + t.Fatalf("chomp(bad to_str): %v", err) + } +} + func TestStringChompResiduals(t *testing.T) { evalCases(t, []struct{ name, src, want string }{ {"newline_sep_removes_cr", `p "abc\r\r".chomp("\n")`, "\"abc\\r\"\n"}, @@ -124,6 +134,55 @@ func TestStringPartitionResiduals(t *testing.T) { }) } +func TestStringCharsetResiduals(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"tr_multibyte_to_single", `p "über".tr("ü", "u")`, "\"uber\"\n"}, + {"tr_single_to_multibyte", `p "uber".tr("u", "ü")`, "\"über\"\n"}, + {"tr_negate", `p "hello".tr("^aeiou", "*")`, "\"*e**o\"\n"}, + {"tr_range", `p "hello".tr("a-y", "b-z")`, "\"ifmmp\"\n"}, + {"tr_s_squeeze", `p "mississippi".tr_s("sp", "*")`, "\"mi*i*i*i\"\n"}, + {"count_multibyte", `p "abcが".count("が")`, "1\n"}, + {"delete_multibyte", `p "我倒是".delete("倒")`, "\"我是\"\n"}, + {"delete_to_str", `class DF; def to_str; "l"; end; end; p "hello".delete(DF.new)`, "\"heo\"\n"}, + {"squeeze_range", `p "aaabbbccc".squeeze("a-b")`, "\"abccc\"\n"}, + {"tr_to_str_args", `class TA; def to_str; "e"; end; end; class TB; def to_str; "i"; end; end; p "hello".tr(TA.new, TB.new)`, "\"hillo\"\n"}, + {"tr_reversed_range_raises", `begin; "x".tr("z-a", "y"); rescue ArgumentError; puts "ae"; end`, "ae\n"}, + {"count_to_str_arg", `class CA; def to_str; "l"; end; end; p "hello".count(CA.new)`, "2\n"}, + }) +} + +func TestStringBytesCodepointsBlock(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"bytes_block_returns_self", `p "ab".bytes { |b| }`, "\"ab\"\n"}, + {"bytes_block_yields", `a=[]; "ab".bytes { |b| a << b }; p a`, "[97, 98]\n"}, + {"codepoints_block_returns_self", `p "ab".codepoints { |c| }`, "\"ab\"\n"}, + {"codepoints_block_yields", `a=[]; "abcd".codepoints { |c| a << c }; p a`, "[97, 98, 99, 100]\n"}, + {"bytes_no_block", `p "ab".bytes`, "[97, 98]\n"}, + }) +} + +func TestStringSplitResiduals(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"awk_trailing_positive_limit", `p " 1 2 ".split(" ", 3)`, "[\"1\", \"2\", \"\"]\n"}, + {"awk_trailing_neg_limit", `p " a b ".split(" ", -1)`, "[\"a\", \"b\", \"\"]\n"}, + {"awk_no_trailing_default", `p " a b ".split(" ")`, "[\"a\", \"b\"]\n"}, + {"awk_limit_remainder", `p " 1 2 ".split(" ", 2)`, "[\"1\", \"2 \"]\n"}, + {"dollar_semi_string", `$; = ","; r = "x,y,z".split(nil); $; = nil; p r`, "[\"x\", \"y\", \"z\"]\n"}, + {"dollar_semi_regexp", `$; = /:/; r = "1:2:".split(nil, -1); $; = nil; p r`, "[\"1\", \"2\", \"\"]\n"}, + {"dollar_semi_nil_whitespace", `$; = nil; p "a b c".split`, "[\"a\", \"b\", \"c\"]\n"}, + }) +} + +func TestStringMiscResiduals(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"replace_to_str", `class RP; def to_str; "hi"; end; end; p "x".replace(RP.new)`, "\"hi\"\n"}, + {"next_is_succ", `p "az".next`, "\"ba\"\n"}, + {"next_bang", `s = "az"; s.next!; p s`, "\"ba\"\n"}, + {"each_line_size_nil", `p "a\nb\nc".each_line.size`, "nil\n"}, + {"each_line_custom_dollar_slash", `$/ = ":"; r = []; "a:b:c".each_line { |l| r << l }; $/ = nil; p r`, "[\"a:\", \"b:\", \"c\"]\n"}, + }) +} + func TestStringIndexRindexErrorMessages(t *testing.T) { if err := runErr(t, `"abc".index(nil)`); err == nil || !strings.Contains(err.Error(), "no implicit conversion") { t.Fatalf("index(nil): %v", err) From 7e3162e73d28f584a1ec2e82020f3e13c9727184 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 18:01:44 +0200 Subject: [PATCH 4/7] casecmp: raise TypeError when #to_str returns non-String; coverage tests 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 --- internal/vm/builtins.go | 13 +++------- internal/vm/string_method_residuals_test.go | 27 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index d4bfc234..f302596c 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -6360,12 +6360,10 @@ func (vm *VM) casecmpOther(self, other object.Value) (string, bool) { os, ok := other.(*object.String) if !ok { if !vm.respondsToDynamic(other, "to_str") { - return "", false - } - r := vm.send(other, "to_str", nil, nil) - if os, ok = r.(*object.String); !ok { - return "", false + return "", false // no String conversion at all → nil } + // A #to_str that returns a non-String raises TypeError (strCoerceArg), as MRI. + _, os = vm.strCoerceArg(other) } if object.IsNil(vm.encodingCompatible(self.(*object.String), os)) { return "", false @@ -6436,11 +6434,8 @@ func (vm *VM) strSearchArg(self, pat object.Value) (needle string, re *Regexp, i } // runeMatchAt reports whether needle occurs in runes starting exactly at rune -// index p. +// index p. The caller guarantees 0 <= p and p+len(needle) <= len(runes). func runeMatchAt(runes, needle []rune, p int) bool { - if p < 0 || p+len(needle) > len(runes) { - return false - } for i, r := range needle { if runes[p+i] != r { return false diff --git a/internal/vm/string_method_residuals_test.go b/internal/vm/string_method_residuals_test.go index 8ef9d45f..18ba95b1 100644 --- a/internal/vm/string_method_residuals_test.go +++ b/internal/vm/string_method_residuals_test.go @@ -105,6 +105,33 @@ func TestStringCoerceArgNonStringToStr(t *testing.T) { if err == nil || !strings.Contains(err.Error(), "can't convert B to String (B#to_str gives Integer)") { t.Fatalf("chomp(bad to_str): %v", err) } + // casecmp with a #to_str that returns a non-String also raises TypeError. + err = runErr(t, `class B2; def to_str; 42; end; end; "abc".casecmp(B2.new)`) + if err == nil || !strings.Contains(err.Error(), "can't convert B2 to String") { + t.Fatalf("casecmp(bad to_str): %v", err) + } +} + +func TestStringSearchIncompatibleEncoding(t *testing.T) { + // index/rindex negotiate the needle's encoding and raise on incompatibility. + for _, m := range []string{"index", "rindex"} { + err := runErr(t, `"あ".`+m+`("れ".encode("EUC-JP"))`) + if err == nil || !strings.Contains(err.Error(), "CompatibilityError") { + t.Fatalf("%s incompatible encoding: %v", m, err) + } + } +} + +func TestStringPartitionSubclassSeparator(t *testing.T) { + // A String-subclass separator is unwrapped (regexpSep) and matched literally. + if got := eval(t, `class SubSep < String; end; p "hello".partition(SubSep.new("l"))`); got != "[\"he\", \"l\", \"lo\"]\n" { + t.Fatalf("partition subclass sep: %q", got) + } + // split with no positional argument falling back to $; exercises the empty-args + // substitution path. + if got := eval(t, `$; = ","; r = "a,b".split; $; = nil; p r`); got != "[\"a\", \"b\"]\n" { + t.Fatalf("split no-arg $;: %q", got) + } } func TestStringChompResiduals(t *testing.T) { From d2b58def3c10d189407d3ec92c3a8921fea62ec1 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 18:06:11 +0200 Subject: [PATCH 5/7] String residuals: complete branch coverage for new helpers 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 --- internal/vm/builtins.go | 11 ++++------- internal/vm/string_method_residuals_test.go | 12 ++++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/internal/vm/builtins.go b/internal/vm/builtins.go index f302596c..3d5aa417 100644 --- a/internal/vm/builtins.go +++ b/internal/vm/builtins.go @@ -5721,15 +5721,12 @@ func reverseStr(s string) string { // - "" (empty): paragraph mode — remove ALL trailing newlines (\r\n / \n). // - any other string (converted with #to_str): remove that exact suffix once. func (vm *VM) chompSep(s string, args []object.Value) string { - var sep string + sep := "\n" if len(args) == 0 { - rs := vm.gvar("$/") - if object.IsNil(rs) { // $/ = nil (or unset) means the default record separator - sep = "\n" - } else if rss, ok := rs.(*object.String); ok { + // No argument → use $/ (the record separator); a non-String $/ (nil default + // included) means the default "\n". + if rss, ok := vm.gvar("$/").(*object.String); ok { sep = rss.Str() - } else { - sep = "\n" } } else { if object.IsNil(args[0]) { // chomp(nil): do not chomp at all diff --git a/internal/vm/string_method_residuals_test.go b/internal/vm/string_method_residuals_test.go index 18ba95b1..1c7081be 100644 --- a/internal/vm/string_method_residuals_test.go +++ b/internal/vm/string_method_residuals_test.go @@ -53,6 +53,7 @@ func TestStringRindexResiduals(t *testing.T) { {"nil_offset_typeerror", `begin; "str".rindex("st", nil); rescue TypeError; puts "te"; end`, "te\n"}, {"integer_typeerror", `begin; "hello".rindex(42); rescue TypeError; puts "te"; end`, "te\n"}, {"string_form_keeps_match", `$~ = nil; "hello".rindex("ll"); p $~`, "nil\n"}, + {"regexp_no_match_nil", `p "hello".rindex(/xyz/)`, "nil\n"}, }) } @@ -66,9 +67,19 @@ func TestStringSubGsubResiduals(t *testing.T) { {"sub_to_str_pattern", `class P; def to_str; "b"; end; end; p "abc".sub(P.new, "X")`, "\"aXc\"\n"}, {"gsub_hash", `p "hello".gsub(/[el]/, "e" => "3", "l" => "1")`, "\"h311o\"\n"}, {"gsub_enum_size_nil", `p "hello".gsub(/l/).size`, "nil\n"}, + {"gsub_block_only", `p "hello".gsub(/l/) { |m| m.upcase }`, "\"heLLo\"\n"}, + {"gsub_string_pattern", `p "hello".gsub("l", "L")`, "\"heLLo\"\n"}, + {"gsub_subclass_pattern", `class SubP < String; end; p "hello".gsub(SubP.new("l"), "L")`, "\"heLLo\"\n"}, }) } +func TestStringSubArityError(t *testing.T) { + // sub with a pattern but no replacement and no block raises ArgumentError. + if err := runErr(t, `"hello".sub(/l/)`); err == nil || !strings.Contains(err.Error(), "ArgumentError") { + t.Fatalf("sub arity: %v", err) + } +} + func TestStringScanResiduals(t *testing.T) { evalCases(t, []struct{ name, src, want string }{ {"basic", `p "cruel world".scan(/\w+/)`, "[\"cruel\", \"world\"]\n"}, @@ -156,6 +167,7 @@ func TestStringPartitionResiduals(t *testing.T) { {"rpartition_string", `p "hello".rpartition("l")`, "[\"hel\", \"l\", \"o\"]\n"}, {"partition_no_match", `p "hello".partition("x")`, "[\"hello\", \"\", \"\"]\n"}, {"rpartition_no_match", `p "hello".rpartition("x")`, "[\"\", \"\", \"hello\"]\n"}, + {"rpartition_regexp_no_match", `p "hello".rpartition(/xyz/)`, "[\"\", \"\", \"hello\"]\n"}, {"partition_to_str", `class LP; def to_str; "l"; end; end; p "hello".partition(LP.new)`, "[\"he\", \"l\", \"lo\"]\n"}, {"partition_typeerror", `begin; "hello".partition(5); rescue TypeError; puts "te"; end`, "te\n"}, }) From 93a40679bd3131e6f994369a1eecdf7e2ba6f9f5 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 18:53:56 +0200 Subject: [PATCH 6/7] String: cover strSubBang (sub!/gsub!) branches 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 --- internal/vm/string_method_residuals_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/internal/vm/string_method_residuals_test.go b/internal/vm/string_method_residuals_test.go index 1c7081be..93c16284 100644 --- a/internal/vm/string_method_residuals_test.go +++ b/internal/vm/string_method_residuals_test.go @@ -78,6 +78,19 @@ func TestStringSubArityError(t *testing.T) { if err := runErr(t, `"hello".sub(/l/)`); err == nil || !strings.Contains(err.Error(), "ArgumentError") { t.Fatalf("sub arity: %v", err) } + // sub! with a pattern but no replacement and no block raises ArgumentError. + if err := runErr(t, `"hello".sub!(/l/)`); err == nil || !strings.Contains(err.Error(), "ArgumentError") { + t.Fatalf("sub! arity: %v", err) + } +} + +func TestStringSubBangResiduals(t *testing.T) { + evalCases(t, []struct{ name, src, want string }{ + {"gsub_bang_changes", `s = "hello"; s.gsub!(/l/, "L"); p s`, "\"heLLo\"\n"}, + {"gsub_bang_no_change_nil", `p "hello".gsub!(/z/, "Z")`, "nil\n"}, + {"gsub_bang_enum_size_nil", `p "abc".gsub!(/a/).size`, "nil\n"}, + {"gsub_bang_enum_materialize", `s = "aaa"; e = s.gsub!(/a/); e.each { |m| }; p s`, "\"\"\n"}, + }) } func TestStringScanResiduals(t *testing.T) { From 954ca26bbd46218dfa0fc8cbd878a9389646bd06 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 18:59:00 +0200 Subject: [PATCH 7/7] String: cover tr!/tr_s!/delete! #to_str coercion 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 --- internal/vm/string_method_residuals_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/vm/string_method_residuals_test.go b/internal/vm/string_method_residuals_test.go index 93c16284..40e01f9f 100644 --- a/internal/vm/string_method_residuals_test.go +++ b/internal/vm/string_method_residuals_test.go @@ -200,6 +200,9 @@ func TestStringCharsetResiduals(t *testing.T) { {"tr_to_str_args", `class TA; def to_str; "e"; end; end; class TB; def to_str; "i"; end; end; p "hello".tr(TA.new, TB.new)`, "\"hillo\"\n"}, {"tr_reversed_range_raises", `begin; "x".tr("z-a", "y"); rescue ArgumentError; puts "ae"; end`, "ae\n"}, {"count_to_str_arg", `class CA; def to_str; "l"; end; end; p "hello".count(CA.new)`, "2\n"}, + {"tr_bang_to_str", `class T1; def to_str; "el"; end; end; class T2; def to_str; "ip"; end; end; s = "hello"; s.tr!(T1.new, T2.new); p s`, "\"hippo\"\n"}, + {"tr_s_bang_to_str", `s = "aabbcc"; s.tr_s!("a-c", "*"); p s`, "\"*\"\n"}, + {"delete_bang_to_str", `class D1; def to_str; "l"; end; end; s = "hello"; s.delete!(D1.new); p s`, "\"heo\"\n"}, }) }