diff --git a/src/libraries/stdlib/integer.ml b/src/libraries/stdlib/integer.ml index 103b96f8acbe155e7f9a7d17cd17f17ca09ac5a7..2b68a32bcc1570e82eaa41170d844c93c9ccdbdc 100644 --- a/src/libraries/stdlib/integer.ml +++ b/src/libraries/stdlib/integer.ml @@ -101,6 +101,11 @@ let to_int = Z.to_int let to_int64 = Z.to_int64 let to_int32 = Z.to_int32 +let wrap to_int i = try Some (to_int i) with Z.Overflow -> None +let to_int_opt = wrap Z.to_int +let to_int64_opt = wrap Z.to_int64 +let to_int32_opt = wrap Z.to_int32 + let of_string = Z.of_string let to_string = Z.to_string diff --git a/src/libraries/stdlib/integer.mli b/src/libraries/stdlib/integer.mli index 5a3019d2b082c88a077a406e04772eaa4f147005..989c259f5974d9e7c19ee9946398eb30a774a357 100644 --- a/src/libraries/stdlib/integer.mli +++ b/src/libraries/stdlib/integer.mli @@ -122,9 +122,45 @@ val of_int : int -> t val of_int64 : Int64.t -> t val of_int32 : Int32.t -> t -val to_int : t -> int (** @raise Z.Overflow if too big *) -val to_int64 : t -> int64 (** @raise Z.Overflow if too big *) -val to_int32 : t -> int32 (** @raise Z.Overflow if too big *) +(** + @raise Z.Overflow if too big + @deprecated Frama-C+dev use [to_int_opt] instead +*) +val to_int : t -> int + +(** + @raise Z.Overflow if too big + @deprecated Frama-C+dev use [to_int64_opt] instead +*) +val to_int64 : t -> int64 + +(** + @raise Z.Overflow if too big + @deprecated Frama-C+dev use [to_int32_opt] instead +*) +val to_int32 : t -> int32 + +(** + Returns [Some i] if the number can be converted to an [int], + or [None] otherwise. + @since Frama-C+dev +*) +val to_int_opt : t -> int option + +(** + Returns [Some i] if the number can be converted to an [int64], + or [None] otherwise. + @since Frama-C+dev +*) +val to_int64_opt : t -> int64 option + +(** + Returns [Some i] if the number can be converted to an [int32], + or [None] otherwise. + @since Frama-C+dev +*) +val to_int32_opt : t -> int32 option + val to_float : t -> float val of_float : float -> t