Skip to content

Commit c0ef875

Browse files
committedMar 19, 2018
Add StringScanner#size, captures, and values_at. #4876
1 parent 94af48f commit c0ef875

File tree

1 file changed

+45
-11
lines changed

1 file changed

+45
-11
lines changed
 

Diff for: ‎core/src/main/java/org/jruby/ext/strscan/RubyStringScanner.java

+45-11
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,7 @@
3131
import org.joni.Option;
3232
import org.joni.Regex;
3333
import org.joni.Region;
34-
import org.jruby.Ruby;
35-
import org.jruby.RubyBoolean;
36-
import org.jruby.RubyClass;
37-
import org.jruby.RubyException;
38-
import org.jruby.RubyFixnum;
39-
import org.jruby.RubyMatchData;
40-
import org.jruby.RubyNumeric;
41-
import org.jruby.RubyObject;
42-
import org.jruby.RubyRegexp;
43-
import org.jruby.RubyString;
44-
import org.jruby.RubyThread;
34+
import org.jruby.*;
4535
import org.jruby.anno.JRubyClass;
4636
import org.jruby.anno.JRubyMethod;
4737
import org.jruby.common.IRubyWarnings.ID;
@@ -626,4 +616,48 @@ private IRubyObject inspect2() {
626616
public static IRubyObject mustCversion(IRubyObject recv) {
627617
return recv;
628618
}
619+
620+
@JRubyMethod(name = "size")
621+
public IRubyObject size(ThreadContext context) {
622+
if (!isMatched()) return context.nil;
623+
return context.runtime.newFixnum(regs.numRegs);
624+
}
625+
626+
@JRubyMethod(name = "captures")
627+
public IRubyObject captures(ThreadContext context) {
628+
int i, numRegs;
629+
RubyArray newAry;
630+
631+
if (!isMatched()) return context.nil;
632+
633+
Ruby runtime = context.runtime;
634+
635+
numRegs = regs.numRegs;
636+
newAry = RubyArray.newArray(runtime, numRegs);
637+
638+
for (i = 1; i < numRegs; i++) {
639+
IRubyObject str = extractRange(runtime, lastPos + regs.beg[i],
640+
lastPos + regs.end[i]);
641+
newAry.push(str);
642+
}
643+
644+
return newAry;
645+
}
646+
647+
@JRubyMethod(name = "values_at", rest = true)
648+
public IRubyObject values_at(ThreadContext context, IRubyObject[] args) {
649+
int i;
650+
RubyArray newAry;
651+
652+
if (!isMatched()) return context.nil;
653+
654+
Ruby runtime = context.runtime;
655+
656+
newAry = RubyArray.newArray(runtime, args.length);
657+
for (i = 0; i < args.length; i++) {
658+
newAry.push(op_aref(context, args[i]));
659+
}
660+
661+
return newAry;
662+
}
629663
}

0 commit comments

Comments
 (0)
Please sign in to comment.