Skip to content

Commit 51004c3

Browse files
byroothsbt
authored andcommitted
[ruby/strscan] Fix a bug that scan_integer doesn't update matched
data (ruby/strscan#133) Fix ruby/strscan#130 Reported by Andrii Konchyn. Thanks!!! ruby/strscan@4e5f17f87a
1 parent c1f024f commit 51004c3

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

ext/strscan/strscan.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,10 @@ strscan_parse_integer(struct strscanner *p, int base, long len)
12921292
integer = rb_cstr2inum(buffer, base);
12931293
RB_ALLOCV_END(buffer_v);
12941294
p->curr += len;
1295+
1296+
MATCHED(p);
1297+
adjust_registers_to_matched(p);
1298+
12951299
return integer;
12961300
}
12971301

@@ -1341,7 +1345,6 @@ strscan_scan_base10_integer(VALUE self)
13411345
return Qnil;
13421346
}
13431347

1344-
MATCHED(p);
13451348
p->prev = p->curr;
13461349

13471350
while (len < remaining_len && rb_isdigit(ptr[len])) {
@@ -1383,7 +1386,6 @@ strscan_scan_base16_integer(VALUE self)
13831386
return Qnil;
13841387
}
13851388

1386-
MATCHED(p);
13871389
p->prev = p->curr;
13881390

13891391
while (len < remaining_len && rb_isxdigit(ptr[len])) {

test/strscan/test_stringscanner.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,18 @@ def test_scan_integer_encoding
946946
end
947947
end
948948

949+
def test_scan_integer_matched
950+
omit("not implemented on TruffleRuby") if RUBY_ENGINE == "truffleruby"
951+
952+
s = create_string_scanner("42abc")
953+
assert_equal(42, s.scan_integer)
954+
assert_equal("42", s.matched)
955+
956+
s = create_string_scanner("42abc")
957+
assert_equal(0x42abc, s.scan_integer(base: 16))
958+
assert_equal("42abc", s.matched)
959+
end
960+
949961
def test_scan_integer_base_16
950962
omit("scan_integer isn't implemented on TruffleRuby yet") if RUBY_ENGINE == "truffleruby"
951963

0 commit comments

Comments
 (0)