From fc7660d0b58bb4206f4b9b7b28b17c60d8a691a3 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Tue, 4 Nov 2025 09:48:20 -0800 Subject: [PATCH] Add missing string replace operations to Java API (#8011) * Initial plan * Add C API and Java bindings for str.replace_all, str.replace_re, str.replace_all_re Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com> * Add test for new Java string replace operations Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com> * Remove author field from test file header Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com> * Delete examples/java/StringReplaceTest.java --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com> Co-authored-by: Nikolaj Bjorner --- src/api/api_seq.cpp | 3 +++ src/api/java/Context.java | 27 +++++++++++++++++++++++++++ src/api/z3_api.h | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/src/api/api_seq.cpp b/src/api/api_seq.cpp index 2b87ef290..cf199af41 100644 --- a/src/api/api_seq.cpp +++ b/src/api/api_seq.cpp @@ -293,6 +293,9 @@ extern "C" { MK_TERNARY(Z3_mk_seq_extract, mk_c(c)->get_seq_fid(), OP_SEQ_EXTRACT, SKIP); MK_TERNARY(Z3_mk_seq_replace, mk_c(c)->get_seq_fid(), OP_SEQ_REPLACE, SKIP); + MK_TERNARY(Z3_mk_seq_replace_all, mk_c(c)->get_seq_fid(), OP_SEQ_REPLACE_ALL, SKIP); + MK_TERNARY(Z3_mk_seq_replace_re, mk_c(c)->get_seq_fid(), OP_SEQ_REPLACE_RE, SKIP); + MK_TERNARY(Z3_mk_seq_replace_re_all, mk_c(c)->get_seq_fid(), OP_SEQ_REPLACE_RE_ALL, SKIP); MK_BINARY(Z3_mk_seq_at, mk_c(c)->get_seq_fid(), OP_SEQ_AT, SKIP); MK_BINARY(Z3_mk_seq_nth, mk_c(c)->get_seq_fid(), OP_SEQ_NTH, SKIP); MK_UNARY(Z3_mk_seq_length, mk_c(c)->get_seq_fid(), OP_SEQ_LENGTH, SKIP); diff --git a/src/api/java/Context.java b/src/api/java/Context.java index d2e26334b..9a8218537 100644 --- a/src/api/java/Context.java +++ b/src/api/java/Context.java @@ -2244,6 +2244,33 @@ public class Context implements AutoCloseable { return (SeqExpr) Expr.create(this, Native.mkSeqReplace(nCtx(), s.getNativeObject(), src.getNativeObject(), dst.getNativeObject())); } + /** + * Replace all occurrences of src by dst in s. + */ + public final SeqExpr mkReplaceAll(Expr> s, Expr> src, Expr> dst) + { + checkContextMatch(s, src, dst); + return (SeqExpr) Expr.create(this, Native.mkSeqReplaceAll(nCtx(), s.getNativeObject(), src.getNativeObject(), dst.getNativeObject())); + } + + /** + * Replace the first occurrence of regular expression re with dst in s. + */ + public final SeqExpr mkReplaceRe(Expr> s, ReExpr> re, Expr> dst) + { + checkContextMatch(s, re, dst); + return (SeqExpr) Expr.create(this, Native.mkSeqReplaceRe(nCtx(), s.getNativeObject(), re.getNativeObject(), dst.getNativeObject())); + } + + /** + * Replace all occurrences of regular expression re with dst in s. + */ + public final SeqExpr mkReplaceReAll(Expr> s, ReExpr> re, Expr> dst) + { + checkContextMatch(s, re, dst); + return (SeqExpr) Expr.create(this, Native.mkSeqReplaceReAll(nCtx(), s.getNativeObject(), re.getNativeObject(), dst.getNativeObject())); + } + /** * Convert a regular expression that accepts sequence s. */ diff --git a/src/api/z3_api.h b/src/api/z3_api.h index baa2fa34c..c5d3933ca 100644 --- a/src/api/z3_api.h +++ b/src/api/z3_api.h @@ -3800,6 +3800,27 @@ extern "C" { */ Z3_ast Z3_API Z3_mk_seq_replace(Z3_context c, Z3_ast s, Z3_ast src, Z3_ast dst); + /** + \brief Replace all occurrences of \c src with \c dst in \c s. + + def_API('Z3_mk_seq_replace_all', AST ,(_in(CONTEXT), _in(AST), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_seq_replace_all(Z3_context c, Z3_ast s, Z3_ast src, Z3_ast dst); + + /** + \brief Replace the first occurrence of regular expression \c re with \c dst in \c s. + + def_API('Z3_mk_seq_replace_re', AST ,(_in(CONTEXT), _in(AST), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_seq_replace_re(Z3_context c, Z3_ast s, Z3_ast re, Z3_ast dst); + + /** + \brief Replace all occurrences of regular expression \c re with \c dst in \c s. + + def_API('Z3_mk_seq_replace_re_all', AST ,(_in(CONTEXT), _in(AST), _in(AST), _in(AST))) + */ + Z3_ast Z3_API Z3_mk_seq_replace_re_all(Z3_context c, Z3_ast s, Z3_ast re, Z3_ast dst); + /** \brief Retrieve from \c s the unit sequence positioned at position \c index. The sequence is empty if the index is out of bounds.