From 1baaa7c6de07801f8c84cd506c1021308a5069a2 Mon Sep 17 00:00:00 2001 From: Geoffroy Couprie Date: Sat, 24 Oct 2020 16:22:34 +0200 Subject: [PATCH] support native endianness in number parsers --- src/number/complete.rs | 48 +++++++++++++++++++++++++++++++++++++++++ src/number/mod.rs | 2 ++ src/number/streaming.rs | 48 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) diff --git a/src/number/complete.rs b/src/number/complete.rs index 7c9ea8c..b89f938 100644 --- a/src/number/complete.rs +++ b/src/number/complete.rs @@ -755,6 +755,10 @@ where match endian { crate::number::Endianness::Big => be_u16, crate::number::Endianness::Little => le_u16, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_u16, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_u16, } } @@ -790,6 +794,10 @@ where match endian { crate::number::Endianness::Big => be_u24, crate::number::Endianness::Little => le_u24, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_u24, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_u24, } } @@ -825,6 +833,10 @@ where match endian { crate::number::Endianness::Big => be_u32, crate::number::Endianness::Little => le_u32, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_u32, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_u32, } } @@ -860,6 +872,10 @@ where match endian { crate::number::Endianness::Big => be_u64, crate::number::Endianness::Little => le_u64, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_u64, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_u64, } } @@ -896,6 +912,10 @@ where match endian { crate::number::Endianness::Big => be_u128, crate::number::Endianness::Little => le_u128, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_u128, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_u128, } } @@ -955,6 +975,10 @@ where match endian { crate::number::Endianness::Big => be_i16, crate::number::Endianness::Little => le_i16, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_i16, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_i16, } } @@ -990,6 +1014,10 @@ where match endian { crate::number::Endianness::Big => be_i24, crate::number::Endianness::Little => le_i24, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_i24, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_i24, } } @@ -1025,6 +1053,10 @@ where match endian { crate::number::Endianness::Big => be_i32, crate::number::Endianness::Little => le_i32, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_i32, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_i32, } } @@ -1060,6 +1092,10 @@ where match endian { crate::number::Endianness::Big => be_i64, crate::number::Endianness::Little => le_i64, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_i64, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_i64, } } @@ -1096,6 +1132,10 @@ where match endian { crate::number::Endianness::Big => be_i128, crate::number::Endianness::Little => le_i128, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_i128, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_i128, } } @@ -1235,6 +1275,10 @@ where match endian { crate::number::Endianness::Big => be_f32, crate::number::Endianness::Little => le_f32, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_f32, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_f32, } } @@ -1270,6 +1314,10 @@ where match endian { crate::number::Endianness::Big => be_f64, crate::number::Endianness::Little => le_f64, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_f64, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_f64, } } diff --git a/src/number/mod.rs b/src/number/mod.rs index 527dcad..6ab2cd5 100644 --- a/src/number/mod.rs +++ b/src/number/mod.rs @@ -13,4 +13,6 @@ pub enum Endianness { Big, /// Little endian Little, + /// Will match the host's endianness + Native, } diff --git a/src/number/streaming.rs b/src/number/streaming.rs index 4afb746..d86327a 100644 --- a/src/number/streaming.rs +++ b/src/number/streaming.rs @@ -729,6 +729,10 @@ where match endian { crate::number::Endianness::Big => be_u16, crate::number::Endianness::Little => le_u16, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_u16, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_u16, } } @@ -764,6 +768,10 @@ where match endian { crate::number::Endianness::Big => be_u24, crate::number::Endianness::Little => le_u24, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_u24, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_u24, } } @@ -799,6 +807,10 @@ where match endian { crate::number::Endianness::Big => be_u32, crate::number::Endianness::Little => le_u32, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_u32, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_u32, } } @@ -834,6 +846,10 @@ where match endian { crate::number::Endianness::Big => be_u64, crate::number::Endianness::Little => le_u64, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_u64, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_u64, } } @@ -870,6 +886,10 @@ where match endian { crate::number::Endianness::Big => be_u128, crate::number::Endianness::Little => le_u128, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_u128, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_u128, } } @@ -929,6 +949,10 @@ where match endian { crate::number::Endianness::Big => be_i16, crate::number::Endianness::Little => le_i16, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_i16, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_i16, } } @@ -964,6 +988,10 @@ where match endian { crate::number::Endianness::Big => be_i24, crate::number::Endianness::Little => le_i24, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_i24, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_i24, } } @@ -999,6 +1027,10 @@ where match endian { crate::number::Endianness::Big => be_i32, crate::number::Endianness::Little => le_i32, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_i32, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_i32, } } @@ -1034,6 +1066,10 @@ where match endian { crate::number::Endianness::Big => be_i64, crate::number::Endianness::Little => le_i64, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_i64, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_i64, } } @@ -1070,6 +1106,10 @@ where match endian { crate::number::Endianness::Big => be_i128, crate::number::Endianness::Little => le_i128, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_i128, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_i128, } } @@ -1205,6 +1245,10 @@ where match endian { crate::number::Endianness::Big => be_f32, crate::number::Endianness::Little => le_f32, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_f32, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_f32, } } @@ -1240,6 +1284,10 @@ where match endian { crate::number::Endianness::Big => be_f64, crate::number::Endianness::Little => le_f64, + #[cfg(target_endian = "big")] + crate::number::Endianness::Native => be_f64, + #[cfg(target_endian = "little")] + crate::number::Endianness::Native => le_f64, } }