Skip to content

Commit

Permalink
use str::from_utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
Weijun-H committed Sep 7, 2023
1 parent a46ba81 commit c504526
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions arrow-json/src/reader/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ macro_rules! primitive_parse {
($($t:ty),+) => {
$(impl ParseJsonNumber for $t {
fn parse(s: &[u8]) -> Option<Self> {
match s.iter().map(|&b| b as char).collect::<String>().parse::<$t>() {
Ok(f) => Some(f),
Err(_) => s.iter().map(|&b| b as char).collect::<String>().parse::<f64>().ok().and_then(NumCast::from),
match std::str::from_utf8(s) {
Ok(s) => s.to_string().parse::<$t>().ok(),
Err(_) => None,
}
}
})+
Expand All @@ -57,31 +57,28 @@ primitive_parse!(i8, i16, i32, i64, u8, u16, u32, u64);

impl ParseJsonNumber for f16 {
fn parse(s: &[u8]) -> Option<Self> {
s.iter()
.map(|&b| b as char)
.collect::<String>()
.parse::<f16>()
.ok()
match std::str::from_utf8(s) {
Ok(s) => s.to_string().parse::<f16>().ok(),
Err(_) => None,
}
}
}

impl ParseJsonNumber for f32 {
fn parse(s: &[u8]) -> Option<Self> {
s.iter()
.map(|&b| b as char)
.collect::<String>()
.parse::<f32>()
.ok()
match std::str::from_utf8(s) {
Ok(s) => s.to_string().parse::<f32>().ok(),
Err(_) => None,
}
}
}

impl ParseJsonNumber for f64 {
fn parse(s: &[u8]) -> Option<Self> {
s.iter()
.map(|&b| b as char)
.collect::<String>()
.parse::<f64>()
.ok()
match std::str::from_utf8(s) {
Ok(s) => s.to_string().parse::<f64>().ok(),
Err(_) => None,
}
}
}

Expand Down

0 comments on commit c504526

Please sign in to comment.