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
3 changes: 2 additions & 1 deletion lib/strscan/strscan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
class StringScanner
unless method_defined?(:integer_at) # For JRuby
def integer_at(specifier, *to_i_args)
self[specifier]&.to_i(*to_i_args)
value = self[specifier]
value.to_i(*to_i_args) unless value.nil? || value.empty?
end
end

Expand Down
10 changes: 9 additions & 1 deletion lib/strscan/truffleruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,15 @@ def [](group)
end
end

def integer_at(group, *to_i_args) = self[group]&.to_i(*to_i_args)
def integer_at(group, *to_i_args)
return if self[group].nil?

group += @last_match.size if Primitive.is_a?(group, Integer) && group < 0
from = @last_match.bytebegin(group)
to = [@last_match.byteend(group), @string.bytesize].min
value = @string.byteslice(from, to - from)
value.to_i(*to_i_args) unless value.nil? || value.empty?
end

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

Expand Down
11 changes: 7 additions & 4 deletions test/strscan/test_stringscanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,14 @@ def test_integer_at_base_auto
assert_integer_at(s, 0, 0) # 0xaf
end

def test_integer_at_shrunk
omit("not supported on TruffleRuby") if RUBY_ENGINE == "truffleruby"
def test_integer_at_empty
s = create_string_scanner("")
assert_equal("", s.scan(/()/))
assert_nil(s.integer_at(0))
assert_nil(s.integer_at(1))
end

def test_integer_at_shrunk
s = create_string_scanner(+"before 29 after")
s.skip_until(" ")
assert_equal("29", s.scan(/\d+/))
Expand All @@ -589,8 +594,6 @@ def test_integer_at_shrunk
end

def test_integer_at_shrunk_partial
omit("not supported on TruffleRuby") if RUBY_ENGINE == "truffleruby"

s = create_string_scanner(+"before 29 after")
s.skip_until(" ")
assert_equal("29", s.scan(/\d+/))
Expand Down
Loading