core: refactor num and unit split

This commit is contained in:
John Shin 2023-05-30 00:26:17 -07:00 committed by Sylvestre Ledru
parent 6cadffc8f1
commit 8ef926c6e8

View file

@ -80,30 +80,15 @@ impl<'parser> Parser<'parser> {
// Split the size argument into numeric and unit parts // Split the size argument into numeric and unit parts
// For example, if the argument is "123K", the numeric part is "123", and // For example, if the argument is "123K", the numeric part is "123", and
// the unit is "K" // the unit is "K"
let (numeric_string, unit) = match number_system { let numeric_string: String = match number_system {
NumberSystem::Hexadecimal => { NumberSystem::Hexadecimal => size
let numeric_string: String = size .chars()
.chars() .take(2)
.take(2) .chain(size.chars().skip(2).take_while(|c| c.is_ascii_hexdigit()))
.chain(size.chars().skip(2).take_while(|c| c.is_ascii_hexdigit())) .collect(),
.collect(); _ => size.chars().take_while(|c| c.is_ascii_digit()).collect(),
let unit: String = size.chars().skip(numeric_string.len()).collect();
(numeric_string, unit)
}
_ => {
let mut unit: String = size
.chars()
.rev()
.take_while(|c| c.is_alphabetic())
.collect();
unit = unit.chars().rev().collect();
let numeric_string = size.chars().take(size.len() - unit.len()).collect();
(numeric_string, unit)
}
}; };
let mut unit: &str = unit.as_str(); let mut unit: &str = &size[numeric_string.len()..];
if let Some(default_unit) = self.default_unit { if let Some(default_unit) = self.default_unit {
// Check if `unit` is empty then assigns `default_unit` to `unit` // Check if `unit` is empty then assigns `default_unit` to `unit`