Skip to content

Commit a4eacdd

Browse files
committedJul 31, 2018
separate implementation for match? for #5256
1 parent 4cc364e commit a4eacdd

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed
 

‎core/src/main/java/org/jruby/RubyRegexp.java

+31-4
Original file line numberDiff line numberDiff line change
@@ -1136,14 +1136,12 @@ public IRubyObject match_m(ThreadContext context, IRubyObject str, IRubyObject p
11361136

11371137
@JRubyMethod(name = "match?")
11381138
public IRubyObject match_p(ThreadContext context, IRubyObject str) {
1139-
IRubyObject[] dummy = new IRubyObject[1];
1140-
return context.runtime.newBoolean(matchPos(context, str, null, dummy, 0) >= 0);
1139+
return matchP(context, str, 0);
11411140
}
11421141

11431142
@JRubyMethod(name = "match?")
11441143
public IRubyObject match_p(ThreadContext context, IRubyObject str, IRubyObject pos) {
1145-
IRubyObject[] dummy = new IRubyObject[1];
1146-
return context.runtime.newBoolean(matchPos(context, str, null, dummy, RubyNumeric.num2int(pos)) >= 0);
1144+
return matchP(context, str, RubyNumeric.num2int(pos));
11471145
}
11481146

11491147
private IRubyObject matchCommon(ThreadContext context, IRubyObject str, int pos, boolean setBackref, Block block) {
@@ -1183,6 +1181,35 @@ private int matchPos(ThreadContext context, IRubyObject arg, RubyString[] strp,
11831181
return search(context, str, pos, false, holder);
11841182
}
11851183

1184+
private RubyBoolean matchP(ThreadContext context, IRubyObject arg, int pos) {
1185+
if (arg == context.nil) return context.fals;
1186+
RubyString str = arg instanceof RubySymbol ? ((RubySymbol) arg).to_s(context.runtime) : arg.convertToString();
1187+
1188+
if (pos != 0) {
1189+
if (pos < 0) {
1190+
pos += str.strLength();
1191+
if (pos < 0) return context.fals;
1192+
}
1193+
pos = str.rbStrOffset(pos);
1194+
}
1195+
1196+
final Regex reg = preparePattern(str);
1197+
1198+
final ByteList strBL = str.getByteList();
1199+
final int beg = strBL.begin();
1200+
1201+
Matcher matcher = reg.matcherNoRegion(strBL.unsafeBytes(), beg, beg + strBL.realSize());
1202+
1203+
int result;
1204+
try {
1205+
result = matcherSearch(context, matcher, beg + pos, beg + strBL.realSize(), RE_OPTION_NONE);
1206+
} catch (JOniException je) {
1207+
throw context.runtime.newRegexpError(je.getMessage());
1208+
}
1209+
1210+
return result == -1 ? context.fals : context.tru;
1211+
}
1212+
11861213
/**
11871214
* MRI: rb_reg_search
11881215
*

0 commit comments

Comments
 (0)