Skip to content

Commit 5683fb5

Browse files
Use numeric_cast instead of other conversion
This is to demonstrate the use of numeric_cast
1 parent fc294f8 commit 5683fb5

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/solvers/refinement/string_refinement.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -794,12 +794,13 @@ static optionalt<exprt> get_array(
794794
return {};
795795
}
796796

797-
unsigned n;
798-
if(to_unsigned_integer(to_constant_expr(size_val), n))
797+
auto n_opt = numeric_cast<unsigned>(size_val);
798+
if(!n_opt)
799799
{
800800
stream << "(sr::get_array) size is not valid" << eom;
801801
return {};
802802
}
803+
unsigned n = *n_opt;
803804

804805
const array_typet ret_type(char_type, from_integer(n, index_type));
805806
array_exprt ret(ret_type);
@@ -824,9 +825,11 @@ static optionalt<exprt> get_array(
824825
for(size_t i = 0; i < arr_val.operands().size(); i += 2)
825826
{
826827
exprt index = arr_val.operands()[i];
827-
unsigned idx;
828-
if(!to_unsigned_integer(to_constant_expr(index), idx) && idx<n)
829-
initial_map[idx] = arr_val.operands()[i + 1];
828+
if(auto idx = numeric_cast<std::size_t>(index))
829+
{
830+
if(*idx < n)
831+
initial_map[*idx] = arr_val.operands()[i + 1];
832+
}
830833
}
831834

832835
// Pad the concretized values to the left to assign the uninitialized
@@ -855,8 +858,7 @@ static std::string string_of_array(const array_exprt &arr)
855858
return std::string("");
856859

857860
exprt size_expr=to_array_type(arr.type()).size();
858-
PRECONDITION(size_expr.id()==ID_constant);
859-
to_unsigned_integer(to_constant_expr(size_expr), n);
861+
auto n = numeric_cast_v<unsigned>(size_expr);
860862
return utf16_constant_array_to_java(arr, n);
861863
}
862864

0 commit comments

Comments
 (0)