diff --git a/internal/vm/regexp.go b/internal/vm/regexp.go index 1554c08b..7349787e 100644 --- a/internal/vm/regexp.go +++ b/internal/vm/regexp.go @@ -1308,6 +1308,21 @@ func (vm *VM) installRegexp() { idx := int64(m.indexForKey(args[0])) return object.NewArrayFromSlice([]object.Value{m.offset(idx, false), m.offset(idx, true)}) }) + // bytebegin / byteend / byteoffset are the byte-index counterparts of begin / + // end / offset (Ruby 3.2+): they skip the byte-to-character conversion. + vm.cMatchData.define("bytebegin", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + m := mdArg(self) + return m.byteOffset(int64(m.indexForKey(args[0])), false) + }) + vm.cMatchData.define("byteend", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + m := mdArg(self) + return m.byteOffset(int64(m.indexForKey(args[0])), true) + }) + vm.cMatchData.define("byteoffset", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + m := mdArg(self) + idx := int64(m.indexForKey(args[0])) + return object.NewArrayFromSlice([]object.Value{m.byteOffset(idx, false), m.byteOffset(idx, true)}) + }) // MatchData#named_captures maps each capture name to its captured substring // (nil when the group did not participate); symbolize_names: true keys by // Symbol instead of String. @@ -1485,6 +1500,22 @@ func (m *MatchData) offset(i int64, end bool) object.Value { return object.IntValue(int64(byteToChar(m.subject, b+m.byteOff))) } +// byteOffset is offset without the byte-to-character conversion: it returns the +// raw byte index of the group's start (or end), or nil for a group that did not +// participate. It backs #bytebegin / #byteend / #byteoffset. +func (m *MatchData) byteOffset(i int64, end bool) object.Value { + var b int + if end { + b = m.md.End(int(i)) + } else { + b = m.md.Begin(int(i)) + } + if b < 0 { + return object.NilV + } + return object.IntValue(int64(b + m.byteOff)) +} + // at implements MatchData#[]: an Integer selects a group by index; a String or // Symbol selects a named group (raising IndexError for an unknown name). func (m *MatchData) at(key object.Value) object.Value { diff --git a/internal/vm/regexp_matchdata_test.go b/internal/vm/regexp_matchdata_test.go index 1c625e79..afe350ff 100644 --- a/internal/vm/regexp_matchdata_test.go +++ b/internal/vm/regexp_matchdata_test.go @@ -287,3 +287,25 @@ func TestMatchGlobals(t *testing.T) { {`"ab".match(/(a)(b)/); p $~.class`, "MatchData\n"}, }) } + +// TestMatchDataByteOffsets covers MatchData#bytebegin / #byteend / #byteoffset — +// the byte-index counterparts of begin / end / offset, including a multibyte +// subject, a named group, a non-participating group and the invalid-key +// IndexError. Asserted against MRI Ruby 4.0.6. +func TestMatchDataByteOffsets(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`m = "héllo wörld".match(/(l+)o (w)/); p [m.bytebegin(0), m.byteend(0), m.byteoffset(0)]`, "[3, 8, [3, 8]]\n"}, + {`m = "héllo wörld".match(/(l+)o (w)/); p [m.bytebegin(1), m.byteoffset(2)]`, "[3, [7, 8]]\n"}, + {`m = "héllo".match(/l/); p [m.begin(0), m.bytebegin(0)]`, "[2, 3]\n"}, + {`m = "abc".match(/(?b)/); p [m.bytebegin(:g), m.byteoffset(:g)]`, "[1, [1, 2]]\n"}, + {`mm = "abc".match(/(x)?b/); p [mm.bytebegin(1), mm.byteoffset(1)]`, "[nil, [nil, nil]]\n"}, + }) + for _, src := range []string{ + `"abc".match(/b/).byteoffset(:nope)`, + `"abc".match(/b/).bytebegin(5)`, + } { + if err := runErr(t, src); err == nil { + t.Errorf("src=%q: expected IndexError, got nil", src) + } + } +} diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 128cbc3a..da34743d 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -15850 +15870