From 38d850cd4bcb675464848988768cdc152d2c701c Mon Sep 17 00:00:00 2001 From: Esben Sonne Date: Thu, 18 Apr 2024 14:04:05 +0200 Subject: [PATCH 1/3] Support leading unary plus for ints --- src/input/shared.rs | 3 +++ tests/validators/test_int.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/input/shared.rs b/src/input/shared.rs index 771f16ed9..1cd8f04e8 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.trim_start_matches('+'); + // strip loading zeros s = strip_leading_zeros(s)?; diff --git a/tests/validators/test_int.py b/tests/validators/test_int.py index 83e527514..4fd22ef27 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,10 @@ ('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', 42), ('0_42', 42), ('4_2.0', 42), From 34587a57590d9554f1090bb333c0ce70167ad754 Mon Sep 17 00:00:00 2001 From: Esben Sonne Date: Thu, 18 Apr 2024 15:23:43 +0200 Subject: [PATCH 2/3] Only allow single unary plus --- src/input/shared.rs | 2 +- tests/validators/test_int.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/input/shared.rs b/src/input/shared.rs index 1cd8f04e8..fe26c503d 100644 --- a/src/input/shared.rs +++ b/src/input/shared.rs @@ -115,7 +115,7 @@ fn clean_int_str(mut s: &str) -> Option> { s = s.trim(); // strip leading unary plus - s = s.trim_start_matches('+'); + 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 4fd22ef27..6cb2377e4 100644 --- a/tests/validators/test_int.py +++ b/tests/validators/test_int.py @@ -47,6 +47,7 @@ ('+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')), ('4_2', 42), ('0_42', 42), ('4_2.0', 42), From 5a65eccdaaa1a7b881637ef3c3f305fd708f60e9 Mon Sep 17 00:00:00 2001 From: Esben Sonne Date: Thu, 18 Apr 2024 15:33:59 +0200 Subject: [PATCH 3/3] Add test case for combined plus minus prefix --- tests/validators/test_int.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/validators/test_int.py b/tests/validators/test_int.py index 6cb2377e4..5732fe181 100644 --- a/tests/validators/test_int.py +++ b/tests/validators/test_int.py @@ -48,6 +48,7 @@ ('+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),