1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

seq: Updates hex parse readability, adds hex test

This commit is contained in:
vulppine 2021-10-05 18:41:28 -07:00
parent 4e1f945e86
commit cddd40b4e1
2 changed files with 19 additions and 20 deletions

View file

@ -89,25 +89,9 @@ impl FromStr for Number {
s = &s[1..]; s = &s[1..];
} }
let is_neg = s.starts_with('-'); 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 { match s.to_lowercase().find("0x") {
(true, i) => match i <= 1 { Some(i) if i <= 1 => match &s.as_bytes()[i + 2] {
false => Err(format!(
"invalid hexadecimal argument: {}\nTry '{} --help' for more information.",
s.quote(),
uucore::execution_phrase(),
)),
true => match &s.as_bytes()[i + 2] {
b'-' | b'+' => Err(format!( b'-' | b'+' => Err(format!(
"invalid hexadecimal argument: {}\nTry '{} --help' for more information.", "invalid hexadecimal argument: {}\nTry '{} --help' for more information.",
s.quote(), s.quote(),
@ -133,8 +117,13 @@ impl FromStr for Number {
} }
} }
}, },
}, Some(_) => Err(format!(
(false, _) => match s.parse::<BigInt>() { "invalid hexadecimal argument: {}\nTry '{} --help' for more information.",
s.quote(),
uucore::execution_phrase(),
)),
None => match s.parse::<BigInt>() {
Ok(n) => { Ok(n) => {
// If `s` is '-0', then `parse()` returns // If `s` is '-0', then `parse()` returns
// `BigInt::zero()`, but we need to return // `BigInt::zero()`, but we need to return

View file

@ -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] #[test]
fn test_rejects_nan() { fn test_rejects_nan() {
let ts = TestScenario::new(util_name!()); let ts = TestScenario::new(util_name!());