From 4aa302a05fad95f3d13eb2ff20bbfee88c32e7ff Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Tue, 10 May 2022 15:04:28 +0300 Subject: T4421: only allow numbers that look like an integer part with an optional fractional part --- src/numeric.ml | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src') diff --git a/src/numeric.ml b/src/numeric.ml index ab59d6c..b296cec 100644 --- a/src/numeric.ml +++ b/src/numeric.ml @@ -35,7 +35,12 @@ let check_positive opts n = if opts.positive && (n <= 0.0) then failwith "Number should be positive" +let looks_like_number value = + try let _ = Pcre.exec ~pat:"^(\\-?)[0-9]+(\\.[0-9]+)?$" value in true + with Not_found -> false + let number_of_string opts s = + if not (looks_like_number s) then Printf.ksprintf failwith "'%s' is not a valid number" s else let n = float_of_string_opt s in match n with | Some n -> -- cgit v1.2.3