Skip to content

Commit

Permalink
Add binding for str.substr (#321)
Browse files Browse the repository at this point in the history
Signed-off-by: Yage Hu <[email protected]>
  • Loading branch information
yagehu authored Nov 6, 2024
1 parent c69cb40 commit ea9df51
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions z3/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,51 @@ impl<'ctx> String<'ctx> {
}
}

/// Retrieve the substring of length `length` starting at `offset`.
///
/// # Examples
/// ```
/// # use z3::{Config, Context, Solver, SatResult};
/// # use z3::ast::{Ast as _, Int, String};
/// #
/// # let cfg = Config::new();
/// # let ctx = Context::new(&cfg);
/// # let solver = Solver::new(&ctx);
/// #
/// let s = String::from_str(&ctx, "abc").unwrap();
/// let sub = String::fresh_const(&ctx, "");
///
/// solver.assert(
/// &sub._eq(
/// &s.substr(
/// &Int::from_u64(&ctx, 1),
/// &Int::from_u64(&ctx, 2),
/// )
/// )
/// );
///
/// assert_eq!(solver.check(), SatResult::Sat);
/// assert_eq!(
/// solver
/// .get_model()
/// .unwrap()
/// .eval(&sub, true)
/// .unwrap()
/// .as_string()
/// .unwrap()
/// .as_str(),
/// "bc",
/// );
/// ```
pub fn substr(&self, offset: &Int<'ctx>, length: &Int<'ctx>) -> Self {
unsafe {
Self::wrap(
self.ctx,
Z3_mk_seq_extract(self.ctx.z3_ctx, self.z3_ast, offset.z3_ast, length.z3_ast),
)
}
}

/// Checks if this string matches a `z3::ast::Regexp`
pub fn regex_matches(&self, regex: &Regexp) -> Bool<'ctx> {
assert!(self.ctx == regex.ctx);
Expand Down

0 comments on commit ea9df51

Please sign in to comment.