Skip to content

Commit 435095d

Browse files
committed
Handle shrunk captures on TruffleRuby
`integer_at` must read captures from the scanner current string.
1 parent 535f9b4 commit 435095d

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

lib/strscan/truffleruby.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,15 @@ def [](group)
133133
end
134134
end
135135

136-
def integer_at(group, *to_i_args) = self[group]&.to_i(*to_i_args)
136+
def integer_at(group, *to_i_args)
137+
return if self[group].nil?
138+
139+
group += @last_match.size if Primitive.is_a?(group, Integer) && group < 0
140+
from = @last_match.bytebegin(group)
141+
to = [@last_match.byteend(group), @string.bytesize].min
142+
value = @string.byteslice(from, to - from)
143+
value.to_i(*to_i_args) unless value.nil? || value.empty?
144+
end
137145

138146
def values_at(*groups) = @last_match&.values_at(*groups)
139147

test/strscan/test_stringscanner.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,6 @@ def test_integer_at_empty
586586
end
587587

588588
def test_integer_at_shrunk
589-
omit("not supported on TruffleRuby") if RUBY_ENGINE == "truffleruby"
590-
591589
s = create_string_scanner(+"before 29 after")
592590
s.skip_until(" ")
593591
assert_equal("29", s.scan(/\d+/))
@@ -596,8 +594,6 @@ def test_integer_at_shrunk
596594
end
597595

598596
def test_integer_at_shrunk_partial
599-
omit("not supported on TruffleRuby") if RUBY_ENGINE == "truffleruby"
600-
601597
s = create_string_scanner(+"before 29 after")
602598
s.skip_until(" ")
603599
assert_equal("29", s.scan(/\d+/))

0 commit comments

Comments
 (0)