drop num_traits dependency for hex literal parsing

This commit is contained in:
Kaspar Schleiser 2023-05-27 17:06:42 +02:00 committed by ISibboI
parent 6211888086
commit f6596b13ec
2 changed files with 6 additions and 14 deletions

View File

@ -27,7 +27,6 @@ regex = { version = "1.5.5", optional = true}
serde = { version = "1.0.133", optional = true} serde = { version = "1.0.133", optional = true}
serde_derive = { version = "1.0.133", optional = true} serde_derive = { version = "1.0.133", optional = true}
rand = { version = "0.8.5", optional = true} rand = { version = "0.8.5", optional = true}
num-traits = "0.2.15"
[features] [features]
serde_support = ["serde", "serde_derive"] serde_support = ["serde", "serde_derive"]

View File

@ -348,7 +348,7 @@ fn partial_tokens_to_tokens(mut tokens: &[PartialToken]) -> EvalexprResult<Vec<T
}, },
PartialToken::Literal(literal) => { PartialToken::Literal(literal) => {
cutoff = 1; cutoff = 1;
if let Ok(number) = literal.parse_dec_or_hex::<IntType>() { if let Ok(number) = parse_dec_or_hex(&literal) {
Some(Token::Int(number)) Some(Token::Int(number))
} else if let Ok(number) = literal.parse::<FloatType>() { } else if let Ok(number) = literal.parse::<FloatType>() {
Some(Token::Float(number)) Some(Token::Float(number))
@ -442,18 +442,11 @@ pub(crate) fn tokenize(string: &str) -> EvalexprResult<Vec<Token>> {
partial_tokens_to_tokens(&str_to_partial_tokens(string)?) partial_tokens_to_tokens(&str_to_partial_tokens(string)?)
} }
/// Helper trait to parse strings to integer from both decimal or hex fn parse_dec_or_hex(literal: &str) -> Result<IntType, std::num::ParseIntError> {
trait ParseDecOrHex { if literal.starts_with("0x") {
fn parse_dec_or_hex<T: num_traits::Num>(&self) -> Result<T, T::FromStrRadixErr>; IntType::from_str_radix(&literal[2..], 16)
} } else {
IntType::from_str_radix(&literal, 10)
impl ParseDecOrHex for str {
fn parse_dec_or_hex<T: num_traits::Num>(&self) -> Result<T, T::FromStrRadixErr> {
if self.starts_with("0x") {
T::from_str_radix(&self[2..], 16)
} else {
T::from_str_radix(&self, 10)
}
} }
} }