From c5045265c3285cdfe2fba616773ef6a66e476a86 Mon Sep 17 00:00:00 2001 From: Weijun Huang Date: Thu, 7 Sep 2023 22:04:25 +0800 Subject: [PATCH] use str::from_utf8 --- arrow-json/src/reader/primitive_array.rs | 33 +++++++++++------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/arrow-json/src/reader/primitive_array.rs b/arrow-json/src/reader/primitive_array.rs index 56fde9e8ac15..624d5706b81e 100644 --- a/arrow-json/src/reader/primitive_array.rs +++ b/arrow-json/src/reader/primitive_array.rs @@ -44,9 +44,9 @@ macro_rules! primitive_parse { ($($t:ty),+) => { $(impl ParseJsonNumber for $t { fn parse(s: &[u8]) -> Option { - match s.iter().map(|&b| b as char).collect::().parse::<$t>() { - Ok(f) => Some(f), - Err(_) => s.iter().map(|&b| b as char).collect::().parse::().ok().and_then(NumCast::from), + match std::str::from_utf8(s) { + Ok(s) => s.to_string().parse::<$t>().ok(), + Err(_) => None, } } })+ @@ -57,31 +57,28 @@ primitive_parse!(i8, i16, i32, i64, u8, u16, u32, u64); impl ParseJsonNumber for f16 { fn parse(s: &[u8]) -> Option { - s.iter() - .map(|&b| b as char) - .collect::() - .parse::() - .ok() + match std::str::from_utf8(s) { + Ok(s) => s.to_string().parse::().ok(), + Err(_) => None, + } } } impl ParseJsonNumber for f32 { fn parse(s: &[u8]) -> Option { - s.iter() - .map(|&b| b as char) - .collect::() - .parse::() - .ok() + match std::str::from_utf8(s) { + Ok(s) => s.to_string().parse::().ok(), + Err(_) => None, + } } } impl ParseJsonNumber for f64 { fn parse(s: &[u8]) -> Option { - s.iter() - .map(|&b| b as char) - .collect::() - .parse::() - .ok() + match std::str::from_utf8(s) { + Ok(s) => s.to_string().parse::().ok(), + Err(_) => None, + } } }