diff --git a/logstash-core/src/main/java/org/logstash/config/ir/compiler/EventCondition.java b/logstash-core/src/main/java/org/logstash/config/ir/compiler/EventCondition.java index 0e4c15ab4..829167b9e 100644 --- a/logstash-core/src/main/java/org/logstash/config/ir/compiler/EventCondition.java +++ b/logstash-core/src/main/java/org/logstash/config/ir/compiler/EventCondition.java @@ -7,6 +7,7 @@ import java.util.Objects; import java.util.function.Predicate; import org.jruby.Ruby; +import org.jruby.RubyRegexp; import org.jruby.RubyString; import org.jruby.runtime.builtin.IRubyObject; import org.jruby.util.ByteList; @@ -467,22 +468,30 @@ public interface EventCondition { !Boolean.toString(false).equals(other); } + private static RubyRegexp newRegexp(String pattern) { + final Ruby runtime = RubyUtil.RUBY; + return RubyRegexp.newRegexpFromStr(runtime, runtime.newString(pattern), 0); + } + + private static boolean matches(RubyString str, RubyRegexp regexp) { + return regexp.match_p(RubyUtil.RUBY.getCurrentContext(), str).isTrue(); // match? returns true/false + } + private static final class FieldMatches implements EventCondition { private final FieldReference field; - private final RubyString regex; + private final RubyRegexp regexp; - private FieldMatches(final String field, final String regex) { + private FieldMatches(final String field, final String pattern) { this.field = FieldReference.from(field); - this.regex = RubyUtil.RUBY.newString(regex); + this.regexp = newRegexp(pattern); } @Override public boolean fulfilled(final JrubyEventExtLibrary.RubyEvent event) { - final Object tomatch = event.getEvent().getUnconvertedField(field); - return tomatch instanceof RubyString && - !((RubyString) tomatch).match(RubyUtil.RUBY.getCurrentContext(), regex).isNil(); + final Object toMatch = event.getEvent().getUnconvertedField(field); + return toMatch instanceof RubyString && matches((RubyString) toMatch, regexp); } } @@ -490,12 +499,9 @@ public interface EventCondition { private final boolean matches; - private ConstantMatches(final Object constant, final String regex) { - final Ruby runtime = RubyUtil.RUBY; + private ConstantMatches(final Object constant, final String pattern) { this.matches = constant instanceof String && - !(runtime.newString((String) constant).match( - runtime.getCurrentContext(), - runtime.newString(regex)).isNil()); + matches(RubyUtil.RUBY.newString((String) constant), newRegexp(pattern)); } @Override