Skip to content

Commit

Permalink
avoid duplicate code (nil back-ref setting) in match19Common helper
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Nov 10, 2015
1 parent e936fb4 commit c7daa04
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
31 changes: 11 additions & 20 deletions core/src/main/java/org/jruby/RubyRegexp.java
Original file line number Diff line number Diff line change
Expand Up @@ -978,12 +978,11 @@ public IRubyObject op_match(ThreadContext context, IRubyObject arg) {
@JRubyMethod(name = "=~", required = 1, writes = BACKREF, reads = BACKREF)
@Override
public IRubyObject op_match19(ThreadContext context, IRubyObject str) {
Ruby runtime = context.runtime;
IRubyObject[] strp = {str};
final IRubyObject[] strp = { str };
int pos = matchPos(context, str, strp, null, 0);
if (pos < 0) return runtime.getNil();
pos = ((RubyString)strp[0]).subLength(pos);
return RubyFixnum.newFixnum(runtime, pos);
if (pos < 0) return context.nil;
pos = ((RubyString) strp[0]).subLength(pos);
return RubyFixnum.newFixnum(context.runtime, pos);
}

/** rb_reg_match_m
Expand All @@ -1007,17 +1006,9 @@ public IRubyObject match_m19(ThreadContext context, IRubyObject str, IRubyObject
return match19Common(context, str, RubyNumeric.num2int(pos), true, block);
}

private IRubyObject match19Common(ThreadContext context, IRubyObject arg, int pos, boolean setBackref, Block block) {
if (arg.isNil()) {
if (setBackref) context.setBackRef(arg);
return arg;
}

RubyString _str = operandCheck(arg);

IRubyObject[] holder = setBackref ? null : new IRubyObject[]{context.nil};
if (matchPos(context, _str, null, holder, pos) < 0) {
setBackRefInternal(context, holder, context.nil);
private IRubyObject match19Common(ThreadContext context, IRubyObject str, int pos, boolean setBackref, Block block) {
IRubyObject[] holder = setBackref ? null : new IRubyObject[] { context.nil };
if (matchPos(context, str, null, holder, pos) < 0) {
return context.nil;
}

Expand All @@ -1030,17 +1021,17 @@ private IRubyObject match19Common(ThreadContext context, IRubyObject arg, int po
* MRI: reg_match_pos
*
* @param context thread context
* @param stringlike the stringlike to match
* @param arg the stringlike to match
* @param strp an out param to hold the coerced string; ignored if null
* @param holder an out param to hold the resulting match object; ignored if null
* @param pos the position from which to start matching
*/
private int matchPos(ThreadContext context, IRubyObject stringlike, IRubyObject[] strp, IRubyObject[] holder, int pos) {
if (stringlike.isNil()) {
private int matchPos(ThreadContext context, IRubyObject arg, IRubyObject[] strp, IRubyObject[] holder, int pos) {
if (arg.isNil()) {
setBackRefInternal(context, holder, context.nil);
return -1;
}
final RubyString str = operandCheck(stringlike);
final RubyString str = operandCheck(arg);
if (strp != null) strp[0] = str;
if (pos != 0) {
if (pos < 0) {
Expand Down
9 changes: 9 additions & 0 deletions test/jruby/test_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ def test_scan_string_pattern_match
$~.inspect # failed with a NPE or might have recycled previous $~ pattern
assert_equal /\-ty\-/, $~.regexp
assert_equal 1, $~.size
assert_equal str, $~.string
assert $~.string.frozen?
end

def test_regexp_match
''.sub!(/foo/, '')
# assert ! $~.nil?
/bar/.match(nil)
assert $~.nil?
end

EOL = "\r\n"
Expand Down

0 comments on commit c7daa04

Please sign in to comment.