3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-23 09:05:31 +00:00

theory_str Replace method

This commit is contained in:
Murphy Berzish 2016-06-15 21:14:54 -04:00
parent fb20951064
commit 5b3c868c90
6 changed files with 118 additions and 11 deletions

View file

@ -176,6 +176,28 @@ br_status str_rewriter::mk_str_LastIndexof(expr * haystack, expr * needle, expr_
}
}
br_status str_rewriter::mk_str_Replace(expr * base, expr * source, expr * target, expr_ref & result) {
TRACE("t_str_rw", tout << "rewrite (Replace " << mk_pp(base, m()) << " " << mk_pp(source, m()) << " " << mk_pp(target, m()) << ")" << std::endl;);
if (m_strutil.is_string(base) && m_strutil.is_string(source) && m_strutil.is_string(target)) {
std::string arg0Str = m_strutil.get_string_constant_value(base);
std::string arg1Str = m_strutil.get_string_constant_value(source);
std::string arg2Str = m_strutil.get_string_constant_value(target);
if (arg0Str.find(arg1Str) != std::string::npos) {
int index1 = arg0Str.find(arg1Str);
int index2 = index1 + arg1Str.length();
std::string substr0 = arg0Str.substr(0, index1);
std::string substr2 = arg0Str.substr(index2);
std::string replaced = substr0 + arg2Str + substr2;
result = m_strutil.mk_string(replaced);
} else {
result = base;
}
return BR_DONE;
} else {
return BR_FAILED;
}
}
br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * const * args, expr_ref & result) {
SASSERT(f->get_family_id() == get_fid());
@ -204,6 +226,9 @@ br_status str_rewriter::mk_app_core(func_decl * f, unsigned num_args, expr * con
case OP_STR_LASTINDEXOF:
SASSERT(num_args == 2);
return mk_str_LastIndexof(args[0], args[1], result);
case OP_STR_REPLACE:
SASSERT(num_args == 3);
return mk_str_Replace(args[0], args[1], args[2], result);
default:
return BR_FAILED;
}

View file

@ -47,6 +47,7 @@ public:
br_status mk_str_Indexof(expr * haystack, expr * needle, expr_ref & result);
br_status mk_str_Indexof2(expr * arg0, expr * arg1, expr * arg2, expr_ref & result);
br_status mk_str_LastIndexof(expr * haystack, expr * needle, expr_ref & result);
br_status mk_str_Replace(expr * base, expr * source, expr * target, expr_ref & result);
bool reduce_eq(expr * l, expr * r, expr_ref_vector & lhs, expr_ref_vector & rhs, bool & change);
bool reduce_eq(expr_ref_vector& ls, expr_ref_vector& rs, expr_ref_vector& lhs, expr_ref_vector& rhs, bool& change);

View file

@ -34,6 +34,7 @@ str_decl_plugin::str_decl_plugin():
m_indexof2_decl(0),
m_lastindexof_decl(0),
m_substr_decl(0),
m_replace_decl(0),
m_arith_plugin(0),
m_arith_fid(0),
m_int_sort(0){
@ -55,6 +56,7 @@ void str_decl_plugin::finalize(void) {
DEC_REF(m_indexof2_decl);
DEC_REF(m_lastindexof_decl);
DEC_REF(m_substr_decl);
DEC_REF(m_replace_decl);
DEC_REF(m_int_sort);
}
@ -114,6 +116,12 @@ void str_decl_plugin::set_manager(ast_manager * m, family_id id) {
m_substr_decl = m->mk_func_decl(symbol("Substring"), 3, d, s, func_decl_info(id, OP_STR_SUBSTR));
m_manager->inc_ref(m_substr_decl);
}
{
sort * d[3] = {s, s, s};
m_replace_decl = m->mk_func_decl(symbol("Replace"), 3, d, s, func_decl_info(id, OP_STR_REPLACE));
m_manager->inc_ref(m_replace_decl);
}
}
decl_plugin * str_decl_plugin::mk_fresh() {
@ -139,6 +147,7 @@ func_decl * str_decl_plugin::mk_func_decl(decl_kind k) {
case OP_STR_INDEXOF2: return m_indexof2_decl;
case OP_STR_LASTINDEXOF: return m_lastindexof_decl;
case OP_STR_SUBSTR: return m_substr_decl;
case OP_STR_REPLACE: return m_replace_decl;
default: return 0;
}
}
@ -203,6 +212,7 @@ void str_decl_plugin::get_op_names(svector<builtin_name> & op_names, symbol cons
op_names.push_back(builtin_name("Indexof2", OP_STR_INDEXOF2));
op_names.push_back(builtin_name("LastIndexof", OP_STR_LASTINDEXOF));
op_names.push_back(builtin_name("Substring", OP_STR_SUBSTR));
op_names.push_back(builtin_name("Replace", OP_STR_REPLACE));
}
void str_decl_plugin::get_sort_names(svector<builtin_name> & sort_names, symbol const & logic) {

View file

@ -39,6 +39,7 @@ enum str_op_kind {
OP_STR_INDEXOF2,
OP_STR_LASTINDEXOF,
OP_STR_SUBSTR,
OP_STR_REPLACE,
// end
LAST_STR_OP
};
@ -59,6 +60,7 @@ protected:
func_decl * m_indexof2_decl;
func_decl * m_lastindexof_decl;
func_decl * m_substr_decl;
func_decl * m_replace_decl;
arith_decl_plugin * m_arith_plugin;
family_id m_arith_fid;