Skip to content

Commit 535f9b4

Browse files
committed
Return nil for empty integer captures
JRuby must match the native implementation when captures become empty.
1 parent 5a6c2ce commit 535f9b4

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

lib/strscan/strscan.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
class StringScanner
44
unless method_defined?(:integer_at) # For JRuby
55
def integer_at(specifier, *to_i_args)
6-
self[specifier]&.to_i(*to_i_args)
6+
value = self[specifier]
7+
value.to_i(*to_i_args) unless value.nil? || value.empty?
78
end
89
end
910

test/strscan/test_stringscanner.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,13 @@ def test_integer_at_base_auto
578578
assert_integer_at(s, 0, 0) # 0xaf
579579
end
580580

581+
def test_integer_at_empty
582+
s = create_string_scanner("")
583+
assert_equal("", s.scan(/()/))
584+
assert_nil(s.integer_at(0))
585+
assert_nil(s.integer_at(1))
586+
end
587+
581588
def test_integer_at_shrunk
582589
omit("not supported on TruffleRuby") if RUBY_ENGINE == "truffleruby"
583590

0 commit comments

Comments
 (0)