From 142a5e3de1b8f18d2840ab8f3788ab5adcc4cf2b Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Mon, 16 Nov 2026 19:13:07 -0500 Subject: [PATCH 1/3] Evaluate FFE regex conditions in linear time Replace java.util.regex for MATCHES and NOT_MATCHES with tiers like the Rust evaluator: literal checks for patterns that are only text, checks that need no engine for .*, .+ and \b-literal patterns, and RE2/J with required prefix and suffix checks for all other patterns. A long attribute value can no longer make one evaluation backtrack for milliseconds. Use the dialect of the Rust regex crate, which libdatadog uses: $ matches only at the end of the input, . does not match a line feed, the u flag is accepted, and lookaround, backreferences and atomic groups are a parse error. The parser compiles each distinct pattern once per configuration. --- dd-openfeature/build.gradle.kts | 2 + dd-openfeature/gradle.lockfile | 1 + .../internal/evaluation/FlagEvaluator.java | 17 +- .../internal/ufc/ConditionConfiguration.java | 26 +- .../internal/ufc/RegexMatcher.java | 802 ++++++++++++++++++ .../ufc/UniversalFlagConfigParser.java | 28 +- .../internal/ufc/RegexMatcherTest.java | 333 ++++++++ 7 files changed, 1180 insertions(+), 29 deletions(-) create mode 100644 dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/RegexMatcher.java create mode 100644 dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/RegexMatcherTest.java diff --git a/dd-openfeature/build.gradle.kts b/dd-openfeature/build.gradle.kts index ecd5d79228c..31910fd81c3 100644 --- a/dd-openfeature/build.gradle.kts +++ b/dd-openfeature/build.gradle.kts @@ -72,6 +72,8 @@ java { dependencies { api("dev.openfeature:sdk:1.23.0") implementation("io.opentelemetry:opentelemetry-api:1.66.0") + // Linear-time regex engine for MATCHES and NOT_MATCHES, like the Rust evaluator. + implementation(libs.re2j) api(libs.jackson.core) testImplementation(libs.bundles.junit5) diff --git a/dd-openfeature/gradle.lockfile b/dd-openfeature/gradle.lockfile index 7a225b9fcf6..52002ec129c 100644 --- a/dd-openfeature/gradle.lockfile +++ b/dd-openfeature/gradle.lockfile @@ -13,6 +13,7 @@ com.github.stephenc.jcip:jcip-annotations:1.0-1=spotbugs com.google.code.findbugs:jsr305:3.0.2=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,spotbugs,testCompileClasspath,testRuntimeClasspath com.google.code.gson:gson:2.14.0=spotbugs com.google.errorprone:error_prone_annotations:2.48.0=spotbugs +com.google.re2j:re2j:1.8=compileClasspath,jmhCompileClasspath,jmhRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath com.thoughtworks.qdox:qdox:1.12.1=codenarc commons-io:commons-io:2.21.0=spotbugs de.thetaphi:forbiddenapis:3.11=compileClasspath,jmhCompileClasspath diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java index 55ba7dc45ce..92d5b5306de 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java @@ -19,6 +19,7 @@ import com.datadog.openfeature.internal.ufc.Feature; import com.datadog.openfeature.internal.ufc.Flag; import com.datadog.openfeature.internal.ufc.ParsedSemver; +import com.datadog.openfeature.internal.ufc.RegexMatcher; import com.datadog.openfeature.internal.ufc.Rule; import com.datadog.openfeature.internal.ufc.ServerConfiguration; import com.datadog.openfeature.internal.ufc.Shard; @@ -41,7 +42,6 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -314,9 +314,9 @@ private static boolean evaluateCondition( switch (condition.operator) { case MATCHES: - return matchesRegex(attributeValue, condition); + return matchesRegex(attributeValue, condition, true); case NOT_MATCHES: - return !matchesRegex(attributeValue, condition); + return matchesRegex(attributeValue, condition, false); case ONE_OF: return isOneOf(attributeValue, condition.value); case NOT_ONE_OF: @@ -346,15 +346,18 @@ private static boolean evaluateCondition( } } + /** Evaluates MATCHES ({@code expectMatch} true) or NOT_MATCHES ({@code expectMatch} false). */ private static boolean matchesRegex( - final Object attributeValue, final ConditionConfiguration condition) { - // Parsed configurations cache the validated pattern. Directly constructed configurations + final Object attributeValue, + final ConditionConfiguration condition, + final boolean expectMatch) { + // Parsed configurations cache the validated matcher. Directly constructed configurations // retain the same syntax and error handling. - final Pattern pattern = + final RegexMatcher regex = condition.regex != null ? condition.regex : ConditionConfiguration.compileRegex(condition.value); - return pattern.matcher(String.valueOf(attributeValue)).find(); + return regex.test(String.valueOf(attributeValue), expectMatch); } private static boolean isOneOf(final Object attributeValue, final Object conditionValue) { diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/ConditionConfiguration.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/ConditionConfiguration.java index 484d126cecf..154cf9f0fc1 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/ConditionConfiguration.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/ConditionConfiguration.java @@ -1,7 +1,5 @@ package com.datadog.openfeature.internal.ufc; -import java.util.regex.Pattern; - public final class ConditionConfiguration { public final ConditionOperator operator; public final String attribute; @@ -11,8 +9,8 @@ public final class ConditionConfiguration { // (not from JSON) when the operator is a SEMVER_* operator. public transient ParsedSemver semverComparand; - // The validated MATCHES/NOT_MATCHES pattern, prepared when the configuration is parsed. - public transient Pattern regex; + // The validated MATCHES/NOT_MATCHES matcher, prepared when the configuration is parsed. + public transient RegexMatcher regex; public ConditionConfiguration( final ConditionOperator operator, final String attribute, final Object value) { @@ -21,18 +19,12 @@ public ConditionConfiguration( this.value = value; } - /** Compiles the Java pattern after translating the supported POSIX character classes. */ - public static Pattern compileRegex(final Object value) { - return Pattern.compile(normalizeRegex(String.valueOf(value))); - } - - private static String normalizeRegex(final String regex) { - return regex - .replace("[:alnum:]", "\\p{Alnum}") - .replace("[:alpha:]", "\\p{Alpha}") - .replace("[:digit:]", "\\p{Digit}") - .replace("[:lower:]", "\\p{Lower}") - .replace("[:upper:]", "\\p{Upper}") - .replace("[:space:]", "\\p{Space}"); + /** + * Compiles a MATCHES/NOT_MATCHES pattern. + * + * @throws java.util.regex.PatternSyntaxException if the pattern is malformed. + */ + public static RegexMatcher compileRegex(final Object value) { + return RegexMatcher.compile(String.valueOf(value)); } } diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/RegexMatcher.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/RegexMatcher.java new file mode 100644 index 00000000000..3aa2b40b2c3 --- /dev/null +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/RegexMatcher.java @@ -0,0 +1,802 @@ +package com.datadog.openfeature.internal.ufc; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import javax.annotation.Nullable; + +/** + * A compiled MATCHES/NOT_MATCHES pattern, with {@code find} semantics: the pattern matches if it + * matches any part of the input. + * + *

The design follows the Rust evaluator (dd-source {@code rkyv/regex.rs}). At parse time, each + * pattern gets the cheapest matcher that gives the same result: + * + *

    + *
  1. A literal check ({@code equals}, {@code startsWith}, {@code endsWith}, {@code contains}) + * for a pattern that is only text, with optional anchors, an optional leading or trailing + * {@code .*}, and groups of literal alternatives, for example {@code .*@(a|b)\.com$}. + *
  2. A check that needs no engine: the empty pattern and {@code .*} always match, {@code .+} + * matches any input that has a character other than a line feed, and {@code \bliteral\b} + * finds the literal at Unicode word boundaries. + *
  3. RE2/J, a linear-time engine with the same syntax family as the Rust {@code regex} crate. It + * does not backtrack, so a long input cannot block the thread. A literal prefix ({@code + * ^abc...}) or suffix ({@code ...abc$}) that every match needs is checked first. + *
+ * + *

The syntax and semantics are the RE2 and Rust ones, not the {@code java.util.regex} ones. For + * example, {@code $} matches only at the end of the input, and {@code .} matches any character + * except a line feed. + * + *

A pattern that RE2/J rejects is invalid, also when {@code java.util.regex} accepts it + * (lookaround, backreferences, possessive quantifiers): {@link #compile} throws, and the parser + * drops the flag with a parse error. The Rust SDK evaluator (libdatadog) does the same. + * + *

Instances are immutable and thread safe. + */ +public abstract class RegexMatcher { + /** The most literals that a pattern can expand to and still use a literal check. */ + static final int MAX_LITERALS = 64; + + /** The matcher kind, for tests and diagnostics. */ + public enum Kind { + EQUALS, + STARTS_WITH, + ENDS_WITH, + CONTAINS, + WORD_BOUNDARY, + ALWAYS, + ANY_CHARACTER, + ENGINE + } + + private final String pattern; + + RegexMatcher(final String pattern) { + this.pattern = pattern; + } + + public String pattern() { + return pattern; + } + + public abstract Kind kind(); + + /** Tells whether the pattern matches a part of the input. */ + abstract boolean find(String input); + + /** + * Evaluates the condition. + * + * @param input the attribute value. + * @param expectMatch {@code true} for MATCHES, {@code false} for NOT_MATCHES. + * @return whether the match result is {@code expectMatch}. + */ + public boolean test(final String input, final boolean expectMatch) { + return find(input) == expectMatch; + } + + /** + * Compiles a pattern. + * + * @throws java.util.regex.PatternSyntaxException if RE2/J rejects the pattern. + */ + public static RegexMatcher compile(final String pattern) { + // The Rust regex crate has Unicode mode on by default, and accepts the u flag. RE2/J has no u + // flag, so remove it. + final String re2Pattern = withoutUnicodeFlag(pattern); + final com.google.re2j.Pattern engine; + try { + engine = com.google.re2j.Pattern.compile(re2Pattern); + } catch (final com.google.re2j.PatternSyntaxException e) { + throw new java.util.regex.PatternSyntaxException(e.getDescription(), pattern, e.getIndex()); + } + @Nullable final List tokens = Lexer.tokenize(re2Pattern); + if (tokens == null) { + return new Engine(pattern, engine, null, null); + } + @Nullable final RegexMatcher fast = FastPath.of(pattern, tokens); + if (fast != null) { + return fast; + } + return new Engine( + pattern, engine, Prefilter.requiredPrefix(tokens), Prefilter.requiredSuffixes(tokens)); + } + + /** + * Removes the {@code u} flag from inline flag groups: {@code (?u)} is removed, {@code (?iu)} + * becomes {@code (?i)}, and {@code (?u:x)} becomes {@code (?:x)}. Classes and escapes are not + * changed. + */ + static String withoutUnicodeFlag(final String pattern) { + if (pattern.indexOf("(?") < 0) { + return pattern; + } + final StringBuilder out = new StringBuilder(pattern.length()); + int i = 0; + while (i < pattern.length()) { + final char c = pattern.charAt(i); + if (c == '\\' && i + 1 < pattern.length()) { + out.append(c).append(pattern.charAt(i + 1)); + i += 2; + } else if (c == '[') { + final int end = Lexer.classEnd(pattern, i); + final int stop = end < 0 ? pattern.length() : end + 1; + out.append(pattern, i, stop); + i = stop; + } else if (c == '(' && i + 1 < pattern.length() && pattern.charAt(i + 1) == '?') { + int j = i + 2; + final StringBuilder flags = new StringBuilder(); + while (j < pattern.length() + && (Character.isLetter(pattern.charAt(j)) || pattern.charAt(j) == '-')) { + if (pattern.charAt(j) != 'u') { + flags.append(pattern.charAt(j)); + } + j++; + } + final boolean flagGroup = + j > i + 2 + && j < pattern.length() + && (pattern.charAt(j) == ')' || pattern.charAt(j) == ':'); + if (!flagGroup) { + out.append(c); + i++; + } else if (pattern.charAt(j) == ')' && (flags.length() == 0 || "-".contentEquals(flags))) { + i = j + 1; + } else { + out.append("(?").append(flags).append(pattern.charAt(j)); + i = j + 1; + } + } else { + out.append(c); + i++; + } + } + return out.toString(); + } + + // ---- Matchers --------------------------------------------------------------------------- + + /** The empty pattern, or {@code .*}: every input matches. */ + static final class Always extends RegexMatcher { + Always(final String pattern) { + super(pattern); + } + + @Override + public Kind kind() { + return Kind.ALWAYS; + } + + @Override + boolean find(final String input) { + return true; + } + } + + /** {@code .+}: the input has a character other than a line feed. */ + static final class AnyCharacter extends RegexMatcher { + AnyCharacter(final String pattern) { + super(pattern); + } + + @Override + public Kind kind() { + return Kind.ANY_CHARACTER; + } + + @Override + boolean find(final String input) { + for (int i = 0; i < input.length(); i++) { + if (input.charAt(i) != '\n') { + return true; + } + } + return false; + } + } + + /** A literal check against one or more literals. */ + static final class Literals extends RegexMatcher { + private final Kind kind; + private final String[] literals; + + Literals(final String pattern, final Kind kind, final List literals) { + super(pattern); + this.kind = kind; + this.literals = literals.toArray(new String[0]); + } + + @Override + public Kind kind() { + return kind; + } + + List literals() { + return Collections.unmodifiableList(java.util.Arrays.asList(literals)); + } + + @Override + boolean find(final String input) { + for (final String literal : literals) { + final boolean hit; + switch (kind) { + case EQUALS: + hit = input.equals(literal); + break; + case STARTS_WITH: + hit = input.startsWith(literal); + break; + case ENDS_WITH: + hit = input.endsWith(literal); + break; + default: + hit = input.contains(literal); + break; + } + if (hit) { + return true; + } + } + return false; + } + } + + /** + * {@code \bliteral} or {@code \bliteral\b}: the literal, at a Unicode word boundary. RE2/J has + * only ASCII word boundaries, so this case does not use it. It follows the Rust evaluator's + * {@code UnicodeWordBoundaryStartsWithAny}. + */ + static final class WordBoundary extends RegexMatcher { + private final String[] literals; + private final boolean trailingBoundary; + + WordBoundary( + final String pattern, final List literals, final boolean trailingBoundary) { + super(pattern); + this.literals = literals.toArray(new String[0]); + this.trailingBoundary = trailingBoundary; + } + + @Override + public Kind kind() { + return Kind.WORD_BOUNDARY; + } + + @Override + boolean find(final String input) { + for (final String literal : literals) { + for (int at = input.indexOf(literal); at >= 0; at = input.indexOf(literal, at + 1)) { + if (isWordBoundary(input, at) + && (!trailingBoundary || isWordBoundary(input, at + literal.length()))) { + return true; + } + } + } + return false; + } + + static boolean isWordBoundary(final String s, final int index) { + final boolean before = index > 0 && isWordCharacter(s.codePointBefore(index)); + final boolean after = index < s.length() && isWordCharacter(s.codePointAt(index)); + return before != after; + } + + /** The Unicode {@code \w} of the Rust regex crate (UTS #18 Annex C). */ + static boolean isWordCharacter(final int codePoint) { + if (Character.isAlphabetic(codePoint) || codePoint == 0x200C || codePoint == 0x200D) { + return true; + } + switch (Character.getType(codePoint)) { + case Character.NON_SPACING_MARK: + case Character.ENCLOSING_MARK: + case Character.COMBINING_SPACING_MARK: + case Character.DECIMAL_DIGIT_NUMBER: + case Character.CONNECTOR_PUNCTUATION: + return true; + default: + return false; + } + } + } + + /** + * RE2/J, with optional literal checks that every match needs: a prefix, and a set of suffixes of + * which the input must end with one. + */ + static final class Engine extends RegexMatcher { + private final com.google.re2j.Pattern engine; + @Nullable private final String requiredPrefix; + @Nullable private final String[] requiredSuffixes; + + Engine( + final String pattern, + final com.google.re2j.Pattern engine, + @Nullable final String requiredPrefix, + @Nullable final List requiredSuffixes) { + super(pattern); + this.engine = engine; + this.requiredPrefix = requiredPrefix; + this.requiredSuffixes = + requiredSuffixes == null ? null : requiredSuffixes.toArray(new String[0]); + } + + @Override + public Kind kind() { + return Kind.ENGINE; + } + + @Nullable + String requiredPrefix() { + return requiredPrefix; + } + + @Nullable + List requiredSuffixes() { + return requiredSuffixes == null ? null : java.util.Arrays.asList(requiredSuffixes); + } + + @Override + boolean find(final String input) { + if (requiredPrefix != null && !input.startsWith(requiredPrefix)) { + return false; + } + if (requiredSuffixes != null && !endsWithAny(input, requiredSuffixes)) { + return false; + } + return engine.matcher(input).find(); + } + + private static boolean endsWithAny(final String input, final String[] suffixes) { + for (final String suffix : suffixes) { + if (input.endsWith(suffix)) { + return true; + } + } + return false; + } + } + + // ---- Pattern analysis ------------------------------------------------------------------- + + /** One lexical token of a pattern. */ + static final class Token { + /** A literal character. */ + static final int LITERAL = 0; + + /** One of {@code . * + ? | ) ^ $ { }}. */ + static final int META = 1; + + /** {@code (} or {@code (?:}. */ + static final int GROUP = 2; + + /** Any other construct: a class, or an escape such as {@code \d} or {@code \b}. */ + static final int OTHER = 3; + + final int type; + final char value; + + Token(final int type, final char value) { + this.type = type; + this.value = value; + } + + boolean isMeta(final char c) { + return type == META && value == c; + } + + boolean isQuantifier() { + return type == META && (value == '*' || value == '+' || value == '?' || value == '{'); + } + } + + /** + * A small, strict lexer. It reads only what the fast paths and prefilters need. RE2/J has already + * accepted the pattern, so the lexer can assume that it is well formed. + */ + static final class Lexer { + private static final String ESCAPABLE = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; + + private Lexer() {} + + /** + * @return the tokens, or {@code null} when the pattern has flags or a special group ({@code + * (?i)}, {@code (?P...)}): their effect on anchors and case needs the full engine. + */ + @Nullable + static List tokenize(final String pattern) { + final List tokens = new ArrayList<>(pattern.length()); + int i = 0; + while (i < pattern.length()) { + final char c = pattern.charAt(i); + if (c == '\\') { + if (i + 1 >= pattern.length()) { + return null; + } + final char e = pattern.charAt(i + 1); + i += 2; + if (ESCAPABLE.indexOf(e) >= 0) { + tokens.add(new Token(Token.LITERAL, e)); + } else if (e == 't') { + tokens.add(new Token(Token.LITERAL, '\t')); + } else if (e == 'n') { + tokens.add(new Token(Token.LITERAL, '\n')); + } else if (e == 'r') { + tokens.add(new Token(Token.LITERAL, '\r')); + } else if (e == 'f') { + tokens.add(new Token(Token.LITERAL, '\f')); + } else { + // \d, \w, \s, \b, \A, \z, \p{..}, \x.., \Q...\E and others. + tokens.add(new Token(Token.OTHER, e)); + } + } else if (c == '[') { + final int end = classEnd(pattern, i); + if (end < 0) { + return null; + } + tokens.add(new Token(Token.OTHER, '[')); + i = end + 1; + } else if (c == '(') { + if (i + 1 < pattern.length() && pattern.charAt(i + 1) == '?') { + if (i + 2 < pattern.length() && pattern.charAt(i + 2) == ':') { + tokens.add(new Token(Token.GROUP, '(')); + i += 3; + } else { + return null; + } + } else { + tokens.add(new Token(Token.GROUP, '(')); + i++; + } + } else if (".*+?|)^${}".indexOf(c) >= 0) { + tokens.add(new Token(Token.META, c)); + i++; + } else { + tokens.add(new Token(Token.LITERAL, c)); + i++; + } + } + return tokens; + } + + /** Finds the {@code ]} that closes the class that starts at {@code start}, or -1. */ + static int classEnd(final String pattern, final int start) { + int j = start + 1; + if (j < pattern.length() && pattern.charAt(j) == '^') { + j++; + } + if (j < pattern.length() && pattern.charAt(j) == ']') { + j++; + } + while (j < pattern.length()) { + final char c = pattern.charAt(j); + if (c == '\\') { + j += 2; + } else if (c == '[' && j + 1 < pattern.length() && pattern.charAt(j + 1) == ':') { + final int close = pattern.indexOf(":]", j + 2); + if (close < 0) { + return -1; + } + j = close + 2; + } else if (c == ']') { + return j; + } else { + j++; + } + } + return -1; + } + } + + /** The literal fast paths, and the checks that need no engine. */ + static final class FastPath { + private FastPath() {} + + @Nullable + static RegexMatcher of(final String pattern, final List all) { + List tokens = all; + if (tokens.isEmpty() || isDotStar(tokens, 0) == tokens.size()) { + return new Always(pattern); + } + if (isDotPlus(tokens)) { + return new AnyCharacter(pattern); + } + if (isWordBoundary(tokens.get(0))) { + final boolean trailing = tokens.size() > 1 && isWordBoundary(tokens.get(tokens.size() - 1)); + @Nullable + final List literals = + expand(tokens.subList(1, tokens.size() - (trailing ? 1 : 0)), false); + return literals == null ? null : new WordBoundary(pattern, literals, trailing); + } + final boolean startAnchored = tokens.get(0).isMeta('^'); + if (startAnchored) { + tokens = tokens.subList(1, tokens.size()); + } else { + // find() already tries each start position, so a leading .* changes nothing. + tokens = tokens.subList(isDotStar(tokens, 0), tokens.size()); + } + final boolean endAnchored = !tokens.isEmpty() && tokens.get(tokens.size() - 1).isMeta('$'); + if (endAnchored) { + tokens = tokens.subList(0, tokens.size() - 1); + } else { + // A trailing .* can match the empty string, so it changes nothing. + tokens = tokens.subList(0, tokens.size() - trailingDotStar(tokens)); + } + @Nullable final List literals = expand(tokens, !startAnchored && !endAnchored); + if (literals == null) { + return null; + } + final Kind kind = + startAnchored + ? (endAnchored ? Kind.EQUALS : Kind.STARTS_WITH) + : (endAnchored ? Kind.ENDS_WITH : Kind.CONTAINS); + return new Literals(pattern, kind, literals); + } + + private static boolean isWordBoundary(final Token token) { + return token.type == Token.OTHER && token.value == 'b'; + } + + /** The length of a {@code .*} or {@code .*?} at {@code index}, or 0. */ + private static int isDotStar(final List tokens, final int index) { + if (index + 1 < tokens.size() + && tokens.get(index).isMeta('.') + && tokens.get(index + 1).isMeta('*')) { + return index + 2 < tokens.size() && tokens.get(index + 2).isMeta('?') ? 3 : 2; + } + return 0; + } + + private static int trailingDotStar(final List tokens) { + final int n = tokens.size(); + if (n >= 3 && isDotStar(tokens, n - 3) == 3) { + return 3; + } + if (n >= 2 && isDotStar(tokens, n - 2) == 2) { + return 2; + } + return 0; + } + + private static boolean isDotPlus(final List tokens) { + return (tokens.size() == 2 || (tokens.size() == 3 && tokens.get(2).isMeta('?'))) + && tokens.get(0).isMeta('.') + && tokens.get(1).isMeta('+'); + } + + /** + * Expands literal text and groups of literal alternatives into the list of literals that the + * body can match. Returns {@code null} for any other construct, for an empty literal, or for + * more than {@link #MAX_LITERALS} literals. + * + * @param topLevelAlternation whether {@code a|b} at the top level is allowed. It is allowed + * only without anchors, because {@code ^a|b$} means {@code (^a)|(b$)}. + */ + @Nullable + private static List expand( + final List tokens, final boolean topLevelAlternation) { + if (tokens.isEmpty()) { + return null; + } + final List> branches = splitTopLevel(tokens); + if (branches == null || (branches.size() > 1 && !topLevelAlternation)) { + return null; + } + final List out = new ArrayList<>(); + for (final List branch : branches) { + @Nullable final List literals = expandBranch(branch); + if (literals == null || out.size() + literals.size() > MAX_LITERALS) { + return null; + } + out.addAll(literals); + } + return out; + } + + /** Splits on {@code |} outside groups. Returns {@code null} for unbalanced groups. */ + @Nullable + private static List> splitTopLevel(final List tokens) { + final List> branches = new ArrayList<>(); + int depth = 0; + int start = 0; + for (int i = 0; i < tokens.size(); i++) { + final Token t = tokens.get(i); + if (t.type == Token.GROUP) { + depth++; + } else if (t.isMeta(')')) { + depth--; + if (depth < 0) { + return null; + } + } else if (depth == 0 && t.isMeta('|')) { + branches.add(tokens.subList(start, i)); + start = i + 1; + } + } + if (depth != 0) { + return null; + } + branches.add(tokens.subList(start, tokens.size())); + return branches; + } + + @Nullable + private static List expandBranch(final List tokens) { + List product = new ArrayList<>(); + product.add(new StringBuilder()); + int i = 0; + while (i < tokens.size()) { + final Token t = tokens.get(i); + if (t.type == Token.LITERAL) { + if (quantified(tokens, i + 1)) { + return null; + } + for (final StringBuilder s : product) { + s.append(t.value); + } + i++; + } else if (t.type == Token.GROUP) { + final List alternatives = new ArrayList<>(); + StringBuilder current = new StringBuilder(); + int j = i + 1; + for (; j < tokens.size() && !tokens.get(j).isMeta(')'); j++) { + final Token u = tokens.get(j); + if (u.type == Token.LITERAL) { + current.append(u.value); + } else if (u.isMeta('|')) { + alternatives.add(current.toString()); + current = new StringBuilder(); + } else { + return null; + } + } + alternatives.add(current.toString()); + if (j >= tokens.size() || quantified(tokens, j + 1) || alternatives.contains("")) { + return null; + } + if (product.size() * alternatives.size() > MAX_LITERALS) { + return null; + } + final List next = new ArrayList<>(product.size() * alternatives.size()); + for (final StringBuilder prefix : product) { + for (final String alternative : alternatives) { + next.add(new StringBuilder(prefix).append(alternative)); + } + } + product = next; + i = j + 1; + } else { + return null; + } + } + final List out = new ArrayList<>(product.size()); + for (final StringBuilder s : product) { + if (s.length() == 0) { + return null; + } + out.add(s.toString()); + } + return out; + } + + private static boolean quantified(final List tokens, final int next) { + return next < tokens.size() && tokens.get(next).isQuantifier(); + } + } + + /** Literal checks that every match needs, run before the engine. */ + static final class Prefilter { + private Prefilter() {} + + /** + * For {@code ^abc...}: {@code abc}. A last character that {@code *}, {@code ?} or {@code {} + * follows can be absent, so it is not part of the prefix. One that {@code +} follows stays. + */ + @Nullable + static String requiredPrefix(final List tokens) { + if (tokens.isEmpty() || !tokens.get(0).isMeta('^') || hasTopLevelAlternation(tokens)) { + return null; + } + final StringBuilder prefix = new StringBuilder(); + int i = 1; + for (; i < tokens.size() && tokens.get(i).type == Token.LITERAL; i++) { + prefix.append(tokens.get(i).value); + } + if (prefix.length() > 0 + && i < tokens.size() + && tokens.get(i).isQuantifier() + && !tokens.get(i).isMeta('+')) { + prefix.setLength(prefix.length() - 1); + } + return prefix.length() == 0 ? null : prefix.toString(); + } + + /** + * For a pattern that ends with literal text and groups of literal alternatives before {@code + * $}, the suffixes of which a matching input ends with one. For example, {@code + * ^u\d+@(?:a|b)\.(?:io|net)$} gives {@code @a.io}, {@code @a.net}, {@code @b.io} and {@code + * @b.net}. When the expansion has more than {@link #MAX_LITERALS} suffixes, the earlier parts + * are left out, so the suffixes become shorter. + */ + @Nullable + static List requiredSuffixes(final List tokens) { + final int last = tokens.size() - 1; + if (last < 1 || !tokens.get(last).isMeta('$') || hasTopLevelAlternation(tokens)) { + return null; + } + // The parts before $, from the last one back. Each part is a list of alternatives. + final List> parts = new ArrayList<>(); + int i = last - 1; + while (i >= 0) { + final Token t = tokens.get(i); + if (t.type == Token.LITERAL) { + parts.add(Collections.singletonList(String.valueOf(t.value))); + i--; + continue; + } + if (!t.isMeta(')')) { + break; + } + int open = i - 1; + while (open >= 0 && tokens.get(open).type != Token.GROUP) { + open--; + } + @Nullable final List alternatives = alternatives(tokens, open + 1, i); + if (open < 0 || alternatives == null) { + break; + } + parts.add(alternatives); + i = open - 1; + } + List suffixes = Collections.singletonList(""); + for (final List part : parts) { + if (suffixes.size() * part.size() > MAX_LITERALS) { + break; + } + final List next = new ArrayList<>(suffixes.size() * part.size()); + for (final String alternative : part) { + for (final String suffix : suffixes) { + next.add(alternative + suffix); + } + } + suffixes = next; + } + return suffixes.get(0).isEmpty() ? null : suffixes; + } + + /** The literal alternatives between two tokens, or {@code null} for any other construct. */ + @Nullable + private static List alternatives( + final List tokens, final int from, final int to) { + final List out = new ArrayList<>(); + StringBuilder current = new StringBuilder(); + for (int j = from; j < to; j++) { + final Token u = tokens.get(j); + if (u.type == Token.LITERAL) { + current.append(u.value); + } else if (u.isMeta('|')) { + out.add(current.toString()); + current = new StringBuilder(); + } else { + return null; + } + } + out.add(current.toString()); + return out.contains("") ? null : out; + } + + private static boolean hasTopLevelAlternation(final List tokens) { + int depth = 0; + for (final Token t : tokens) { + if (t.type == Token.GROUP) { + depth++; + } else if (t.isMeta(')')) { + depth--; + } else if (depth == 0 && t.isMeta('|')) { + return true; + } + } + return false; + } + } +} diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParser.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParser.java index d6fb0e435f4..9f125ceb5d2 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParser.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParser.java @@ -174,12 +174,14 @@ private static Map toFlags( return null; } final Map flags = new HashMap<>(); + // One matcher for each distinct pattern in this configuration. + final Map regexes = new HashMap<>(); for (final Map.Entry entry : asObject(value, "flags").entrySet()) { final String flagKey = (String) entry.getKey(); try { final Flag flag = toFlag(entry.getValue()); if (flag != null) { - validateFlag(flagKey, flag); + validateFlag(flagKey, flag, regexes); flags.put(flagKey, flag); } } catch (final IllegalArgumentException error) { @@ -491,7 +493,8 @@ private static Instant toInstant(@Nullable final Object value) { } /** Validates the required nested UFC fields and condition operands for a flag. */ - private static void validateFlag(final String flagKey, final Flag flag) { + private static void validateFlag( + final String flagKey, final Flag flag, final Map regexes) { validateNumericVariants(flagKey, flag); if (flag.allocations == null) { return; @@ -500,7 +503,7 @@ private static void validateFlag(final String flagKey, final Flag flag) { if (allocation == null) { continue; } - validateConditionOperands(flagKey, allocation); + validateConditionOperands(flagKey, allocation, regexes); if (allocation.splits == null) { continue; } @@ -528,6 +531,20 @@ private static void validateFlag(final String flagKey, final Flag flag) { validateAndCacheSemverComparands(flagKey, flag); } + /** + * Returns the shared matcher for a pattern, and compiles it on first use. + * + * @throws java.util.regex.PatternSyntaxException if the pattern is malformed. + */ + private static RegexMatcher regex(final String pattern, final Map regexes) { + RegexMatcher matcher = regexes.get(pattern); + if (matcher == null) { + matcher = RegexMatcher.compile(pattern); + regexes.put(pattern, matcher); + } + return matcher; + } + /** Preserve Java's numeric coercion, but reject values that cannot be mapped at all. */ private static void validateNumericVariants(final String flagKey, final Flag flag) { if (flag.variations == null @@ -546,7 +563,8 @@ private static void validateNumericVariants(final String flagKey, final Flag fla } } - private static void validateConditionOperands(final String flagKey, final Allocation allocation) { + private static void validateConditionOperands( + final String flagKey, final Allocation allocation, final Map regexes) { if (allocation.rules == null) { return; } @@ -562,7 +580,7 @@ private static void validateConditionOperands(final String flagKey, final Alloca case MATCHES: case NOT_MATCHES: // Configuration validity is independent of targeting and experimentation opt-out. - condition.regex = ConditionConfiguration.compileRegex(condition.value); + condition.regex = regex(String.valueOf(condition.value), regexes); break; case LT: case LTE: diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/RegexMatcherTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/RegexMatcherTest.java new file mode 100644 index 00000000000..58b1609a9c8 --- /dev/null +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/RegexMatcherTest.java @@ -0,0 +1,333 @@ +package com.datadog.openfeature.internal.ufc; + +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import com.datadog.openfeature.internal.ufc.RegexMatcher.Kind; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.SplittableRandom; +import java.util.regex.PatternSyntaxException; +import java.util.stream.Stream; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class RegexMatcherTest { + + static Stream classifications() { + return Stream.of( + Arguments.of("", Kind.ALWAYS, null), + Arguments.of(".*", Kind.ALWAYS, null), + Arguments.of(".+", Kind.ANY_CHARACTER, null), + Arguments.of("zach", Kind.CONTAINS, Arrays.asList("zach")), + Arguments.of("@example\\.com", Kind.CONTAINS, Arrays.asList("@example.com")), + Arguments.of("abc|def", Kind.CONTAINS, Arrays.asList("abc", "def")), + Arguments.of("^abc", Kind.STARTS_WITH, Arrays.asList("abc")), + Arguments.of("abc$", Kind.ENDS_WITH, Arrays.asList("abc")), + Arguments.of("^abc$", Kind.EQUALS, Arrays.asList("abc")), + Arguments.of(".*\\@(a|b)$", Kind.ENDS_WITH, Arrays.asList("@a", "@b")), + Arguments.of( + ".*@(alpha|bravo)\\.(io|net)$", + Kind.ENDS_WITH, + Arrays.asList("@alpha.io", "@alpha.net", "@bravo.io", "@bravo.net")), + Arguments.of("(?:x|y)z.*", Kind.CONTAINS, Arrays.asList("xz", "yz")), + Arguments.of("^(us|eu)$", Kind.EQUALS, Arrays.asList("us", "eu")), + Arguments.of(".*?abc", Kind.CONTAINS, Arrays.asList("abc")), + Arguments.of("(?u)\\bma\u00f1ana\\b", Kind.WORD_BOUNDARY, null), + Arguments.of("\\bfoo", Kind.WORD_BOUNDARY, null), + Arguments.of("\\b(foo|bar)\\b", Kind.WORD_BOUNDARY, null), + Arguments.of("(?iu)abc", Kind.ENGINE, null), + // Not literal: a dot, a class, a quantifier, flags, or an anchor next to .*. + Arguments.of(".*@example.com", Kind.ENGINE, null), + Arguments.of("^[a-zA-Z0-9._%+-]+@site\\.test$", Kind.ENGINE, null), + Arguments.of("ab+c", Kind.ENGINE, null), + Arguments.of("(?i)abc", Kind.ENGINE, null), + Arguments.of("^.*abc", Kind.ENGINE, null), + Arguments.of("abc.*$", Kind.ENGINE, null), + Arguments.of("^a|b$", Kind.ENGINE, null), + Arguments.of("(a|)b", Kind.ENGINE, null), + Arguments.of("\\d+", Kind.ENGINE, null)); + } + + @ParameterizedTest + @MethodSource("classifications") + void classifiesEachPattern(final String pattern, final Kind kind, final List literals) { + final RegexMatcher matcher = RegexMatcher.compile(pattern); + assertEquals(kind, matcher.kind(), pattern); + if (literals != null) { + assertEquals(literals, ((RegexMatcher.Literals) matcher).literals(), pattern); + } + } + + /** Each fast path and prefilter must give the same result as the RE2/J engine alone. */ + @Test + void fastPathsAgreeWithTheEngine() { + final List patterns = + Arrays.asList( + "", + ".*", + ".+", + ".+?", + "zach", + "@example\\.com", + "abc|def", + "^abc", + "abc$", + "^abc$", + ".*\\@(a|b)$", + ".*@(alpha|bravo)\\.(io|net)$", + "(?:x|y)z.*", + "^(us|eu)$", + ".*?abc", + "abc.*", + "^ab+c", + "^abc*d", + "^a{2}b", + "x+abc$", + "^[a-zA-Z0-9._%+-]+@site\\.test$", + "^u[0-9]{1,3}@(?:alpha|bravo)\\.(?:io|net)$", + "^x(?:a|b)+\\.com$", + "\\.test$", + "a\\$", + "a\\|b", + "\\(a\\)", + "tab\\there"); + final List inputs = inputs(); + for (final String pattern : patterns) { + final RegexMatcher matcher = RegexMatcher.compile(pattern); + final com.google.re2j.Pattern engine = com.google.re2j.Pattern.compile(pattern); + for (final String input : inputs) { + assertEquals( + engine.matcher(input).find(), + matcher.find(input), + "pattern=" + pattern + " input=" + input.replace("\n", "\\n")); + } + } + } + + private static List inputs() { + final List inputs = + new ArrayList<>( + Arrays.asList( + "", + "\n", + "\r", + "a\n", + "abc", + "abc\n", + "xabcx", + "def", + "zach", + "zachary", + "us", + "eu", + "use", + "xz", + "yzz", + "user@a", + "user@b", + "user@c", + "u@alpha.io", + "u@bravo.net", + "u@alpha.com", + "u12@alpha.io", + "u1234@bravo.net", + "xab.com", + "x.com", + "John.Doe+x@site.test", + "john@site.test\n", + "bad char!@site.test", + "abbbc", + "ac", + "abd", + "aab", + "xxabc", + "a$", + "a|b", + "(a)", + "tab\there", + "@example.com", + "x@example.com")); + final SplittableRandom rnd = new SplittableRandom(1); + final String alphabet = "abcdefxyz@.|$()\n\tuszach"; + for (int i = 0; i < 2000; i++) { + final StringBuilder s = new StringBuilder(); + final int length = rnd.nextInt(12); + for (int j = 0; j < length; j++) { + s.append(alphabet.charAt(rnd.nextInt(alphabet.length()))); + } + inputs.add(s.toString()); + } + return inputs; + } + + /** The RE2 and Rust semantics, where they differ from java.util.regex. */ + @Test + void usesRe2Semantics() { + // $ matches only at the end of the input, not before a final line feed. + assertFalse(RegexMatcher.compile("a$").find("a\n")); + assertFalse(RegexMatcher.compile("^[a-z]+@site\\.test$").find("john@site.test\n")); + // . matches any character except a line feed. + assertTrue(RegexMatcher.compile("^a.b$").find("a\rb")); + assertFalse(RegexMatcher.compile("^a.b$").find("a\nb")); + assertFalse(RegexMatcher.compile(".+").find("\n\n")); + assertTrue(RegexMatcher.compile(".+").find("\n\r")); + // RE2/J supports the bracketed POSIX classes. + assertTrue(RegexMatcher.compile("^[[:alnum:]]+$").find("abc123")); + assertFalse(RegexMatcher.compile("^[[:alnum:]]+$").find("abc-123")); + } + + @Test + void removesTheUnicodeFlag() { + assertEquals("abc", RegexMatcher.withoutUnicodeFlag("(?u)abc")); + assertEquals("(?i)abc", RegexMatcher.withoutUnicodeFlag("(?iu)abc")); + assertEquals("(?:abc)", RegexMatcher.withoutUnicodeFlag("(?u:abc)")); + assertEquals("(?:abc)", RegexMatcher.withoutUnicodeFlag("(?:abc)")); + assertEquals("[(?u)]x", RegexMatcher.withoutUnicodeFlag("[(?u)]x")); + assertEquals("\\(?u)", RegexMatcher.withoutUnicodeFlag("\\(?u)")); + } + + /** Word boundaries use the Rust (Unicode) definition of a word character. */ + @Test + void wordBoundaryIsUnicodeAware() { + final RegexMatcher manana = RegexMatcher.compile("(?u)\\bma\u00f1ana\\b"); + assertTrue(manana.find("llega ma\u00f1ana temprano")); + assertTrue(manana.find("ma\u00f1ana")); + assertFalse(manana.find("ma\u00f1anas")); + // A non-ASCII letter is a word character, so there is no boundary before "m". + assertFalse(manana.find("\u00f1ma\u00f1ana")); + final RegexMatcher prefix = RegexMatcher.compile("\\bfoo"); + assertTrue(prefix.find("a foobar")); + assertFalse(prefix.find("afoo")); + assertFalse(prefix.find("\u00e9foo")); + assertTrue(prefix.find("-foo")); + } + + @Test + void matchesAndNotMatchesAreOpposites() { + final RegexMatcher matcher = RegexMatcher.compile("@example\\.com$"); + assertTrue(matcher.test("a@example.com", true)); + assertFalse(matcher.test("a@example.com", false)); + assertFalse(matcher.test("a@other.com", true)); + assertTrue(matcher.test("a@other.com", false)); + } + + @Test + void malformedPatternThrows() { + assertThrows(PatternSyntaxException.class, () -> RegexMatcher.compile("*@datadoghq.com")); + assertThrows(PatternSyntaxException.class, () -> RegexMatcher.compile("(abc")); + assertThrows(PatternSyntaxException.class, () -> RegexMatcher.compile("[abc")); + // java.util.regex accepts these, but the Rust regex crate and RE2/J do not. + for (final String pattern : + Arrays.asList("(?=a)a", "(?a)", "\\p{Alnum}")) { + assertThrows(PatternSyntaxException.class, () -> RegexMatcher.compile(pattern), pattern); + } + } + + /** Patterns that make java.util.regex backtrack finish in linear time. */ + @Test + void backtrackingPatternsFinishFast() { + final String as = repeat('a', 5_000); + assertTimeoutPreemptively( + Duration.ofSeconds(2), + () -> { + assertFalse(RegexMatcher.compile("(a+)+$").find(as + "!")); + assertFalse(RegexMatcher.compile("^(a|aa)+$").find(as + "!")); + assertFalse(RegexMatcher.compile("(a*)*b").find(as)); + assertFalse(RegexMatcher.compile(".*a.*a.*a.*a.*b").find(as)); + assertFalse(RegexMatcher.compile(".*@(x|y)\\.com$").find(as + "@z.com")); + assertFalse(RegexMatcher.compile("^[a-z]+@site\\.test$").find(as + "@other.test")); + }); + } + + @Test + void findsRequiredPrefixAndSuffix() { + assertEquals( + Arrays.asList("@site.test"), engine("^[a-zA-Z0-9._%+-]+@site\\.test$").requiredSuffixes()); + assertEquals( + Arrays.asList("@alpha.io", "@alpha.net", "@bravo.io", "@bravo.net"), + engine("^u[0-9]{1,3}@(?:alpha|bravo)\\.(?:io|net)$").requiredSuffixes()); + // A quantified group is not a required suffix; only the text after it is. + assertEquals(Arrays.asList(".com"), engine("^x(?:a|b)+\\.com$").requiredSuffixes()); + assertNull(engine("^[a-zA-Z0-9._%+-]+@site\\.test$").requiredPrefix()); + assertEquals("ab", engine("^ab+c").requiredPrefix()); + assertEquals("a", engine("^ab?c").requiredPrefix()); + assertEquals("ab", engine("^abc*d").requiredPrefix()); + // {n,m} can allow zero repetitions, so the quantified character is not required. + assertNull(engine("^a{2}b").requiredPrefix()); + assertEquals(Arrays.asList("abc"), engine("x+abc$").requiredSuffixes()); + assertNull(engine("^ab+c|d").requiredPrefix()); + assertNull(engine("(?i)^abc+").requiredPrefix()); + assertNull(engine("[a-z]+$").requiredSuffixes()); + } + + @Test + void parserSharesOneMatcherForEachPattern() throws Exception { + final String json = + "{\"createdAt\":\"2024-04-17T19:40:53.716Z\",\"environment\":{\"name\":\"Test\"}," + + "\"flags\":{" + + flag("one", "^[a-z]+@site\\\\.test$") + + "," + + flag("two", "^[a-z]+@site\\\\.test$") + + "," + + flag("three", "@other\\\\.test$") + + "}}"; + final ServerConfiguration config = UniversalFlagConfigParser.parse(json.getBytes(UTF_8)); + final RegexMatcher one = regex(config, "one"); + assertSame(one, regex(config, "two")); + assertEquals(Kind.ENGINE, one.kind()); + assertEquals(Kind.ENDS_WITH, regex(config, "three").kind()); + } + + @Test + void parserDropsAFlagWithSyntaxThatRustRejects() throws Exception { + final String json = + "{\"createdAt\":\"2024-04-17T19:40:53.716Z\",\"environment\":{\"name\":\"Test\"}," + + "\"flags\":{" + + flag("lookahead", "(?=a)a") + + "," + + flag("valid", "@other\\\\.test$") + + "}}"; + final ServerConfiguration config = UniversalFlagConfigParser.parse(json.getBytes(UTF_8)); + assertFalse(config.flags.containsKey("lookahead")); + assertTrue(config.invalidFlags.containsKey("lookahead")); + assertEquals(Kind.ENDS_WITH, regex(config, "valid").kind()); + } + + private static String flag(final String key, final String pattern) { + return "\"" + + key + + "\":{\"key\":\"" + + key + + "\",\"enabled\":true,\"variationType\":\"BOOLEAN\"," + + "\"variations\":{\"on\":{\"key\":\"on\",\"value\":true}}," + + "\"allocations\":[{\"key\":\"a\",\"rules\":[{\"conditions\":[" + + "{\"attribute\":\"email\",\"operator\":\"MATCHES\",\"value\":\"" + + pattern + + "\"}]}],\"splits\":[{\"variationKey\":\"on\",\"shards\":[]}]}]}"; + } + + private static RegexMatcher regex(final ServerConfiguration config, final String key) { + return config.flags.get(key).allocations.get(0).rules.get(0).conditions.get(0).regex; + } + + private static RegexMatcher.Engine engine(final String pattern) { + return (RegexMatcher.Engine) RegexMatcher.compile(pattern); + } + + private static String repeat(final char c, final int n) { + final char[] chars = new char[n]; + Arrays.fill(chars, c); + return new String(chars); + } +} From 945a25303c45756623ff5781a9f223e4aea207c7 Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Mon, 16 Nov 2026 19:26:07 -0500 Subject: [PATCH 2/3] Address review findings in the regex matcher - Read each multi-character escape (\p{..}, \pX, \x{..}, \xHH, octal) as one token, so that its characters are not a required literal suffix. - Translate \d, \D, \s and \S to the Unicode classes of the Rust regex crate. Reject \Q, class set operations and nested classes, which the Rust crate rejects or reads differently from RE2/J. - Remove a - that is left without a flag after the u flag is removed. - Keep a surrogate pair together when a quantifier follows it. - Return an unmodifiable list of required suffixes. - Share the group and alternation helpers, remove the unused pattern field, and use matches() in place of an expectMatch parameter. - Move the parser tests to UniversalFlagConfigParserTest. - Add a JMH benchmark that compares java.util.regex with RegexMatcher. --- .../internal/ufc/RegexMatcherBenchmark.java | 104 ++++ .../internal/evaluation/FlagEvaluator.java | 11 +- .../internal/ufc/RegexMatcher.java | 542 +++++++++++------- .../internal/ufc/RegexMatcherTest.java | 256 ++++----- .../ufc/UniversalFlagConfigParserTest.java | 49 ++ 5 files changed, 610 insertions(+), 352 deletions(-) create mode 100644 dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/ufc/RegexMatcherBenchmark.java diff --git a/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/ufc/RegexMatcherBenchmark.java b/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/ufc/RegexMatcherBenchmark.java new file mode 100644 index 00000000000..94a64d7634d --- /dev/null +++ b/dd-openfeature/src/jmh/java/com/datadog/openfeature/internal/ufc/RegexMatcherBenchmark.java @@ -0,0 +1,104 @@ +package com.datadog.openfeature.internal.ufc; + +import static java.util.concurrent.TimeUnit.NANOSECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; + +import java.util.regex.Pattern; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; + +/** + * Compares {@code java.util.regex} {@code find()}, which the evaluator used before, with {@link + * RegexMatcher}, for each matcher tier. Each pattern shape comes from real UFC targeting rules. + */ +@State(Scope.Thread) +@Warmup(iterations = 3, time = 1, timeUnit = SECONDS) +@Measurement(iterations = 5, time = 1, timeUnit = SECONDS) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(NANOSECONDS) +@Fork(1) +public class RegexMatcherBenchmark { + /** The pattern shape. */ + @Param({"literal-suffix", "wildcard-alternation", "char-class", "complex", "word-boundary"}) + public String shape; + + /** A matching input, a short input that does not match, or a 2,000-character miss. */ + @Param({"hit", "miss", "long-miss"}) + public String input; + + private Pattern jdk; + private RegexMatcher linear; + private String value; + + @Setup + public void setup() { + final String pattern; + final String hit; + final String miss; + switch (shape) { + case "literal-suffix": + pattern = "@example\\.com$"; + hit = "jane.doe@example.com"; + miss = "jane.doe@example.org"; + break; + case "wildcard-alternation": + pattern = ".*@(site-a|site-b)\\.example$"; + hit = "jane.doe@site-b.example"; + miss = "jane.doe@site-c.example"; + break; + case "char-class": + pattern = "^[a-zA-Z0-9._%+-]+@site\\.test$"; + hit = "jane.doe+x@site.test"; + miss = "jane.doe+x@other.test"; + break; + case "complex": + pattern = "^u[0-9]{1,6}@(?:alpha|bravo|charlie)\\.(?:io|net)$"; + hit = "u12345@bravo.net"; + miss = "u12345@delta.net"; + break; + case "word-boundary": + pattern = "\\bma\u00f1ana\\b"; + hit = "llega ma\u00f1ana temprano"; + miss = "llega pronto"; + break; + default: + throw new IllegalArgumentException(shape); + } + jdk = Pattern.compile(pattern); + linear = RegexMatcher.compile(pattern); + switch (input) { + case "hit": + value = hit; + break; + case "miss": + value = miss; + break; + default: + final StringBuilder s = new StringBuilder(2_000 + miss.length()); + for (int i = 0; i < 2_000; i++) { + s.append('a'); + } + value = s.append(miss).toString(); + break; + } + } + + @Benchmark + public boolean javaUtilRegex() { + return jdk.matcher(value).find(); + } + + @Benchmark + public boolean regexMatcher() { + return linear.matches(value); + } +} diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java index 92d5b5306de..811ce7ff68c 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/evaluation/FlagEvaluator.java @@ -314,9 +314,9 @@ private static boolean evaluateCondition( switch (condition.operator) { case MATCHES: - return matchesRegex(attributeValue, condition, true); + return matchesRegex(attributeValue, condition); case NOT_MATCHES: - return matchesRegex(attributeValue, condition, false); + return !matchesRegex(attributeValue, condition); case ONE_OF: return isOneOf(attributeValue, condition.value); case NOT_ONE_OF: @@ -346,18 +346,15 @@ private static boolean evaluateCondition( } } - /** Evaluates MATCHES ({@code expectMatch} true) or NOT_MATCHES ({@code expectMatch} false). */ private static boolean matchesRegex( - final Object attributeValue, - final ConditionConfiguration condition, - final boolean expectMatch) { + final Object attributeValue, final ConditionConfiguration condition) { // Parsed configurations cache the validated matcher. Directly constructed configurations // retain the same syntax and error handling. final RegexMatcher regex = condition.regex != null ? condition.regex : ConditionConfiguration.compileRegex(condition.value); - return regex.test(String.valueOf(attributeValue), expectMatch); + return regex.matches(String.valueOf(attributeValue)); } private static boolean isOneOf(final Object attributeValue, final Object conditionValue) { diff --git a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/RegexMatcher.java b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/RegexMatcher.java index 3aa2b40b2c3..f939bfb783d 100644 --- a/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/RegexMatcher.java +++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/RegexMatcher.java @@ -1,8 +1,12 @@ package com.datadog.openfeature.internal.ufc; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; +import static java.util.Collections.unmodifiableList; + import java.util.ArrayList; -import java.util.Collections; import java.util.List; +import java.util.regex.PatternSyntaxException; import javax.annotation.Nullable; /** @@ -21,16 +25,27 @@ * finds the literal at Unicode word boundaries. *

  • RE2/J, a linear-time engine with the same syntax family as the Rust {@code regex} crate. It * does not backtrack, so a long input cannot block the thread. A literal prefix ({@code - * ^abc...}) or suffix ({@code ...abc$}) that every match needs is checked first. + * ^abc...}) or a set of literal suffixes ({@code ...(?:a|b)$}) that every match needs is + * checked first. * * - *

    The syntax and semantics are the RE2 and Rust ones, not the {@code java.util.regex} ones. For - * example, {@code $} matches only at the end of the input, and {@code .} matches any character - * except a line feed. + *

    The syntax and semantics are those of the Rust {@code regex} crate, which the libdatadog + * evaluator uses, not those of {@code java.util.regex}. For example, {@code $} matches only at the + * end of the input, {@code .} matches any character except a line feed, and {@code \d} and {@code + * \s} are Unicode classes. A pattern that the Rust crate rejects (lookaround, backreferences, + * atomic groups, {@code \Q...\E}) is invalid: {@link #compile} throws, and the parser drops the + * flag with a parse error, as libdatadog does. + * + *

    Known differences from the Rust crate: * - *

    A pattern that RE2/J rejects is invalid, also when {@code java.util.regex} accepts it - * (lookaround, backreferences, possessive quantifiers): {@link #compile} throws, and the parser - * drops the flag with a parse error. The Rust SDK evaluator (libdatadog) does the same. + *

    * *

    Instances are immutable and thread safe. */ @@ -50,143 +65,62 @@ public enum Kind { ENGINE } - private final String pattern; - - RegexMatcher(final String pattern) { - this.pattern = pattern; - } - - public String pattern() { - return pattern; - } + RegexMatcher() {} public abstract Kind kind(); /** Tells whether the pattern matches a part of the input. */ - abstract boolean find(String input); - - /** - * Evaluates the condition. - * - * @param input the attribute value. - * @param expectMatch {@code true} for MATCHES, {@code false} for NOT_MATCHES. - * @return whether the match result is {@code expectMatch}. - */ - public boolean test(final String input, final boolean expectMatch) { - return find(input) == expectMatch; - } + public abstract boolean matches(String input); /** * Compiles a pattern. * - * @throws java.util.regex.PatternSyntaxException if RE2/J rejects the pattern. + * @throws PatternSyntaxException if the pattern is invalid in the Rust {@code regex} dialect, or + * uses syntax that RE2/J reads differently. */ public static RegexMatcher compile(final String pattern) { - // The Rust regex crate has Unicode mode on by default, and accepts the u flag. RE2/J has no u - // flag, so remove it. - final String re2Pattern = withoutUnicodeFlag(pattern); + final String re2Pattern = Dialect.toRe2(pattern); final com.google.re2j.Pattern engine; try { engine = com.google.re2j.Pattern.compile(re2Pattern); } catch (final com.google.re2j.PatternSyntaxException e) { - throw new java.util.regex.PatternSyntaxException(e.getDescription(), pattern, e.getIndex()); + throw new PatternSyntaxException(e.getDescription(), pattern, e.getIndex()); } @Nullable final List tokens = Lexer.tokenize(re2Pattern); if (tokens == null) { - return new Engine(pattern, engine, null, null); + return new Engine(engine, null, null); } - @Nullable final RegexMatcher fast = FastPath.of(pattern, tokens); + @Nullable final RegexMatcher fast = FastPath.of(tokens); if (fast != null) { return fast; } - return new Engine( - pattern, engine, Prefilter.requiredPrefix(tokens), Prefilter.requiredSuffixes(tokens)); - } - - /** - * Removes the {@code u} flag from inline flag groups: {@code (?u)} is removed, {@code (?iu)} - * becomes {@code (?i)}, and {@code (?u:x)} becomes {@code (?:x)}. Classes and escapes are not - * changed. - */ - static String withoutUnicodeFlag(final String pattern) { - if (pattern.indexOf("(?") < 0) { - return pattern; - } - final StringBuilder out = new StringBuilder(pattern.length()); - int i = 0; - while (i < pattern.length()) { - final char c = pattern.charAt(i); - if (c == '\\' && i + 1 < pattern.length()) { - out.append(c).append(pattern.charAt(i + 1)); - i += 2; - } else if (c == '[') { - final int end = Lexer.classEnd(pattern, i); - final int stop = end < 0 ? pattern.length() : end + 1; - out.append(pattern, i, stop); - i = stop; - } else if (c == '(' && i + 1 < pattern.length() && pattern.charAt(i + 1) == '?') { - int j = i + 2; - final StringBuilder flags = new StringBuilder(); - while (j < pattern.length() - && (Character.isLetter(pattern.charAt(j)) || pattern.charAt(j) == '-')) { - if (pattern.charAt(j) != 'u') { - flags.append(pattern.charAt(j)); - } - j++; - } - final boolean flagGroup = - j > i + 2 - && j < pattern.length() - && (pattern.charAt(j) == ')' || pattern.charAt(j) == ':'); - if (!flagGroup) { - out.append(c); - i++; - } else if (pattern.charAt(j) == ')' && (flags.length() == 0 || "-".contentEquals(flags))) { - i = j + 1; - } else { - out.append("(?").append(flags).append(pattern.charAt(j)); - i = j + 1; - } - } else { - out.append(c); - i++; - } - } - return out.toString(); + return new Engine(engine, Prefilter.requiredPrefix(tokens), Prefilter.requiredSuffixes(tokens)); } // ---- Matchers --------------------------------------------------------------------------- /** The empty pattern, or {@code .*}: every input matches. */ static final class Always extends RegexMatcher { - Always(final String pattern) { - super(pattern); - } - @Override public Kind kind() { return Kind.ALWAYS; } @Override - boolean find(final String input) { + public boolean matches(final String input) { return true; } } /** {@code .+}: the input has a character other than a line feed. */ static final class AnyCharacter extends RegexMatcher { - AnyCharacter(final String pattern) { - super(pattern); - } - @Override public Kind kind() { return Kind.ANY_CHARACTER; } @Override - boolean find(final String input) { + public boolean matches(final String input) { for (int i = 0; i < input.length(); i++) { if (input.charAt(i) != '\n') { return true; @@ -201,8 +135,7 @@ static final class Literals extends RegexMatcher { private final Kind kind; private final String[] literals; - Literals(final String pattern, final Kind kind, final List literals) { - super(pattern); + Literals(final Kind kind, final List literals) { this.kind = kind; this.literals = literals.toArray(new String[0]); } @@ -213,11 +146,11 @@ public Kind kind() { } List literals() { - return Collections.unmodifiableList(java.util.Arrays.asList(literals)); + return unmodifiableList(asList(literals)); } @Override - boolean find(final String input) { + public boolean matches(final String input) { for (final String literal : literals) { final boolean hit; switch (kind) { @@ -251,9 +184,7 @@ static final class WordBoundary extends RegexMatcher { private final String[] literals; private final boolean trailingBoundary; - WordBoundary( - final String pattern, final List literals, final boolean trailingBoundary) { - super(pattern); + WordBoundary(final List literals, final boolean trailingBoundary) { this.literals = literals.toArray(new String[0]); this.trailingBoundary = trailingBoundary; } @@ -264,7 +195,7 @@ public Kind kind() { } @Override - boolean find(final String input) { + public boolean matches(final String input) { for (final String literal : literals) { for (int at = input.indexOf(literal); at >= 0; at = input.indexOf(literal, at + 1)) { if (isWordBoundary(input, at) @@ -310,11 +241,9 @@ static final class Engine extends RegexMatcher { @Nullable private final String[] requiredSuffixes; Engine( - final String pattern, final com.google.re2j.Pattern engine, @Nullable final String requiredPrefix, @Nullable final List requiredSuffixes) { - super(pattern); this.engine = engine; this.requiredPrefix = requiredPrefix; this.requiredSuffixes = @@ -333,11 +262,11 @@ String requiredPrefix() { @Nullable List requiredSuffixes() { - return requiredSuffixes == null ? null : java.util.Arrays.asList(requiredSuffixes); + return requiredSuffixes == null ? null : unmodifiableList(asList(requiredSuffixes)); } @Override - boolean find(final String input) { + public boolean matches(final String input) { if (requiredPrefix != null && !input.startsWith(requiredPrefix)) { return false; } @@ -357,6 +286,147 @@ private static boolean endsWithAny(final String input, final String[] suffixes) } } + // ---- Dialect ---------------------------------------------------------------------------- + + /** Translates a pattern from the Rust {@code regex} dialect to RE2/J syntax. */ + static final class Dialect { + /** The Unicode White_Space property, which is {@code \s} in the Rust crate. */ + static final String WHITE_SPACE = + "\\x{9}-\\x{D}\\x{20}\\x{85}\\x{A0}\\x{1680}\\x{2000}-\\x{200A}" + + "\\x{2028}\\x{2029}\\x{202F}\\x{205F}\\x{3000}"; + + private Dialect() {} + + /** + * Translates the pattern: + * + *

    + * + * @throws PatternSyntaxException for {@code \Q}, which the Rust crate rejects, and for class + * set operations and nested classes, which RE2/J reads as literal characters. + */ + static String toRe2(final String pattern) { + final StringBuilder out = new StringBuilder(pattern.length() + 16); + int i = 0; + while (i < pattern.length()) { + final char c = pattern.charAt(i); + if (c == '\\' && i + 1 < pattern.length()) { + i = escape(pattern, i, false, out); + } else if (c == '[') { + i = bracketClass(pattern, i, out); + } else if (c == '(' && i + 1 < pattern.length() && pattern.charAt(i + 1) == '?') { + i = flagGroup(pattern, i, out); + } else { + out.append(c); + i++; + } + } + return out.toString(); + } + + /** Copies the escape at {@code i} and returns the index after it. */ + private static int escape( + final String pattern, final int i, final boolean inClass, final StringBuilder out) { + final char e = pattern.charAt(i + 1); + switch (e) { + case 'Q': + throw new PatternSyntaxException("\\Q quoting is not supported", pattern, i); + case 'd': + out.append("\\p{Nd}"); + return i + 2; + case 's': + out.append(inClass ? WHITE_SPACE : "[" + WHITE_SPACE + "]"); + return i + 2; + case 'D': + out.append(inClass ? "\\D" : "\\P{Nd}"); + return i + 2; + case 'S': + out.append(inClass ? "\\S" : "[^" + WHITE_SPACE + "]"); + return i + 2; + default: + out.append('\\').append(e); + return i + 2; + } + } + + /** Copies the bracketed class at {@code start} and returns the index after it. */ + private static int bracketClass( + final String pattern, final int start, final StringBuilder out) { + final int end = Lexer.classEnd(pattern, start); + if (end < 0) { + // Unterminated: RE2/J reports the error. + out.append(pattern, start, pattern.length()); + return pattern.length(); + } + out.append('['); + int j = start + 1; + if (pattern.charAt(j) == '^') { + out.append('^'); + j++; + } + if (pattern.charAt(j) == ']') { + out.append(']'); + j++; + } + while (j < end) { + final char c = pattern.charAt(j); + if (c == '\\') { + j = escape(pattern, j, true, out); + } else if (c == '[' && pattern.charAt(j + 1) == ':') { + final int close = pattern.indexOf(":]", j + 2) + 2; + out.append(pattern, j, close); + j = close; + } else if (c == '[') { + throw new PatternSyntaxException("nested classes are not supported", pattern, j); + } else if ((c == '&' || c == '-' || c == '~') && pattern.charAt(j + 1) == c) { + throw new PatternSyntaxException("class set operations are not supported", pattern, j); + } else { + out.append(c); + j++; + } + } + out.append(']'); + return end + 1; + } + + /** + * Copies the group start at {@code i} without the {@code u} flag: {@code (?u)} is removed, + * {@code (?iu)} becomes {@code (?i)}, {@code (?i-u)} becomes {@code (?i)}, and {@code (?u:x)} + * becomes {@code (?:x)}. Returns the index after the copied part. + */ + private static int flagGroup(final String pattern, final int i, final StringBuilder out) { + int j = i + 2; + final StringBuilder flags = new StringBuilder(); + while (j < pattern.length() + && (Character.isLetter(pattern.charAt(j)) || pattern.charAt(j) == '-')) { + if (pattern.charAt(j) != 'u') { + flags.append(pattern.charAt(j)); + } + j++; + } + if (j == i + 2 + || j >= pattern.length() + || (pattern.charAt(j) != ')' && pattern.charAt(j) != ':')) { + // Not a flag group, for example (?: or (?P. + out.append("(?"); + return i + 2; + } + if (flags.length() > 0 && flags.charAt(flags.length() - 1) == '-') { + flags.setLength(flags.length() - 1); + } + if (pattern.charAt(j) == ')' && flags.length() == 0) { + return j + 1; + } + out.append("(?").append(flags).append(pattern.charAt(j)); + return j + 1; + } + } + // ---- Pattern analysis ------------------------------------------------------------------- /** One lexical token of a pattern. */ @@ -370,7 +440,9 @@ static final class Token { /** {@code (} or {@code (?:}. */ static final int GROUP = 2; - /** Any other construct: a class, or an escape such as {@code \d} or {@code \b}. */ + /** + * Any other construct: a class, or an escape such as {@code \d}, {@code \b} or {@code \x41}. + */ static final int OTHER = 3; final int type; @@ -414,7 +486,6 @@ static List tokenize(final String pattern) { return null; } final char e = pattern.charAt(i + 1); - i += 2; if (ESCAPABLE.indexOf(e) >= 0) { tokens.add(new Token(Token.LITERAL, e)); } else if (e == 't') { @@ -426,9 +497,11 @@ static List tokenize(final String pattern) { } else if (e == 'f') { tokens.add(new Token(Token.LITERAL, '\f')); } else { - // \d, \w, \s, \b, \A, \z, \p{..}, \x.., \Q...\E and others. + // \d, \w, \s, \b, \A, \z, \p{..}, \x.., octal and others: one token for the whole + // escape. tokens.add(new Token(Token.OTHER, e)); } + i = escapeEnd(pattern, i); } else if (c == '[') { final int end = classEnd(pattern, i); if (end < 0) { @@ -459,6 +532,38 @@ static List tokenize(final String pattern) { return tokens; } + /** + * Returns the index after the escape at {@code i}: {@code \p{..}}, {@code \x{..}}, {@code + * \xHH}, {@code \pX}, octal {@code \NNN}, or one escaped character. + */ + private static int escapeEnd(final String pattern, final int i) { + final char e = pattern.charAt(i + 1); + final int next = i + 2; + if ((e == 'p' || e == 'P' || e == 'x') + && next < pattern.length() + && pattern.charAt(next) == '{') { + final int close = pattern.indexOf('}', next); + return close < 0 ? pattern.length() : close + 1; + } + if (e == 'p' || e == 'P') { + return Math.min(next + 1, pattern.length()); + } + if (e == 'x') { + return Math.min(next + 2, pattern.length()); + } + if (e >= '0' && e <= '7') { + int j = next; + while (j < pattern.length() + && j < next + 2 + && pattern.charAt(j) >= '0' + && pattern.charAt(j) <= '7') { + j++; + } + return j; + } + return next; + } + /** Finds the {@code ]} that closes the class that starts at {@code start}, or -1. */ static int classEnd(final String pattern, final int start) { int j = start + 1; @@ -486,6 +591,76 @@ static int classEnd(final String pattern, final int start) { } return -1; } + + /** Splits on {@code |} outside groups. Returns {@code null} for unbalanced groups. */ + @Nullable + static List> splitTopLevel(final List tokens) { + final List> branches = new ArrayList<>(); + int depth = 0; + int start = 0; + for (int i = 0; i < tokens.size(); i++) { + final Token t = tokens.get(i); + if (t.type == Token.GROUP) { + depth++; + } else if (t.isMeta(')')) { + depth--; + if (depth < 0) { + return null; + } + } else if (depth == 0 && t.isMeta('|')) { + branches.add(tokens.subList(start, i)); + start = i + 1; + } + } + if (depth != 0) { + return null; + } + branches.add(tokens.subList(start, tokens.size())); + return branches; + } + + /** Tells whether the pattern is one branch with balanced groups. */ + static boolean isSingleBranch(final List tokens) { + @Nullable final List> branches = splitTopLevel(tokens); + return branches != null && branches.size() == 1; + } + + /** + * The literal alternatives of a group body (the tokens between {@code (} and {@code )}), or + * {@code null} for any other construct or for an empty alternative. + */ + @Nullable + static List alternatives(final List tokens, final int from, final int to) { + final List out = new ArrayList<>(); + StringBuilder current = new StringBuilder(); + for (int j = from; j < to; j++) { + final Token u = tokens.get(j); + if (u.type == Token.LITERAL) { + current.append(u.value); + } else if (u.isMeta('|')) { + out.add(current.toString()); + current = new StringBuilder(); + } else { + return null; + } + } + out.add(current.toString()); + return out.contains("") ? null : out; + } + + /** The index of the {@code )} that closes the group at {@code open}, if the body is flat. */ + static int flatGroupEnd(final List tokens, final int open) { + for (int j = open + 1; j < tokens.size(); j++) { + final Token t = tokens.get(j); + if (t.isMeta(')')) { + return j; + } + if (t.type == Token.GROUP) { + return -1; + } + } + return -1; + } } /** The literal fast paths, and the checks that need no engine. */ @@ -493,34 +668,34 @@ static final class FastPath { private FastPath() {} @Nullable - static RegexMatcher of(final String pattern, final List all) { + static RegexMatcher of(final List all) { List tokens = all; - if (tokens.isEmpty() || isDotStar(tokens, 0) == tokens.size()) { - return new Always(pattern); + if (tokens.isEmpty() || dotStarLength(tokens, 0) == tokens.size()) { + return new Always(); } if (isDotPlus(tokens)) { - return new AnyCharacter(pattern); + return new AnyCharacter(); } if (isWordBoundary(tokens.get(0))) { final boolean trailing = tokens.size() > 1 && isWordBoundary(tokens.get(tokens.size() - 1)); @Nullable final List literals = expand(tokens.subList(1, tokens.size() - (trailing ? 1 : 0)), false); - return literals == null ? null : new WordBoundary(pattern, literals, trailing); + return literals == null ? null : new WordBoundary(literals, trailing); } final boolean startAnchored = tokens.get(0).isMeta('^'); if (startAnchored) { tokens = tokens.subList(1, tokens.size()); } else { // find() already tries each start position, so a leading .* changes nothing. - tokens = tokens.subList(isDotStar(tokens, 0), tokens.size()); + tokens = tokens.subList(dotStarLength(tokens, 0), tokens.size()); } final boolean endAnchored = !tokens.isEmpty() && tokens.get(tokens.size() - 1).isMeta('$'); if (endAnchored) { tokens = tokens.subList(0, tokens.size() - 1); } else { // A trailing .* can match the empty string, so it changes nothing. - tokens = tokens.subList(0, tokens.size() - trailingDotStar(tokens)); + tokens = tokens.subList(0, tokens.size() - trailingDotStarLength(tokens)); } @Nullable final List literals = expand(tokens, !startAnchored && !endAnchored); if (literals == null) { @@ -530,7 +705,7 @@ static RegexMatcher of(final String pattern, final List all) { startAnchored ? (endAnchored ? Kind.EQUALS : Kind.STARTS_WITH) : (endAnchored ? Kind.ENDS_WITH : Kind.CONTAINS); - return new Literals(pattern, kind, literals); + return new Literals(kind, literals); } private static boolean isWordBoundary(final Token token) { @@ -538,7 +713,7 @@ private static boolean isWordBoundary(final Token token) { } /** The length of a {@code .*} or {@code .*?} at {@code index}, or 0. */ - private static int isDotStar(final List tokens, final int index) { + private static int dotStarLength(final List tokens, final int index) { if (index + 1 < tokens.size() && tokens.get(index).isMeta('.') && tokens.get(index + 1).isMeta('*')) { @@ -547,12 +722,12 @@ private static int isDotStar(final List tokens, final int index) { return 0; } - private static int trailingDotStar(final List tokens) { + private static int trailingDotStarLength(final List tokens) { final int n = tokens.size(); - if (n >= 3 && isDotStar(tokens, n - 3) == 3) { + if (n >= 3 && dotStarLength(tokens, n - 3) == 3) { return 3; } - if (n >= 2 && isDotStar(tokens, n - 2) == 2) { + if (n >= 2 && dotStarLength(tokens, n - 2) == 2) { return 2; } return 0; @@ -578,7 +753,7 @@ private static List expand( if (tokens.isEmpty()) { return null; } - final List> branches = splitTopLevel(tokens); + @Nullable final List> branches = Lexer.splitTopLevel(tokens); if (branches == null || (branches.size() > 1 && !topLevelAlternation)) { return null; } @@ -593,33 +768,6 @@ private static List expand( return out; } - /** Splits on {@code |} outside groups. Returns {@code null} for unbalanced groups. */ - @Nullable - private static List> splitTopLevel(final List tokens) { - final List> branches = new ArrayList<>(); - int depth = 0; - int start = 0; - for (int i = 0; i < tokens.size(); i++) { - final Token t = tokens.get(i); - if (t.type == Token.GROUP) { - depth++; - } else if (t.isMeta(')')) { - depth--; - if (depth < 0) { - return null; - } - } else if (depth == 0 && t.isMeta('|')) { - branches.add(tokens.subList(start, i)); - start = i + 1; - } - } - if (depth != 0) { - return null; - } - branches.add(tokens.subList(start, tokens.size())); - return branches; - } - @Nullable private static List expandBranch(final List tokens) { List product = new ArrayList<>(); @@ -636,25 +784,13 @@ private static List expandBranch(final List tokens) { } i++; } else if (t.type == Token.GROUP) { - final List alternatives = new ArrayList<>(); - StringBuilder current = new StringBuilder(); - int j = i + 1; - for (; j < tokens.size() && !tokens.get(j).isMeta(')'); j++) { - final Token u = tokens.get(j); - if (u.type == Token.LITERAL) { - current.append(u.value); - } else if (u.isMeta('|')) { - alternatives.add(current.toString()); - current = new StringBuilder(); - } else { - return null; - } - } - alternatives.add(current.toString()); - if (j >= tokens.size() || quantified(tokens, j + 1) || alternatives.contains("")) { - return null; - } - if (product.size() * alternatives.size() > MAX_LITERALS) { + final int close = Lexer.flatGroupEnd(tokens, i); + @Nullable + final List alternatives = + close < 0 ? null : Lexer.alternatives(tokens, i + 1, close); + if (alternatives == null + || quantified(tokens, close + 1) + || product.size() * alternatives.size() > MAX_LITERALS) { return null; } final List next = new ArrayList<>(product.size() * alternatives.size()); @@ -664,7 +800,7 @@ private static List expandBranch(final List tokens) { } } product = next; - i = j + 1; + i = close + 1; } else { return null; } @@ -694,7 +830,7 @@ private Prefilter() {} */ @Nullable static String requiredPrefix(final List tokens) { - if (tokens.isEmpty() || !tokens.get(0).isMeta('^') || hasTopLevelAlternation(tokens)) { + if (tokens.isEmpty() || !tokens.get(0).isMeta('^') || !Lexer.isSingleBranch(tokens)) { return null; } final StringBuilder prefix = new StringBuilder(); @@ -706,7 +842,13 @@ static String requiredPrefix(final List tokens) { && i < tokens.size() && tokens.get(i).isQuantifier() && !tokens.get(i).isMeta('+')) { - prefix.setLength(prefix.length() - 1); + // The quantifier applies to the whole last code point, which can be a surrogate pair. + final int last = prefix.length() - 1; + final boolean pair = + last > 0 + && Character.isLowSurrogate(prefix.charAt(last)) + && Character.isHighSurrogate(prefix.charAt(last - 1)); + prefix.setLength(pair ? last - 1 : last); } return prefix.length() == 0 ? null : prefix.toString(); } @@ -721,7 +863,7 @@ static String requiredPrefix(final List tokens) { @Nullable static List requiredSuffixes(final List tokens) { final int last = tokens.size() - 1; - if (last < 1 || !tokens.get(last).isMeta('$') || hasTopLevelAlternation(tokens)) { + if (last < 1 || !tokens.get(last).isMeta('$') || !Lexer.isSingleBranch(tokens)) { return null; } // The parts before $, from the last one back. Each part is a list of alternatives. @@ -730,7 +872,7 @@ static List requiredSuffixes(final List tokens) { while (i >= 0) { final Token t = tokens.get(i); if (t.type == Token.LITERAL) { - parts.add(Collections.singletonList(String.valueOf(t.value))); + parts.add(singletonList(String.valueOf(t.value))); i--; continue; } @@ -741,14 +883,15 @@ static List requiredSuffixes(final List tokens) { while (open >= 0 && tokens.get(open).type != Token.GROUP) { open--; } - @Nullable final List alternatives = alternatives(tokens, open + 1, i); - if (open < 0 || alternatives == null) { + @Nullable + final List alternatives = open < 0 ? null : Lexer.alternatives(tokens, open + 1, i); + if (alternatives == null) { break; } parts.add(alternatives); i = open - 1; } - List suffixes = Collections.singletonList(""); + List suffixes = singletonList(""); for (final List part : parts) { if (suffixes.size() * part.size() > MAX_LITERALS) { break; @@ -763,40 +906,5 @@ static List requiredSuffixes(final List tokens) { } return suffixes.get(0).isEmpty() ? null : suffixes; } - - /** The literal alternatives between two tokens, or {@code null} for any other construct. */ - @Nullable - private static List alternatives( - final List tokens, final int from, final int to) { - final List out = new ArrayList<>(); - StringBuilder current = new StringBuilder(); - for (int j = from; j < to; j++) { - final Token u = tokens.get(j); - if (u.type == Token.LITERAL) { - current.append(u.value); - } else if (u.isMeta('|')) { - out.add(current.toString()); - current = new StringBuilder(); - } else { - return null; - } - } - out.add(current.toString()); - return out.contains("") ? null : out; - } - - private static boolean hasTopLevelAlternation(final List tokens) { - int depth = 0; - for (final Token t : tokens) { - if (t.type == Token.GROUP) { - depth++; - } else if (t.isMeta(')')) { - depth--; - } else if (depth == 0 && t.isMeta('|')) { - return true; - } - } - return false; - } } } diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/RegexMatcherTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/RegexMatcherTest.java index 58b1609a9c8..4244f20b8a5 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/RegexMatcherTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/RegexMatcherTest.java @@ -1,10 +1,9 @@ package com.datadog.openfeature.internal.ufc; -import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Arrays.asList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -21,6 +20,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; class RegexMatcherTest { @@ -29,34 +29,36 @@ static Stream classifications() { Arguments.of("", Kind.ALWAYS, null), Arguments.of(".*", Kind.ALWAYS, null), Arguments.of(".+", Kind.ANY_CHARACTER, null), - Arguments.of("zach", Kind.CONTAINS, Arrays.asList("zach")), - Arguments.of("@example\\.com", Kind.CONTAINS, Arrays.asList("@example.com")), - Arguments.of("abc|def", Kind.CONTAINS, Arrays.asList("abc", "def")), - Arguments.of("^abc", Kind.STARTS_WITH, Arrays.asList("abc")), - Arguments.of("abc$", Kind.ENDS_WITH, Arrays.asList("abc")), - Arguments.of("^abc$", Kind.EQUALS, Arrays.asList("abc")), - Arguments.of(".*\\@(a|b)$", Kind.ENDS_WITH, Arrays.asList("@a", "@b")), + Arguments.of("zach", Kind.CONTAINS, asList("zach")), + Arguments.of("@example\\.com", Kind.CONTAINS, asList("@example.com")), + Arguments.of("abc|def", Kind.CONTAINS, asList("abc", "def")), + Arguments.of("^abc", Kind.STARTS_WITH, asList("abc")), + Arguments.of("abc$", Kind.ENDS_WITH, asList("abc")), + Arguments.of("^abc$", Kind.EQUALS, asList("abc")), + Arguments.of(".*\\@(a|b)$", Kind.ENDS_WITH, asList("@a", "@b")), Arguments.of( ".*@(alpha|bravo)\\.(io|net)$", Kind.ENDS_WITH, - Arrays.asList("@alpha.io", "@alpha.net", "@bravo.io", "@bravo.net")), - Arguments.of("(?:x|y)z.*", Kind.CONTAINS, Arrays.asList("xz", "yz")), - Arguments.of("^(us|eu)$", Kind.EQUALS, Arrays.asList("us", "eu")), - Arguments.of(".*?abc", Kind.CONTAINS, Arrays.asList("abc")), - Arguments.of("(?u)\\bma\u00f1ana\\b", Kind.WORD_BOUNDARY, null), + asList("@alpha.io", "@alpha.net", "@bravo.io", "@bravo.net")), + Arguments.of("(?:x|y)z.*", Kind.CONTAINS, asList("xz", "yz")), + Arguments.of("^(us|eu)$", Kind.EQUALS, asList("us", "eu")), + Arguments.of(".*?abc", Kind.CONTAINS, asList("abc")), + Arguments.of("(?u)\\bmañana\\b", Kind.WORD_BOUNDARY, null), Arguments.of("\\bfoo", Kind.WORD_BOUNDARY, null), Arguments.of("\\b(foo|bar)\\b", Kind.WORD_BOUNDARY, null), - Arguments.of("(?iu)abc", Kind.ENGINE, null), // Not literal: a dot, a class, a quantifier, flags, or an anchor next to .*. Arguments.of(".*@example.com", Kind.ENGINE, null), Arguments.of("^[a-zA-Z0-9._%+-]+@site\\.test$", Kind.ENGINE, null), Arguments.of("ab+c", Kind.ENGINE, null), Arguments.of("(?i)abc", Kind.ENGINE, null), + Arguments.of("(?iu)abc", Kind.ENGINE, null), Arguments.of("^.*abc", Kind.ENGINE, null), Arguments.of("abc.*$", Kind.ENGINE, null), Arguments.of("^a|b$", Kind.ENGINE, null), Arguments.of("(a|)b", Kind.ENGINE, null), - Arguments.of("\\d+", Kind.ENGINE, null)); + Arguments.of("\\d+", Kind.ENGINE, null), + Arguments.of("\\pN$", Kind.ENGINE, null), + Arguments.of("\\x41$", Kind.ENGINE, null)); } @ParameterizedTest @@ -73,7 +75,7 @@ void classifiesEachPattern(final String pattern, final Kind kind, final List patterns = - Arrays.asList( + asList( "", ".*", ".+", @@ -101,15 +103,22 @@ void fastPathsAgreeWithTheEngine() { "a\\$", "a\\|b", "\\(a\\)", - "tab\\there"); + "tab\\there", + "\\pL$", + "\\p{Lu}c$", + "\\x41$", + "\\x{41}bc$", + "^\\x41b", + "\\101$"); final List inputs = inputs(); for (final String pattern : patterns) { final RegexMatcher matcher = RegexMatcher.compile(pattern); - final com.google.re2j.Pattern engine = com.google.re2j.Pattern.compile(pattern); + final com.google.re2j.Pattern engine = + com.google.re2j.Pattern.compile(RegexMatcher.Dialect.toRe2(pattern)); for (final String input : inputs) { assertEquals( engine.matcher(input).find(), - matcher.find(input), + matcher.matches(input), "pattern=" + pattern + " input=" + input.replace("\n", "\\n")); } } @@ -118,7 +127,7 @@ void fastPathsAgreeWithTheEngine() { private static List inputs() { final List inputs = new ArrayList<>( - Arrays.asList( + asList( "", "\n", "\r", @@ -126,6 +135,9 @@ private static List inputs() { "abc", "abc\n", "xabcx", + "Abc", + "AAbc", + "Abbc", "def", "zach", "zachary", @@ -146,7 +158,7 @@ private static List inputs() { "x.com", "John.Doe+x@site.test", "john@site.test\n", - "bad char!@site.test", + "bad char@site.test", "abbbc", "ac", "abd", @@ -157,9 +169,13 @@ private static List inputs() { "(a)", "tab\there", "@example.com", - "x@example.com")); + "x@example.com", + "abc5", + "N", + "A", + "Éc")); final SplittableRandom rnd = new SplittableRandom(1); - final String alphabet = "abcdefxyz@.|$()\n\tuszach"; + final String alphabet = "abcdefxyzAN5@.|$()\n\tuszach"; for (int i = 0; i < 2000; i++) { final StringBuilder s = new StringBuilder(); final int length = rnd.nextInt(12); @@ -171,67 +187,91 @@ private static List inputs() { return inputs; } - /** The RE2 and Rust semantics, where they differ from java.util.regex. */ + /** Escapes longer than one character are not literal suffixes or prefixes. */ + @Test + void multiCharacterEscapesAreNotLiterals() { + assertTrue(RegexMatcher.compile("\\pN$").matches("abc5")); + assertTrue(RegexMatcher.compile("\\p{Lu}c$").matches("Ac")); + assertTrue(RegexMatcher.compile("\\x41$").matches("A")); + assertTrue(RegexMatcher.compile("\\x{41}bc$").matches("Abc")); + assertTrue(RegexMatcher.compile("\\101$").matches("A")); + assertEquals(asList("bc"), engine("\\x{41}bc$").requiredSuffixes()); + assertNull(engine("\\pN$").requiredSuffixes()); + } + + /** The Rust regex dialect, where it differs from java.util.regex and from plain RE2/J. */ @Test - void usesRe2Semantics() { + void usesTheRustDialect() { // $ matches only at the end of the input, not before a final line feed. - assertFalse(RegexMatcher.compile("a$").find("a\n")); - assertFalse(RegexMatcher.compile("^[a-z]+@site\\.test$").find("john@site.test\n")); + assertFalse(RegexMatcher.compile("a$").matches("a\n")); + assertFalse(RegexMatcher.compile("^[a-z]+@site\\.test$").matches("john@site.test\n")); // . matches any character except a line feed. - assertTrue(RegexMatcher.compile("^a.b$").find("a\rb")); - assertFalse(RegexMatcher.compile("^a.b$").find("a\nb")); - assertFalse(RegexMatcher.compile(".+").find("\n\n")); - assertTrue(RegexMatcher.compile(".+").find("\n\r")); - // RE2/J supports the bracketed POSIX classes. - assertTrue(RegexMatcher.compile("^[[:alnum:]]+$").find("abc123")); - assertFalse(RegexMatcher.compile("^[[:alnum:]]+$").find("abc-123")); + assertTrue(RegexMatcher.compile("^a.b$").matches("a\rb")); + assertFalse(RegexMatcher.compile("^a.b$").matches("a\nb")); + assertFalse(RegexMatcher.compile(".+").matches("\n\n")); + assertTrue(RegexMatcher.compile(".+").matches("\n\r")); + // Bracketed POSIX classes. + assertTrue(RegexMatcher.compile("^[[:alnum:]]+$").matches("abc123")); + assertFalse(RegexMatcher.compile("^[[:alnum:]]+$").matches("abc-123")); + // \d and \s are Unicode classes. + assertTrue(RegexMatcher.compile("^\\d+$").matches("١٢٣")); + assertTrue(RegexMatcher.compile("^[\\d-]+$").matches("١-2")); + assertFalse(RegexMatcher.compile("^\\D+$").matches("a١")); + assertTrue(RegexMatcher.compile("^a\\sb$").matches("a b")); + assertTrue(RegexMatcher.compile("^a[\\s]b$").matches("a b")); + assertFalse(RegexMatcher.compile("^\\S+$").matches("a
b")); } @Test void removesTheUnicodeFlag() { - assertEquals("abc", RegexMatcher.withoutUnicodeFlag("(?u)abc")); - assertEquals("(?i)abc", RegexMatcher.withoutUnicodeFlag("(?iu)abc")); - assertEquals("(?:abc)", RegexMatcher.withoutUnicodeFlag("(?u:abc)")); - assertEquals("(?:abc)", RegexMatcher.withoutUnicodeFlag("(?:abc)")); - assertEquals("[(?u)]x", RegexMatcher.withoutUnicodeFlag("[(?u)]x")); - assertEquals("\\(?u)", RegexMatcher.withoutUnicodeFlag("\\(?u)")); + assertEquals("abc", RegexMatcher.Dialect.toRe2("(?u)abc")); + assertEquals("(?i)abc", RegexMatcher.Dialect.toRe2("(?iu)abc")); + assertEquals("(?i)abc", RegexMatcher.Dialect.toRe2("(?i-u)abc")); + assertEquals("(?:abc)", RegexMatcher.Dialect.toRe2("(?u:abc)")); + assertEquals("(?:abc)", RegexMatcher.Dialect.toRe2("(?-u:abc)")); + assertEquals("(?:abc)", RegexMatcher.Dialect.toRe2("(?:abc)")); + assertEquals("(?Px)", RegexMatcher.Dialect.toRe2("(?Px)")); + assertEquals("[(?u)]x", RegexMatcher.Dialect.toRe2("[(?u)]x")); + assertEquals("\\(?u)", RegexMatcher.Dialect.toRe2("\\(?u)")); + assertTrue(RegexMatcher.compile("(?i-u)ABC").matches("abc")); } /** Word boundaries use the Rust (Unicode) definition of a word character. */ @Test void wordBoundaryIsUnicodeAware() { - final RegexMatcher manana = RegexMatcher.compile("(?u)\\bma\u00f1ana\\b"); - assertTrue(manana.find("llega ma\u00f1ana temprano")); - assertTrue(manana.find("ma\u00f1ana")); - assertFalse(manana.find("ma\u00f1anas")); + final RegexMatcher manana = RegexMatcher.compile("(?u)\\bmañana\\b"); + assertTrue(manana.matches("llega mañana temprano")); + assertTrue(manana.matches("mañana")); + assertFalse(manana.matches("mañanas")); // A non-ASCII letter is a word character, so there is no boundary before "m". - assertFalse(manana.find("\u00f1ma\u00f1ana")); + assertFalse(manana.matches("ñmañana")); final RegexMatcher prefix = RegexMatcher.compile("\\bfoo"); - assertTrue(prefix.find("a foobar")); - assertFalse(prefix.find("afoo")); - assertFalse(prefix.find("\u00e9foo")); - assertTrue(prefix.find("-foo")); + assertTrue(prefix.matches("a foobar")); + assertFalse(prefix.matches("afoo")); + assertFalse(prefix.matches("éfoo")); + assertTrue(prefix.matches("-foo")); } - @Test - void matchesAndNotMatchesAreOpposites() { - final RegexMatcher matcher = RegexMatcher.compile("@example\\.com$"); - assertTrue(matcher.test("a@example.com", true)); - assertFalse(matcher.test("a@example.com", false)); - assertFalse(matcher.test("a@other.com", true)); - assertTrue(matcher.test("a@other.com", false)); - } - - @Test - void malformedPatternThrows() { - assertThrows(PatternSyntaxException.class, () -> RegexMatcher.compile("*@datadoghq.com")); - assertThrows(PatternSyntaxException.class, () -> RegexMatcher.compile("(abc")); - assertThrows(PatternSyntaxException.class, () -> RegexMatcher.compile("[abc")); - // java.util.regex accepts these, but the Rust regex crate and RE2/J do not. - for (final String pattern : - Arrays.asList("(?=a)a", "(?a)", "\\p{Alnum}")) { - assertThrows(PatternSyntaxException.class, () -> RegexMatcher.compile(pattern), pattern); - } + @ParameterizedTest + @ValueSource( + strings = { + "*@datadoghq.com", + "(abc", + "[abc", + // java.util.regex accepts these, but the Rust regex crate does not. + "(?=a)a", + "(?a)", + "\\p{Alnum}", + "\\Qa.b\\E", + // RE2/J reads these as literal characters, but the Rust regex crate does not. + "[a-z&&[^aeiou]]", + "[[a-z]--[aeiou]]", + "[a~~b]" + }) + void rejectsInvalidPatterns(final String pattern) { + assertThrows(PatternSyntaxException.class, () -> RegexMatcher.compile(pattern), pattern); } /** Patterns that make java.util.regex backtrack finish in linear time. */ @@ -241,84 +281,44 @@ void backtrackingPatternsFinishFast() { assertTimeoutPreemptively( Duration.ofSeconds(2), () -> { - assertFalse(RegexMatcher.compile("(a+)+$").find(as + "!")); - assertFalse(RegexMatcher.compile("^(a|aa)+$").find(as + "!")); - assertFalse(RegexMatcher.compile("(a*)*b").find(as)); - assertFalse(RegexMatcher.compile(".*a.*a.*a.*a.*b").find(as)); - assertFalse(RegexMatcher.compile(".*@(x|y)\\.com$").find(as + "@z.com")); - assertFalse(RegexMatcher.compile("^[a-z]+@site\\.test$").find(as + "@other.test")); + assertFalse(RegexMatcher.compile("(a+)+$").matches(as + "!")); + assertFalse(RegexMatcher.compile("^(a|aa)+$").matches(as + "!")); + assertFalse(RegexMatcher.compile("(a*)*b").matches(as)); + assertFalse(RegexMatcher.compile(".*a.*a.*a.*a.*b").matches(as)); + assertFalse(RegexMatcher.compile(".*.*.*.*.*=").matches(as)); + assertFalse(RegexMatcher.compile(".*@(x|y)\\.com$").matches(as + "@z.com")); + assertFalse(RegexMatcher.compile("^[a-z]+@site\\.test$").matches(as + "@other.test")); }); } @Test - void findsRequiredPrefixAndSuffix() { + void findsRequiredPrefixAndSuffixes() { assertEquals( - Arrays.asList("@site.test"), engine("^[a-zA-Z0-9._%+-]+@site\\.test$").requiredSuffixes()); + asList("@site.test"), engine("^[a-zA-Z0-9._%+-]+@site\\.test$").requiredSuffixes()); assertEquals( - Arrays.asList("@alpha.io", "@alpha.net", "@bravo.io", "@bravo.net"), + asList("@alpha.io", "@alpha.net", "@bravo.io", "@bravo.net"), engine("^u[0-9]{1,3}@(?:alpha|bravo)\\.(?:io|net)$").requiredSuffixes()); // A quantified group is not a required suffix; only the text after it is. - assertEquals(Arrays.asList(".com"), engine("^x(?:a|b)+\\.com$").requiredSuffixes()); + assertEquals(asList(".com"), engine("^x(?:a|b)+\\.com$").requiredSuffixes()); + assertEquals(asList("abc"), engine("x+abc$").requiredSuffixes()); + assertNull(engine("[a-z]+$").requiredSuffixes()); assertNull(engine("^[a-zA-Z0-9._%+-]+@site\\.test$").requiredPrefix()); assertEquals("ab", engine("^ab+c").requiredPrefix()); assertEquals("a", engine("^ab?c").requiredPrefix()); assertEquals("ab", engine("^abc*d").requiredPrefix()); // {n,m} can allow zero repetitions, so the quantified character is not required. assertNull(engine("^a{2}b").requiredPrefix()); - assertEquals(Arrays.asList("abc"), engine("x+abc$").requiredSuffixes()); assertNull(engine("^ab+c|d").requiredPrefix()); assertNull(engine("(?i)^abc+").requiredPrefix()); - assertNull(engine("[a-z]+$").requiredSuffixes()); - } - - @Test - void parserSharesOneMatcherForEachPattern() throws Exception { - final String json = - "{\"createdAt\":\"2024-04-17T19:40:53.716Z\",\"environment\":{\"name\":\"Test\"}," - + "\"flags\":{" - + flag("one", "^[a-z]+@site\\\\.test$") - + "," - + flag("two", "^[a-z]+@site\\\\.test$") - + "," - + flag("three", "@other\\\\.test$") - + "}}"; - final ServerConfiguration config = UniversalFlagConfigParser.parse(json.getBytes(UTF_8)); - final RegexMatcher one = regex(config, "one"); - assertSame(one, regex(config, "two")); - assertEquals(Kind.ENGINE, one.kind()); - assertEquals(Kind.ENDS_WITH, regex(config, "three").kind()); + // A quantifier applies to a whole surrogate pair. + assertEquals("a", engine("^a😀?\\d").requiredPrefix()); + assertTrue(RegexMatcher.compile("^a😀?\\d").matches("a1")); } @Test - void parserDropsAFlagWithSyntaxThatRustRejects() throws Exception { - final String json = - "{\"createdAt\":\"2024-04-17T19:40:53.716Z\",\"environment\":{\"name\":\"Test\"}," - + "\"flags\":{" - + flag("lookahead", "(?=a)a") - + "," - + flag("valid", "@other\\\\.test$") - + "}}"; - final ServerConfiguration config = UniversalFlagConfigParser.parse(json.getBytes(UTF_8)); - assertFalse(config.flags.containsKey("lookahead")); - assertTrue(config.invalidFlags.containsKey("lookahead")); - assertEquals(Kind.ENDS_WITH, regex(config, "valid").kind()); - } - - private static String flag(final String key, final String pattern) { - return "\"" - + key - + "\":{\"key\":\"" - + key - + "\",\"enabled\":true,\"variationType\":\"BOOLEAN\"," - + "\"variations\":{\"on\":{\"key\":\"on\",\"value\":true}}," - + "\"allocations\":[{\"key\":\"a\",\"rules\":[{\"conditions\":[" - + "{\"attribute\":\"email\",\"operator\":\"MATCHES\",\"value\":\"" - + pattern - + "\"}]}],\"splits\":[{\"variationKey\":\"on\",\"shards\":[]}]}]}"; - } - - private static RegexMatcher regex(final ServerConfiguration config, final String key) { - return config.flags.get(key).allocations.get(0).rules.get(0).conditions.get(0).regex; + void requiredSuffixesCannotBeChanged() { + final List suffixes = engine("^[a-z]+@(?:a|b)$").requiredSuffixes(); + assertThrows(UnsupportedOperationException.class, () -> suffixes.set(0, "x")); } private static RegexMatcher.Engine engine(final String pattern) { diff --git a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParserTest.java b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParserTest.java index 5afe68fa544..5fb93738d58 100644 --- a/dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParserTest.java +++ b/dd-openfeature/src/test/java/com/datadog/openfeature/internal/ufc/UniversalFlagConfigParserTest.java @@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -447,6 +448,54 @@ void readsNumericAllocationDatesAsUnbounded() throws Exception { assertTrue(config.invalidFlags.isEmpty()); } + @Test + void sharesOneRegexMatcherForEachPattern() throws Exception { + final ServerConfiguration config = + deserialize( + regexConfig( + regexFlag("one", "^[a-z]+@site\\\\.test$"), + regexFlag("two", "^[a-z]+@site\\\\.test$"), + regexFlag("three", "@other\\\\.test$"))); + final RegexMatcher one = regex(config, "one"); + assertSame(one, regex(config, "two")); + assertEquals(RegexMatcher.Kind.ENGINE, one.kind()); + assertEquals(RegexMatcher.Kind.ENDS_WITH, regex(config, "three").kind()); + } + + @Test + void dropsAFlagWithRegexSyntaxThatRustRejects() throws Exception { + final ServerConfiguration config = + deserialize( + regexConfig(regexFlag("lookahead", "(?=a)a"), regexFlag("valid", "@other\\\\.test$"))); + assertFalse(config.flags.containsKey("lookahead")); + assertTrue(config.invalidFlags.containsKey("lookahead")); + assertEquals(RegexMatcher.Kind.ENDS_WITH, regex(config, "valid").kind()); + } + + private static String regexConfig(final String... flags) { + return "{\"createdAt\":\"2024-04-17T19:40:53.716Z\",\"environment\":{\"name\":\"Test\"}," + + "\"flags\":{" + + String.join(",", flags) + + "}}"; + } + + private static String regexFlag(final String key, final String pattern) { + return "\"" + + key + + "\":{\"key\":\"" + + key + + "\",\"enabled\":true,\"variationType\":\"BOOLEAN\"," + + "\"variations\":{\"on\":{\"key\":\"on\",\"value\":true}}," + + "\"allocations\":[{\"key\":\"a\",\"rules\":[{\"conditions\":[" + + "{\"attribute\":\"email\",\"operator\":\"MATCHES\",\"value\":\"" + + pattern + + "\"}]}],\"splits\":[{\"variationKey\":\"on\",\"shards\":[]}],\"doLog\":true}]}"; + } + + private static RegexMatcher regex(final ServerConfiguration config, final String key) { + return config.flags.get(key).allocations.get(0).rules.get(0).conditions.get(0).regex; + } + private static ServerConfiguration deserialize(final String json) throws Exception { return UniversalFlagConfigParser.parse(json.getBytes(UTF_8)); } From a571f3a8ee96f7fd5d54acbfc989eb178d394f5e Mon Sep 17 00:00:00 2001 From: Leo Romanovsky Date: Tue, 17 Nov 2026 00:59:31 +0000 Subject: [PATCH 3/3] Benchmark the regex suite with the linear-time matcher The base scale benchmark cannot run the regex suite, because the JDK engine does not finish its backtracking cases. With the linear-time matcher it does, so add its literal and backtracking case files. Co-Authored-By: Claude Opus 5.5 --- .../java/com/datadog/openfeature/ScaleSuiteBenchmark.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dd-openfeature/src/jmh/java/com/datadog/openfeature/ScaleSuiteBenchmark.java b/dd-openfeature/src/jmh/java/com/datadog/openfeature/ScaleSuiteBenchmark.java index 5efa3df3752..e69ec2942d7 100644 --- a/dd-openfeature/src/jmh/java/com/datadog/openfeature/ScaleSuiteBenchmark.java +++ b/dd-openfeature/src/jmh/java/com/datadog/openfeature/ScaleSuiteBenchmark.java @@ -33,7 +33,9 @@ * Evaluation cost on the opt-in scale suites of the shared FFE fixtures ({@code * evaluation-suites/scale} and {@code evaluation-suites/scale-json}): large lists, regex, rules, * splits and shards, time windows, many variations and flags, and JSON values up to 10 MB. The - * dimensions are listed in {@code evaluation-suites/scale/AGENTS.md} of the fixtures. + * dimensions are listed in {@code evaluation-suites/scale/AGENTS.md} of the fixtures. The {@code + * regex} suite adds literal patterns and inputs that make a backtracking engine slow; it needs the + * linear-time matcher. * *

    Each operation evaluates a case of the selected case file through the provider. With {@code * index=all}, the operations go through the cases of the file in turn, so a row is the mean of its @@ -73,7 +75,9 @@ public class ScaleSuiteBenchmark { "scale/wide-context", "scale-json/json-shapes", "scale-json/json-sizes", - "scale-json/json-values" + "scale-json/json-values", + "regex/backtracking", + "regex/literals" }) public String cases;