|
5 | 5 | import java.util.Map;
|
6 | 6 | import java.util.Objects;
|
7 | 7 | import java.util.function.Predicate;
|
| 8 | +import org.jruby.Ruby; |
| 9 | +import org.jruby.RubyRegexp; |
8 | 10 | import org.jruby.RubyString;
|
9 | 11 | import org.jruby.runtime.builtin.IRubyObject;
|
10 | 12 | import org.jruby.util.ByteList;
|
@@ -466,34 +468,40 @@ private static boolean valueIsTruthy(Object object) {
|
466 | 468 | !Boolean.toString(false).equals(other);
|
467 | 469 | }
|
468 | 470 |
|
| 471 | + private static RubyRegexp newRegexp(String pattern) { |
| 472 | + final Ruby runtime = RubyUtil.RUBY; |
| 473 | + return RubyRegexp.newRegexpFromStr(runtime, runtime.newString(pattern), 0); |
| 474 | + } |
| 475 | + |
| 476 | + private static boolean matches(RubyString str, RubyRegexp regexp) { |
| 477 | + return regexp.match_p(RubyUtil.RUBY.getCurrentContext(), str).isTrue(); // match? returns true/false |
| 478 | + } |
| 479 | + |
469 | 480 | private static final class FieldMatches implements EventCondition {
|
470 | 481 |
|
471 | 482 | private final FieldReference field;
|
472 | 483 |
|
473 |
| - private final RubyString regex; |
| 484 | + private final RubyRegexp regexp; |
474 | 485 |
|
475 |
| - private FieldMatches(final String field, final String regex) { |
| 486 | + private FieldMatches(final String field, final String pattern) { |
476 | 487 | this.field = FieldReference.from(field);
|
477 |
| - this.regex = RubyUtil.RUBY.newString(regex); |
| 488 | + this.regexp = newRegexp(pattern); |
478 | 489 | }
|
479 | 490 |
|
480 | 491 | @Override
|
481 | 492 | public boolean fulfilled(final JrubyEventExtLibrary.RubyEvent event) {
|
482 |
| - final Object tomatch = event.getEvent().getUnconvertedField(field); |
483 |
| - return tomatch instanceof RubyString && |
484 |
| - !((RubyString) tomatch).match(WorkerLoop.THREAD_CONTEXT.get(), regex).isNil(); |
| 493 | + final Object toMatch = event.getEvent().getUnconvertedField(field); |
| 494 | + return toMatch instanceof RubyString && matches((RubyString) toMatch, regexp); |
485 | 495 | }
|
486 | 496 | }
|
487 | 497 |
|
488 | 498 | private static final class ConstantMatches implements EventCondition {
|
489 | 499 |
|
490 | 500 | private final boolean matches;
|
491 | 501 |
|
492 |
| - private ConstantMatches(final Object constant, final String regex) { |
| 502 | + private ConstantMatches(final Object constant, final String pattern) { |
493 | 503 | this.matches = constant instanceof String &&
|
494 |
| - !(RubyUtil.RUBY.newString((String) constant).match( |
495 |
| - WorkerLoop.THREAD_CONTEXT.get(), |
496 |
| - RubyUtil.RUBY.newString(regex)).isNil()); |
| 504 | + matches(RubyUtil.RUBY.newString((String) constant), newRegexp(pattern)); |
497 | 505 | }
|
498 | 506 |
|
499 | 507 | @Override
|
|
0 commit comments