Skip to content

Instantly share code, notes, and snippets.

@lopex
Created July 25, 2018 19:42
Show Gist options
  • Save lopex/ca85859f002ca8ffbd7cf0966a368d6b to your computer and use it in GitHub Desktop.
Save lopex/ca85859f002ca8ffbd7cf0966a368d6b to your computer and use it in GitHub Desktop.
diff --git a/core/src/main/java/org/jruby/RubyRegexp.java b/core/src/main/java/org/jruby/RubyRegexp.java
index a86ab9da95..42af190df0 100755
--- a/core/src/main/java/org/jruby/RubyRegexp.java
+++ b/core/src/main/java/org/jruby/RubyRegexp.java
@@ -1136,14 +1136,12 @@ public class RubyRegexp extends RubyObject implements ReOptions, EncodingCapable
@JRubyMethod(name = "match?")
public IRubyObject match_p(ThreadContext context, IRubyObject str) {
- IRubyObject[] dummy = new IRubyObject[1];
- return context.runtime.newBoolean(matchPos(context, str, null, dummy, 0) >= 0);
+ return matchP(context, str, 0);
}
@JRubyMethod(name = "match?")
public IRubyObject match_p(ThreadContext context, IRubyObject str, IRubyObject pos) {
- IRubyObject[] dummy = new IRubyObject[1];
- return context.runtime.newBoolean(matchPos(context, str, null, dummy, RubyNumeric.num2int(pos)) >= 0);
+ return matchP(context, str, RubyNumeric.num2int(pos));
}
private IRubyObject matchCommon(ThreadContext context, IRubyObject str, int pos, boolean setBackref, Block block) {
@@ -1183,6 +1181,53 @@ public class RubyRegexp extends RubyObject implements ReOptions, EncodingCapable
return search(context, str, pos, false, holder);
}
+ private RubyBoolean matchP(ThreadContext context, IRubyObject arg, int pos) {
+ if (arg == context.nil) return context.fals;
+ RubyString str = arg instanceof RubySymbol ? ((RubySymbol) arg).to_s(context.runtime) : arg.convertToString();
+
+ if (pos != 0) {
+ if (pos < 0) {
+ pos += str.strLength();
+ if (pos < 0) return context.fals;
+ }
+ pos = str.rbStrOffset(pos);
+ }
+
+ final Regex reg = preparePattern(str);
+ final boolean tmpreg = reg != this.pattern;
+ if (!tmpreg) this.useCount++;
+
+ final ByteList strBL = str.getByteList();
+ final int beg = strBL.begin();
+
+ Matcher matcher = reg.matcherNoRegion(strBL.unsafeBytes(), beg, beg + strBL.realSize());
+
+ int result = -1;
+ JOniException exception = null;
+ try {
+ result = matcherSearch(context, matcher, beg + pos, beg + strBL.realSize(), RE_OPTION_NONE);
+ } catch (JOniException je) {
+ exception = je;
+ }
+
+ if (tmpreg) {
+ if (this.useCount > 0) {
+ } else {
+ this.pattern = reg;
+ }
+ } else {
+ this.useCount--;
+ }
+
+ if (result < 0) {
+ if (result == -1) {
+ return context.fals;
+ }
+ throw context.runtime.newRegexpError(exception == null ? "FIXME: missing message" : exception.getMessage());
+ }
+ return context.tru;
+ }
+
/**
* MRI: rb_reg_search
*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment