Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions internal/vm/regexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions internal/vm/regexp_matchdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(/(?<g>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)
}
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15850
15870