From 51675b1fb69456aa694ea0d6c0b9cc2e52199172 Mon Sep 17 00:00:00 2001 From: chielP Date: Thu, 24 Aug 2023 12:12:17 +0200 Subject: [PATCH] better way of checking numeric --- crates/polars-arrow/src/compute/decimal.rs | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/polars-arrow/src/compute/decimal.rs b/crates/polars-arrow/src/compute/decimal.rs index 2af5037a2a86..05d91b5eea16 100644 --- a/crates/polars-arrow/src/compute/decimal.rs +++ b/crates/polars-arrow/src/compute/decimal.rs @@ -1,4 +1,4 @@ -use atoi::atoi; +use atoi::FromRadix10SignedChecked; fn significant_digits(bytes: &[u8]) -> u8 { (bytes.len() as u8) - leading_zeros(bytes) @@ -15,10 +15,12 @@ fn split_decimal_bytes(bytes: &[u8]) -> (Option<&[u8]>, Option<&[u8]>) { (lhs, rhs) } -fn is_numeric(bytes: &[u8]) -> bool { - bytes - .iter() - .all(|b| matches!(*b, b'0'..=b'9' | b'.' | b'+' | b'-')) +fn parse_integer_checked(bytes: &[u8]) -> Option{ + let (n,len) = i128::from_radix_10_signed_checked(bytes); + match n { + Some(i) if len == bytes.len() => Some(i), + _ => None + } } pub fn infer_scale(bytes: &[u8]) -> Option { @@ -32,12 +34,10 @@ pub fn infer_scale(bytes: &[u8]) -> Option { pub(super) fn deserialize_decimal(bytes: &[u8], precision: Option, scale: u8) -> Option { let (lhs, rhs) = split_decimal_bytes(bytes); let precision = precision.unwrap_or(u8::MAX); - if !is_numeric(bytes) { - return None; - } + match (lhs, rhs) { - (Some(lhs), Some(rhs)) => atoi::(lhs).and_then(|x| { - atoi::(rhs) + (Some(lhs), Some(rhs)) => parse_integer_checked(lhs).and_then(|x| { + parse_integer_checked(rhs) .map(|y| (x, lhs, y, rhs)) .and_then(|(lhs, lhs_b, rhs, rhs_b)| { let lhs_s = significant_digits(lhs_b); @@ -86,13 +86,13 @@ pub(super) fn deserialize_decimal(bytes: &[u8], precision: Option, scale: u8 if rhs.len() > precision as usize || rhs.len() != scale as usize { return None; } - atoi::(rhs) + parse_integer_checked(rhs) }, (Some(lhs), None) => { if lhs.len() > precision as usize || scale != 0 { return None; } - atoi::(lhs) + parse_integer_checked(lhs) }, (None, None) => None, }