Skip to content

Commit

Permalink
Add StringScanner#size, captures, and values_at. #4876
Browse files Browse the repository at this point in the history
headius committed Mar 19, 2018

Verified

This commit was signed with the committer’s verified signature.
headius Charles Oliver Nutter
1 parent 94af48f commit c0ef875
Showing 1 changed file with 45 additions and 11 deletions.
56 changes: 45 additions & 11 deletions core/src/main/java/org/jruby/ext/strscan/RubyStringScanner.java
Original file line number Diff line number Diff line change
@@ -31,17 +31,7 @@
import org.joni.Option;
import org.joni.Regex;
import org.joni.Region;
import org.jruby.Ruby;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyException;
import org.jruby.RubyFixnum;
import org.jruby.RubyMatchData;
import org.jruby.RubyNumeric;
import org.jruby.RubyObject;
import org.jruby.RubyRegexp;
import org.jruby.RubyString;
import org.jruby.RubyThread;
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.common.IRubyWarnings.ID;
@@ -626,4 +616,48 @@ private IRubyObject inspect2() {
public static IRubyObject mustCversion(IRubyObject recv) {
return recv;
}

@JRubyMethod(name = "size")
public IRubyObject size(ThreadContext context) {
if (!isMatched()) return context.nil;
return context.runtime.newFixnum(regs.numRegs);
}

@JRubyMethod(name = "captures")
public IRubyObject captures(ThreadContext context) {
int i, numRegs;
RubyArray newAry;

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

Ruby runtime = context.runtime;

numRegs = regs.numRegs;
newAry = RubyArray.newArray(runtime, numRegs);

for (i = 1; i < numRegs; i++) {
IRubyObject str = extractRange(runtime, lastPos + regs.beg[i],
lastPos + regs.end[i]);
newAry.push(str);
}

return newAry;
}

@JRubyMethod(name = "values_at", rest = true)
public IRubyObject values_at(ThreadContext context, IRubyObject[] args) {
int i;
RubyArray newAry;

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

Ruby runtime = context.runtime;

newAry = RubyArray.newArray(runtime, args.length);
for (i = 0; i < args.length; i++) {
newAry.push(op_aref(context, args[i]));
}

return newAry;
}
}

0 comments on commit c0ef875

Please sign in to comment.