Skip to content

Commit e1280cc

Browse files
Add utility function add_constraint_on_characters
This generalizes what is done in add_default_axioms for making characters printable.
1 parent ccdd483 commit e1280cc

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

src/solvers/refinement/string_constraint_generator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ class string_constraint_generatort final
109109

110110
void add_default_axioms(const array_string_exprt &s);
111111
exprt axiom_for_is_positive_index(const exprt &x);
112+
void add_constraint_on_characters(
113+
const array_string_exprt &s,
114+
const exprt &start,
115+
const exprt &end,
116+
const std::string &char_set);
112117

113118
// The following functions add axioms for the returned value
114119
// to be equal to the result of the function given as argument.

src/solvers/refinement/string_constraint_generator_main.cpp

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,16 +324,43 @@ void string_constraint_generatort::add_default_axioms(
324324
if(max_string_length!=std::numeric_limits<size_t>::max())
325325
axioms.push_back(s.axiom_for_length_le(max_string_length));
326326

327+
const exprt index_zero = from_integer(0, s.length().type());
327328
if(force_printable_characters)
328-
{
329-
symbol_exprt qvar=fresh_univ_index("printable", s.length().type());
330-
exprt chr=s[qvar];
331-
and_exprt printable(
332-
binary_relation_exprt(chr, ID_ge, from_integer(' ', chr.type())),
333-
binary_relation_exprt(chr, ID_le, from_integer('~', chr.type())));
334-
string_constraintt sc(qvar, s.length(), printable);
335-
axioms.push_back(sc);
336-
}
329+
add_constraint_on_characters(s, index_zero, s.length(), " -~");
330+
}
331+
332+
/// Add constraint on characters of a string.
333+
///
334+
/// This constraint is
335+
/// \f$ \forall i \in [start, end), low\_char \le s[i] \le high\_char \f$
336+
/// \param s: a string expression
337+
/// \param start: index of the first character to constrain
338+
/// \param end: index at which we stop adding constraints
339+
/// \param char_set: a string of the form "<low_char>-<high_char>" where
340+
/// `<low_char>` and `<high_char>` are two characters, which represents
341+
/// the set of characters that are between `low_char` and `high_char`.
342+
/// \return a string expression that is linked to the argument through axioms
343+
/// that are added to the list
344+
void string_constraint_generatort::add_constraint_on_characters(
345+
const array_string_exprt &s,
346+
const exprt &start,
347+
const exprt &end,
348+
const std::string &char_set)
349+
{
350+
// Parse char_set
351+
PRECONDITION(char_set.length() == 3);
352+
PRECONDITION(char_set[1] == '-');
353+
const char &low_char = char_set[0];
354+
const char &high_char = char_set[2];
355+
356+
// Add constraint
357+
const symbol_exprt qvar = fresh_univ_index("char_constr", s.length().type());
358+
const exprt chr = s[qvar];
359+
const and_exprt char_in_set(
360+
binary_relation_exprt(chr, ID_ge, from_integer(low_char, chr.type())),
361+
binary_relation_exprt(chr, ID_le, from_integer(high_char, chr.type())));
362+
const string_constraintt sc(qvar, start, end, true_exprt(), char_in_set);
363+
axioms.push_back(sc);
337364
}
338365

339366
/// Adds creates a new array if it does not already exists

0 commit comments

Comments
 (0)