Skip to content

Commit

Permalink
Fix remaining strscan MRI failures.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Apr 3, 2015
1 parent e65c9b4 commit 2d6b3f6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/org/jruby/RubyMatchData.java
Expand Up @@ -399,6 +399,9 @@ private int nameToBackrefNumber(RubyString str) {
}

private static int nameToBackrefNumber(Ruby runtime, Regex pattern, Region regs, RubyString str) {
if (pattern == null) {
throw runtime.newIndexError("undefined group name reference: " + str);
}
ByteList value = str.getByteList();
try {
return pattern.nameToBackrefNumber(value.getUnsafeBytes(), value.getBegin(), value.getBegin() + value.getRealSize(), regs);
Expand Down
35 changes: 20 additions & 15 deletions core/src/main/java/org/jruby/ext/strscan/RubyStringScanner.java
Expand Up @@ -46,6 +46,7 @@
import org.jruby.common.IRubyWarnings.ID;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.Block;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
Expand Down Expand Up @@ -184,7 +185,7 @@ public RubyString string() {

@JRubyMethod(name = "string=", required = 1)
public IRubyObject set_string(ThreadContext context, IRubyObject str) {
this.str = (RubyString) str.convertToString().strDup(context.runtime).freeze(context);
this.str = RubyString.stringValue(str);
pos = 0;
clearMatched();
return str;
Expand Down Expand Up @@ -214,6 +215,13 @@ public IRubyObject set_pos(IRubyObject pos) {
return RubyFixnum.newFixnum(getRuntime(), i);
}

@JRubyMethod(name = "charpos")
public IRubyObject charpos(ThreadContext context) {
Ruby runtime = context.runtime;
RubyString sub = (RubyString)Helpers.invoke(context, str, "byteslice", runtime.newFixnum(0), runtime.newFixnum(pos));
return runtime.newFixnum(sub.strLength());
}

private IRubyObject extractRange(Ruby runtime, int beg, int end) {
int size = str.getByteList().getRealSize();
if (beg > size) return getRuntime().getNil();
Expand Down Expand Up @@ -492,25 +500,22 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject idx) {
return context.nil;
}

int i;

if (regs == null) {
i = RubyNumeric.fix2int(idx);
if (i != 0) return context.nil;
if (beg == -1) return context.nil;
return extractRange(runtime, lastPos + beg, lastPos + end);
}
int i = RubyMatchData.backrefNumber(runtime, pattern, regs, idx);
int numRegs = regs == null ? 1 : regs.numRegs;

i = RubyMatchData.backrefNumber(runtime, pattern, regs, idx);

int numRegs = regs.numRegs;
if (i < 0) i += numRegs;
if (i < 0 || i >= numRegs) {
return context.nil;
}

if (regs.beg[i] == -1) return getRuntime().getNil();
return extractRange(runtime, lastPos + regs.beg[i], lastPos + regs.end[i]);

if (regs == null) {
assert i == 0;
if (beg == -1) return context.nil;
return extractRange(runtime, lastPos + beg, lastPos + end);
} else {
if (regs.beg[i] == -1) return getRuntime().getNil();
return extractRange(context.runtime, lastPos + regs.beg[i], lastPos + regs.end[i]);
}
}

@JRubyMethod(name = "pre_match")
Expand Down
4 changes: 0 additions & 4 deletions test/mri/excludes/TestStringScanner.rb

This file was deleted.

0 comments on commit 2d6b3f6

Please sign in to comment.