From cddd40b4e1766a32d3f46f010e64dc07c2fb1fad Mon Sep 17 00:00:00 2001 From: vulppine Date: Tue, 5 Oct 2021 18:41:28 -0700 Subject: [PATCH] seq: Updates hex parse readability, adds hex test --- src/uu/seq/src/seq.rs | 29 +++++++++-------------------- tests/by-util/test_seq.rs | 10 ++++++++++ 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/uu/seq/src/seq.rs b/src/uu/seq/src/seq.rs index 594796641..a76a23c4e 100644 --- a/src/uu/seq/src/seq.rs +++ b/src/uu/seq/src/seq.rs @@ -89,25 +89,9 @@ impl FromStr for Number { s = &s[1..]; } let is_neg = s.starts_with('-'); - let is_hex = { - // GNU 20.11.2 - Parsing of Floats - match s.find("0x") { - Some(i) => (true, i), - None => match s.find("0X") { - Some(i) => (true, i), - None => (false, 0), - }, - } - }; - match is_hex { - (true, i) => match i <= 1 { - false => Err(format!( - "invalid hexadecimal argument: {}\nTry '{} --help' for more information.", - s.quote(), - uucore::execution_phrase(), - )), - true => match &s.as_bytes()[i + 2] { + match s.to_lowercase().find("0x") { + Some(i) if i <= 1 => match &s.as_bytes()[i + 2] { b'-' | b'+' => Err(format!( "invalid hexadecimal argument: {}\nTry '{} --help' for more information.", s.quote(), @@ -133,8 +117,13 @@ impl FromStr for Number { } } }, - }, - (false, _) => match s.parse::() { + Some(_) => Err(format!( + "invalid hexadecimal argument: {}\nTry '{} --help' for more information.", + s.quote(), + uucore::execution_phrase(), + )), + + None => match s.parse::() { Ok(n) => { // If `s` is '-0', then `parse()` returns // `BigInt::zero()`, but we need to return diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index 7136f5e76..6ed3cb67d 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -54,6 +54,16 @@ fn test_hex_big_number() { ); } +#[test] +fn test_hex_identifier_in_wrong_place() { + new_ucmd!() + .args(&["1234ABCD0x"]) + .fails() + .no_stdout() + .stderr_contains("invalid hexadecimal argument: '1234ABCD0x'") + .stderr_contains("for more information."); +} + #[test] fn test_rejects_nan() { let ts = TestScenario::new(util_name!());