From 91c354182d631b9845cc37a3ed3f7177c27ec1c4 Mon Sep 17 00:00:00 2001 From: Esben Sonne Date: Thu, 18 Apr 2024 19:07:34 +0200 Subject: [PATCH] fix validation of ints with leading unary plus (#1272) --- src/input/shared.rs | 3 +++ tests/validators/test_int.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/input/shared.rs b/src/input/shared.rs index 771f16ed9..fe26c503d 100644 --- a/src/input/shared.rs +++ b/src/input/shared.rs @@ -114,6 +114,9 @@ fn clean_int_str(mut s: &str) -> Option> { // strip leading and trailing whitespace s = s.trim(); + // strip leading unary plus + s = s.strip_prefix('+').unwrap_or(s); + // strip loading zeros s = strip_leading_zeros(s)?; diff --git a/tests/validators/test_int.py b/tests/validators/test_int.py index 83e527514..5732fe181 100644 --- a/tests/validators/test_int.py +++ b/tests/validators/test_int.py @@ -23,6 +23,10 @@ ('00', 0), ('000', 0), ('0_000', 0), + ('+0', 0), + ('+00', 0), + ('+000', 0), + ('+0_000', 0), (1, 1), (' 1 ', 1), (42, 42), @@ -39,6 +43,12 @@ ('00_', Err('Input should be a valid integer, unable to parse string as an integer')), # next character after 9 is not valid ('0:', Err('Input should be a valid integer, unable to parse string as an integer')), + ('+4_2', 42), + ('+0_42', 42), + ('+4_2.0', 42), + ('+04_2.0', 42), + ('++4_2', Err('Input should be a valid integer, unable to parse string as an integer')), + ('+-1', Err('Input should be a valid integer, unable to parse string as an integer')), ('4_2', 42), ('0_42', 42), ('4_2.0', 42),