Skip to content

Commit bf6c106

Browse files
byroothsbt
authored andcommitted
[ruby/strscan] scan_integer(base: 16) ignore x suffix if not
followed by hexadecimal (ruby/strscan#141) Fix: ruby/strscan#140 `0x<EOF>`, `0xZZZ` should be parsed as `0` instead of not matching at all. ruby/strscan@c4e4795ed2
1 parent 0f8a6e1 commit bf6c106

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

ext/strscan/strscan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ strscan_scan_base16_integer(VALUE self)
13791379
len++;
13801380
}
13811381

1382-
if ((remaining_len >= (len + 2)) && ptr[len] == '0' && ptr[len + 1] == 'x') {
1382+
if ((remaining_len >= (len + 3)) && ptr[len] == '0' && ptr[len + 1] == 'x' && rb_isxdigit(ptr[len + 2])) {
13831383
len += 2;
13841384
}
13851385

test/strscan/test_stringscanner.rb

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,19 +1052,24 @@ def test_scan_integer_base_16
10521052
assert_predicate(s, :matched?)
10531053

10541054
s = create_string_scanner('0x')
1055-
assert_nil(s.scan_integer(base: 16))
1056-
assert_equal(0, s.pos)
1057-
refute_predicate(s, :matched?)
1055+
assert_equal(0, s.scan_integer(base: 16))
1056+
assert_equal(1, s.pos)
1057+
assert_predicate(s, :matched?)
1058+
1059+
s = create_string_scanner('0xyz')
1060+
assert_equal(0, s.scan_integer(base: 16))
1061+
assert_equal(1, s.pos)
1062+
assert_predicate(s, :matched?)
10581063

10591064
s = create_string_scanner('-0x')
1060-
assert_nil(s.scan_integer(base: 16))
1061-
assert_equal(0, s.pos)
1062-
refute_predicate(s, :matched?)
1065+
assert_equal(0, s.scan_integer(base: 16))
1066+
assert_equal(2, s.pos)
1067+
assert_predicate(s, :matched?)
10631068

10641069
s = create_string_scanner('+0x')
1065-
assert_nil(s.scan_integer(base: 16))
1066-
assert_equal(0, s.pos)
1067-
refute_predicate(s, :matched?)
1070+
assert_equal(0, s.scan_integer(base: 16))
1071+
assert_equal(2, s.pos)
1072+
assert_predicate(s, :matched?)
10681073

10691074
s = create_string_scanner('-123abc')
10701075
assert_equal(-0x123abc, s.scan_integer(base: 16))

0 commit comments

Comments
 (0)