Skip to content

Commit ad278f2

Browse files
committed
Allow parsing strings larger than 4GiB
For a reason unknown, even though `pos` is stored as a `long`, the `#pos` and `#pos=` treat it as an `int`, which prevent skeeing into strings larger than 4GiB.
1 parent baa5de3 commit ad278f2

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

ext/strscan/strscan.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ strscan_get_pos(VALUE self)
524524
struct strscanner *p;
525525

526526
GET_SCANNER(self, p);
527-
return INT2FIX(p->curr);
527+
return LONG2NUM(p->curr);
528528
}
529529

530530
/*
@@ -554,7 +554,7 @@ strscan_set_pos(VALUE self, VALUE v)
554554
long i;
555555

556556
GET_SCANNER(self, p);
557-
i = NUM2INT(v);
557+
i = NUM2LONG(v);
558558
if (i < 0) i += S_LEN(p);
559559
if (i < 0) rb_raise(rb_eRangeError, "index out of range");
560560
if (i > S_LEN(p)) rb_raise(rb_eRangeError, "index out of range");

test/strscan/test_stringscanner.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,17 @@ def test_fixed_anchor_false
11281128
assert_equal(false, StringScanner.new("a", fixed_anchor: nil).fixed_anchor?)
11291129
assert_equal(false, StringScanner.new("a", fixed_anchor: false).fixed_anchor?)
11301130
end
1131+
1132+
def test_large_string
1133+
omit("not supported on JRuby") if RUBY_ENGINE == "jruby"
1134+
1135+
two_gib = (2 * 1024 * 1024 * 1024) + 100
1136+
string = "a" * two_gib
1137+
s = create_string_scanner(string) # 6 GiB string
1138+
s.pos = two_gib - 1
1139+
assert_equal(two_gib - 1, s.pos)
1140+
string.clear
1141+
end
11311142
end
11321143

11331144
class TestStringScannerFixedAnchor < Test::Unit::TestCase

0 commit comments

Comments
 (0)