/** * If a sequence contains content after an end anchor then it will never be able to match * and can be treated as false. */ static Matcher sequenceWithStuffAfterEndIsFalse(Matcher matcher) { if (matcher instanceof SeqMatcher) { boolean end = false; for (Matcher m : matcher.<SeqMatcher>as().matchers()) { if (m instanceof EndMatcher) { end = true; } else if (end && !m.alwaysMatches()) { return FalseMatcher.INSTANCE; } } } return matcher; }
/** * If a sequence contains content after an end anchor then it will never be able to match * and can be treated as false. */ static Matcher sequenceWithStuffAfterEndIsFalse(Matcher matcher) { if (matcher instanceof SeqMatcher) { boolean end = false; for (Matcher m : matcher.<SeqMatcher>as().matchers()) { if (m instanceof EndMatcher) { end = true; } else if (end && !m.alwaysMatches()) { return FalseMatcher.INSTANCE; } } } return matcher; }
/** * Extract common prefix from OR clause. This is beneficial because it reduces the amount * that needs to be checked for each branch. For example: {@code "ab|ac" => "a(b|c)"}. */ static Matcher extractPrefixFromOr(Matcher matcher) { if (matcher instanceof OrMatcher) { // Get the prefix for the first condition List<Matcher> matchers = matcher.<OrMatcher>as().matchers(); if (matchers.isEmpty()) { return matcher; } Matcher prefix = PatternUtils.getPrefix(matchers.get(0)); if (prefix.alwaysMatches()) { return matcher; } List<Matcher> ms = new ArrayList<>(); ms.add(PatternUtils.getSuffix(matchers.get(0))); // Verify all OR conditions have the same prefix for (Matcher m : matchers.subList(1, matchers.size())) { Matcher p = PatternUtils.getPrefix(m); if (!prefix.equals(p)) { return matcher; } ms.add(PatternUtils.getSuffix(m)); } return SeqMatcher.create(prefix, OrMatcher.create(ms)); } return matcher; }
/** * Extract common prefix from OR clause. This is beneficial because it reduces the amount * that needs to be checked for each branch. For example: {@code "ab|ac" => "a(b|c)"}. */ static Matcher extractPrefixFromOr(Matcher matcher) { if (matcher instanceof OrMatcher) { // Get the prefix for the first condition List<Matcher> matchers = matcher.<OrMatcher>as().matchers(); if (matchers.isEmpty()) { return matcher; } Matcher prefix = PatternUtils.getPrefix(matchers.get(0)); if (prefix.alwaysMatches()) { return matcher; } List<Matcher> ms = new ArrayList<>(); ms.add(PatternUtils.getSuffix(matchers.get(0))); // Verify all OR conditions have the same prefix for (Matcher m : matchers.subList(1, matchers.size())) { Matcher p = PatternUtils.getPrefix(m); if (!prefix.equals(p)) { return matcher; } ms.add(PatternUtils.getSuffix(m)); } return SeqMatcher.create(prefix, OrMatcher.create(ms)); } return matcher; }