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/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;
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 55ba7dc45ce..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
@@ -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;
@@ -348,13 +348,13 @@ private static boolean evaluateCondition(
private static boolean matchesRegex(
final Object attributeValue, final ConditionConfiguration condition) {
- // Parsed configurations cache the validated pattern. Directly constructed configurations
+ // 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.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/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..f939bfb783d
--- /dev/null
+++ b/dd-openfeature/src/main/java/com/datadog/openfeature/internal/ufc/RegexMatcher.java
@@ -0,0 +1,910 @@
+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.List;
+import java.util.regex.PatternSyntaxException;
+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:
+ *
+ *
+ * - 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$}.
+ *
- 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.
+ *
- 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 a set of literal suffixes ({@code ...(?:a|b)$}) that every match needs is
+ * checked first.
+ *
+ *
+ * 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:
+ *
+ *
+ * - {@code \w}, {@code \W}, {@code \b} and {@code \B} use ASCII word characters in the RE2/J
+ * tier. Only the {@code \bliteral} fast path uses Unicode word characters.
+ *
- {@code \D} and {@code \S} inside a bracketed class are ASCII classes.
+ *
- Class set operations ({@code &&}, {@code --}, {@code ~~}), nested classes, the {@code x}
+ * and {@code R} flags, {@code \<}, {@code \>}, {@code \b{...}}, and nested repetitions such
+ * as {@code a*+} are a parse error here; the Rust crate accepts them.
+ *
+ *
+ * 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
+ }
+
+ RegexMatcher() {}
+
+ public abstract Kind kind();
+
+ /** Tells whether the pattern matches a part of the input. */
+ public abstract boolean matches(String input);
+
+ /**
+ * Compiles a 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) {
+ 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 PatternSyntaxException(e.getDescription(), pattern, e.getIndex());
+ }
+ @Nullable final List tokens = Lexer.tokenize(re2Pattern);
+ if (tokens == null) {
+ return new Engine(engine, null, null);
+ }
+ @Nullable final RegexMatcher fast = FastPath.of(tokens);
+ if (fast != null) {
+ return fast;
+ }
+ 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 {
+ @Override
+ public Kind kind() {
+ return Kind.ALWAYS;
+ }
+
+ @Override
+ 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 {
+ @Override
+ public Kind kind() {
+ return Kind.ANY_CHARACTER;
+ }
+
+ @Override
+ public boolean matches(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 Kind kind, final List literals) {
+ this.kind = kind;
+ this.literals = literals.toArray(new String[0]);
+ }
+
+ @Override
+ public Kind kind() {
+ return kind;
+ }
+
+ List literals() {
+ return unmodifiableList(asList(literals));
+ }
+
+ @Override
+ public boolean matches(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 List literals, final boolean trailingBoundary) {
+ this.literals = literals.toArray(new String[0]);
+ this.trailingBoundary = trailingBoundary;
+ }
+
+ @Override
+ public Kind kind() {
+ return Kind.WORD_BOUNDARY;
+ }
+
+ @Override
+ 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)
+ && (!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 com.google.re2j.Pattern engine,
+ @Nullable final String requiredPrefix,
+ @Nullable final List requiredSuffixes) {
+ 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 : unmodifiableList(asList(requiredSuffixes));
+ }
+
+ @Override
+ public boolean matches(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;
+ }
+ }
+
+ // ---- 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:
+ *
+ *
+ * - {@code \d} and {@code \D} become {@code \p{Nd}} and {@code \P{Nd}}, and {@code \s} and
+ * {@code \S} become the White_Space class, as in the Rust crate. RE2/J classes are ASCII.
+ *
- The {@code u} flag is removed: Unicode mode is the default in the Rust crate, and RE2/J
+ * has no {@code u} flag.
+ *
+ *
+ * @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. */
+ 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}, {@code \b} or {@code \x41}.
+ */
+ 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);
+ 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.., 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) {
+ 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;
+ }
+
+ /**
+ * 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;
+ 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;
+ }
+
+ /** 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. */
+ static final class FastPath {
+ private FastPath() {}
+
+ @Nullable
+ static RegexMatcher of(final List all) {
+ List tokens = all;
+ if (tokens.isEmpty() || dotStarLength(tokens, 0) == tokens.size()) {
+ return new Always();
+ }
+ if (isDotPlus(tokens)) {
+ 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(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(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() - trailingDotStarLength(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(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 dotStarLength(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 trailingDotStarLength(final List tokens) {
+ final int n = tokens.size();
+ if (n >= 3 && dotStarLength(tokens, n - 3) == 3) {
+ return 3;
+ }
+ if (n >= 2 && dotStarLength(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;
+ }
+ @Nullable final List> branches = Lexer.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;
+ }
+
+ @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 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());
+ for (final StringBuilder prefix : product) {
+ for (final String alternative : alternatives) {
+ next.add(new StringBuilder(prefix).append(alternative));
+ }
+ }
+ product = next;
+ i = close + 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('^') || !Lexer.isSingleBranch(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('+')) {
+ // 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();
+ }
+
+ /**
+ * 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('$') || !Lexer.isSingleBranch(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(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 = open < 0 ? null : Lexer.alternatives(tokens, open + 1, i);
+ if (alternatives == null) {
+ break;
+ }
+ parts.add(alternatives);
+ i = open - 1;
+ }
+ List suffixes = 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;
+ }
+ }
+}
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..4244f20b8a5
--- /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.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.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;
+import org.junit.jupiter.params.provider.ValueSource;
+
+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, 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,
+ 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),
+ // 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("\\pN$", Kind.ENGINE, null),
+ Arguments.of("\\x41$", 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 =
+ 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",
+ "\\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(RegexMatcher.Dialect.toRe2(pattern));
+ for (final String input : inputs) {
+ assertEquals(
+ engine.matcher(input).find(),
+ matcher.matches(input),
+ "pattern=" + pattern + " input=" + input.replace("\n", "\\n"));
+ }
+ }
+ }
+
+ private static List inputs() {
+ final List inputs =
+ new ArrayList<>(
+ asList(
+ "",
+ "\n",
+ "\r",
+ "a\n",
+ "abc",
+ "abc\n",
+ "xabcx",
+ "Abc",
+ "AAbc",
+ "Abbc",
+ "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",
+ "abc5",
+ "N",
+ "A",
+ "Éc"));
+ final SplittableRandom rnd = new SplittableRandom(1);
+ final String alphabet = "abcdefxyzAN5@.|$()\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;
+ }
+
+ /** 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 usesTheRustDialect() {
+ // $ matches only at the end of the input, not before a final line feed.
+ 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$").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.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ñ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.matches("ñmañana"));
+ final RegexMatcher prefix = RegexMatcher.compile("\\bfoo");
+ assertTrue(prefix.matches("a foobar"));
+ assertFalse(prefix.matches("afoo"));
+ assertFalse(prefix.matches("éfoo"));
+ assertTrue(prefix.matches("-foo"));
+ }
+
+ @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. */
+ @Test
+ void backtrackingPatternsFinishFast() {
+ final String as = repeat('a', 5_000);
+ assertTimeoutPreemptively(
+ Duration.ofSeconds(2),
+ () -> {
+ 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 findsRequiredPrefixAndSuffixes() {
+ assertEquals(
+ asList("@site.test"), engine("^[a-zA-Z0-9._%+-]+@site\\.test$").requiredSuffixes());
+ assertEquals(
+ 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(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());
+ assertNull(engine("^ab+c|d").requiredPrefix());
+ assertNull(engine("(?i)^abc+").requiredPrefix());
+ // A quantifier applies to a whole surrogate pair.
+ assertEquals("a", engine("^a😀?\\d").requiredPrefix());
+ assertTrue(RegexMatcher.compile("^a😀?\\d").matches("a1"));
+ }
+
+ @Test
+ 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) {
+ 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);
+ }
+}
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));
}