[libc-commits] [libc] [libc][regex] Add ERE {m, n} interval quantifiers (PR #236569)

Alex Strelnikov via libc-commits libc-commits at lists.llvm.org
Wed Nov 11 09:47:29 PST 2026


================
@@ -283,10 +337,115 @@ ErrorOr<ExprId> ExprPool::make_concat(ExprId l, ExprId r) {
       return tail;
     return make_concat(le.bin.left, *tail);
   }
-  return intern(Expr::make_concat(l, r, is_nullable(l) && is_nullable(r),
-                                  has_anchors(l) || has_anchors(r)));
+  const Expr re = get(r);
+  return intern(Expr::make_concat(
+      l, r, le.nullable && re.nullable, le.has_anchors || re.has_anchors,
+      le.has_repeat || re.has_repeat,
+      cpp::max(le.rep_expansion, re.rep_expansion)));
 }
 
+namespace {
+
+// Coalesces R{m1,n1} | R{m2,n2} (and shared-prefix/shared-suffix Concat nodes)
+// when their repetition intervals overlap or are adjacent.  Returns
+// INVALID_EXPR_ID when the pair cannot be coalesced.
+ErrorOr<ExprId> coalesce_repeat_pair(ExprId a, ExprId b, ExprPool &pool) {
+  const Expr ae = pool.get(a);
+  const Expr be = pool.get(b);
+  if (!ae.has_repeat && !be.has_repeat)
+    return INVALID_EXPR_ID;
+  if (ae.kind == ExprKind::Concat && be.kind == ExprKind::Concat) {
+    if (ae.bin.right == be.bin.right) {
+      ErrorOr<ExprId> left =
+          coalesce_repeat_pair(ae.bin.left, be.bin.left, pool);
+      if (!left.has_value() || *left == INVALID_EXPR_ID)
+        return left;
+      return pool.make_concat(*left, ae.bin.right);
+    }
+    if (ae.bin.left == be.bin.left) {
+      ErrorOr<ExprId> right =
+          coalesce_repeat_pair(ae.bin.right, be.bin.right, pool);
+      if (!right.has_value() || *right == INVALID_EXPR_ID)
+        return right;
+      return pool.make_concat(ae.bin.left, *right);
+    }
+  }
+  // Handle Concat(X, S) | S and Concat(P, X) | P where R{0,0} normalized to
+  // EmptyStr on one side.
+  if (ae.kind == ExprKind::Concat && ae.bin.right == b) {
+    ErrorOr<ExprId> left =
+        coalesce_repeat_pair(ae.bin.left, EMPTY_STR_ID, pool);
+    if (!left.has_value() || *left == INVALID_EXPR_ID)
+      return left;
+    return pool.make_concat(*left, b);
+  }
+  if (be.kind == ExprKind::Concat && be.bin.right == a) {
+    ErrorOr<ExprId> left =
+        coalesce_repeat_pair(EMPTY_STR_ID, be.bin.left, pool);
+    if (!left.has_value() || *left == INVALID_EXPR_ID)
+      return left;
+    return pool.make_concat(*left, a);
+  }
----------------
strel-12 wrote:

This appears to handle `Concat(X, S) | S` (and the mirrored `S | Concat(X, S)`), but not `Concat(P, X) | P` (and the mirrored `P | Concat(P, X)`) as the comment above suggests?

https://github.com/llvm/llvm-project/pull/236569


More information about the libc-commits mailing list