mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
core: refactor parse_number in size parser
This commit is contained in:
parent
aed8a5759a
commit
81871d54a2
1 changed files with 13 additions and 12 deletions
|
@ -156,25 +156,16 @@ impl<'parser> Parser<'parser> {
|
||||||
if numeric_string.is_empty() {
|
if numeric_string.is_empty() {
|
||||||
1
|
1
|
||||||
} else {
|
} else {
|
||||||
match numeric_string.parse() {
|
self.parse_number(&numeric_string, 10, size)?
|
||||||
Ok(n) => n,
|
|
||||||
Err(_) => return Err(ParseSizeError::parse_failure(size)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
NumberSystem::Octal => {
|
NumberSystem::Octal => {
|
||||||
let trimmed_string = numeric_string.trim_start_matches('0');
|
let trimmed_string = numeric_string.trim_start_matches('0');
|
||||||
match u64::from_str_radix(trimmed_string, 8) {
|
self.parse_number(trimmed_string, 8, size)?
|
||||||
Ok(res) => res,
|
|
||||||
Err(_) => return Err(ParseSizeError::parse_failure(size)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
NumberSystem::Hexadecimal => {
|
NumberSystem::Hexadecimal => {
|
||||||
let trimmed_string = numeric_string.trim_start_matches("0x");
|
let trimmed_string = numeric_string.trim_start_matches("0x");
|
||||||
match u64::from_str_radix(trimmed_string, 16) {
|
self.parse_number(trimmed_string, 16, size)?
|
||||||
Ok(res) => res,
|
|
||||||
Err(_) => return Err(ParseSizeError::parse_failure(size)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -204,6 +195,16 @@ impl<'parser> Parser<'parser> {
|
||||||
|
|
||||||
NumberSystem::Decimal
|
NumberSystem::Decimal
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_number(
|
||||||
|
&self,
|
||||||
|
numeric_string: &str,
|
||||||
|
radix: u32,
|
||||||
|
original_size: &str,
|
||||||
|
) -> Result<u64, ParseSizeError> {
|
||||||
|
u64::from_str_radix(numeric_string, radix)
|
||||||
|
.map_err(|_| ParseSizeError::ParseFailure(original_size.to_string()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse a size string into a number of bytes.
|
/// Parse a size string into a number of bytes.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue