Skip to content

Commit fedd600

Browse files
committed
fix captures nil case
fix #70
1 parent 8adf8b8 commit fedd600

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ public IRubyObject size(ThreadContext context) {
892892
public IRubyObject captures(ThreadContext context) {
893893
int i, numRegs;
894894
RubyArray newAry;
895+
IRubyObject str;
895896

896897
if (!isMatched()) return context.nil;
897898

@@ -901,9 +902,13 @@ public IRubyObject captures(ThreadContext context) {
901902
newAry = RubyArray.newArray(runtime, numRegs);
902903

903904
for (i = 1; i < numRegs; i++) {
904-
IRubyObject str = extractRange(runtime,
905+
if (REGION_ADAPTER.getBeg(regs, i) == -1) {
906+
str = context.nil;
907+
} else {
908+
str = extractRange(runtime,
905909
adjustRegisterPosition(REGION_ADAPTER.getBeg(regs, i)),
906910
adjustRegisterPosition(REGION_ADAPTER.getEnd(regs, i)));
911+
}
907912
newAry.push(str);
908913
}
909914

ext/strscan/strscan.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,17 +1243,18 @@ strscan_size(VALUE self)
12431243
* If nothing was priorly matched, it returns nil.
12441244
*
12451245
* s = StringScanner.new("Fri Dec 12 1975 14:39")
1246-
* s.scan(/(\w+) (\w+) (\d+) /) # -> "Fri Dec 12 "
1247-
* s.captures # -> ["Fri", "Dec", "12"]
1248-
* s.scan(/(\w+) (\w+) (\d+) /) # -> nil
1249-
* s.captures # -> nil
1246+
* s.scan(/(\w+) (\w+) (\d+) (1980)?/) # -> "Fri Dec 12 "
1247+
* s.captures # -> ["Fri", "Dec", "12", nil]
1248+
* s.scan(/(\w+) (\w+) (\d+) (1980)?/) # -> nil
1249+
* s.captures # -> nil
12501250
*/
12511251
static VALUE
12521252
strscan_captures(VALUE self)
12531253
{
12541254
struct strscanner *p;
12551255
int i, num_regs;
12561256
VALUE new_ary;
1257+
VALUE str;
12571258

12581259
GET_SCANNER(self, p);
12591260
if (! MATCHED_P(p)) return Qnil;
@@ -1262,9 +1263,12 @@ strscan_captures(VALUE self)
12621263
new_ary = rb_ary_new2(num_regs);
12631264

12641265
for (i = 1; i < num_regs; i++) {
1265-
VALUE str = extract_range(p,
1266-
adjust_register_position(p, p->regs.beg[i]),
1267-
adjust_register_position(p, p->regs.end[i]));
1266+
if (p->regs.beg[i] == -1)
1267+
str = Qnil;
1268+
else
1269+
str = extract_range(p,
1270+
adjust_register_position(p, p->regs.beg[i]),
1271+
adjust_register_position(p, p->regs.end[i]));
12681272
rb_ary_push(new_ary, str);
12691273
}
12701274

test/strscan/test_stringscanner.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,8 @@ def test_size
737737
def test_captures
738738
s = create_string_scanner("Timestamp: Fri Dec 12 1975 14:39")
739739
s.scan("Timestamp: ")
740-
s.scan(/(\w+) (\w+) (\d+) /)
741-
assert_equal(["Fri", "Dec", "12"], s.captures)
740+
s.scan(/(\w+) (\w+) (\d+) (1980)?/)
741+
assert_equal(["Fri", "Dec", "12", nil], s.captures)
742742
s.scan(/(\w+) (\w+) (\d+) /)
743743
assert_nil(s.captures)
744744
end

0 commit comments

Comments
 (0)