From 20b0a247e0cfa816ad7a2da6377b857fe566d5ce Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 8 Dec 2022 09:16:11 -0600 Subject: [PATCH] refactor(number): Remove macros --- src/number/complete.rs | 70 +++++++++++++++++------------------------ src/number/streaming.rs | 70 +++++++++++++++++------------------------ 2 files changed, 58 insertions(+), 82 deletions(-) diff --git a/src/number/complete.rs b/src/number/complete.rs index 3ac143b03..24ad1fdc6 100644 --- a/src/number/complete.rs +++ b/src/number/complete.rs @@ -13,26 +13,6 @@ use crate::traits::{ AsBytes, AsChar, Compare, InputIter, InputLength, InputTake, InputTakeAtPosition, Offset, Slice, }; -#[doc(hidden)] -macro_rules! map( - // Internal parser, do not use directly - (__impl $i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - $crate::combinator::map(move |i| {$submac!(i, $($args)*)}, $g).parse($i) - ); - ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - map!(__impl $i, $submac!($($args)*), $g) - ); - ($i:expr, $f:expr, $g:expr) => ( - map!(__impl $i, call!($f), $g) - ); -); - -#[doc(hidden)] -macro_rules! call ( - ($i:expr, $fun:expr) => ( $fun( $i ) ); - ($i:expr, $fun:expr, $($args:expr),* ) => ( $fun( $i, $($args),* ) ); -); - /// Recognizes an unsigned 1 byte integer. /// /// *Complete version*: Returns an error if there is not enough input data. @@ -248,7 +228,7 @@ pub fn be_i8>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, be_u8, |x| x as i8) + be_u8.map(|x| x as i8).parse(input) } /// Recognizes a big endian signed 2 bytes integer. @@ -271,7 +251,7 @@ pub fn be_i16>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, be_u16, |x| x as i16) + be_u16.map(|x| x as i16).parse(input) } /// Recognizes a big endian signed 3 bytes integer. @@ -295,11 +275,15 @@ where I: Slice> + InputIter + InputLength, { // Same as the unsigned version but we need to sign-extend manually here - map!(input, be_u24, |x| if x & 0x80_00_00 != 0 { - (x | 0xff_00_00_00) as i32 - } else { - x as i32 - }) + be_u24 + .map(|x| { + if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + } + }) + .parse(input) } /// Recognizes a big endian signed 4 bytes integer. @@ -322,7 +306,7 @@ pub fn be_i32>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, be_u32, |x| x as i32) + be_u32.map(|x| x as i32).parse(input) } /// Recognizes a big endian signed 8 bytes integer. @@ -345,7 +329,7 @@ pub fn be_i64>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, be_u64, |x| x as i64) + be_u64.map(|x| x as i64).parse(input) } /// Recognizes a big endian signed 16 bytes integer. @@ -368,7 +352,7 @@ pub fn be_i128>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, be_u128, |x| x as i128) + be_u128.map(|x| x as i128).parse(input) } /// Recognizes an unsigned 1 byte integer. @@ -586,7 +570,7 @@ pub fn le_i8>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, be_u8, |x| x as i8) + be_u8.map(|x| x as i8).parse(input) } /// Recognizes a little endian signed 2 bytes integer. @@ -609,7 +593,7 @@ pub fn le_i16>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, le_u16, |x| x as i16) + le_u16.map(|x| x as i16).parse(input) } /// Recognizes a little endian signed 3 bytes integer. @@ -633,11 +617,15 @@ where I: Slice> + InputIter + InputLength, { // Same as the unsigned version but we need to sign-extend manually here - map!(input, le_u24, |x| if x & 0x80_00_00 != 0 { - (x | 0xff_00_00_00) as i32 - } else { - x as i32 - }) + le_u24 + .map(|x| { + if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + } + }) + .parse(input) } /// Recognizes a little endian signed 4 bytes integer. @@ -660,7 +648,7 @@ pub fn le_i32>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, le_u32, |x| x as i32) + le_u32.map(|x| x as i32).parse(input) } /// Recognizes a little endian signed 8 bytes integer. @@ -683,7 +671,7 @@ pub fn le_i64>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, le_u64, |x| x as i64) + le_u64.map(|x| x as i64).parse(input) } /// Recognizes a little endian signed 16 bytes integer. @@ -706,7 +694,7 @@ pub fn le_i128>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, le_u128, |x| x as i128) + le_u128.map(|x| x as i128).parse(input) } /// Recognizes an unsigned 1 byte integer @@ -957,7 +945,7 @@ pub fn i8>(i: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(i, u8, |x| x as i8) + u8.map(|x| x as i8).parse(i) } /// Recognizes a signed 2 byte integer diff --git a/src/number/streaming.rs b/src/number/streaming.rs index 56b4dedd5..b8b809c12 100644 --- a/src/number/streaming.rs +++ b/src/number/streaming.rs @@ -12,26 +12,6 @@ use crate::traits::{ AsBytes, AsChar, Compare, InputIter, InputLength, InputTake, InputTakeAtPosition, Offset, Slice, }; -#[doc(hidden)] -macro_rules! map( - // Internal parser, do not use directly - (__impl $i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - $crate::combinator::map(move |i| {$submac!(i, $($args)*)}, $g).parse($i) - ); - ($i:expr, $submac:ident!( $($args:tt)* ), $g:expr) => ( - map!(__impl $i, $submac!($($args)*), $g) - ); - ($i:expr, $f:expr, $g:expr) => ( - map!(__impl $i, call!($f), $g) - ); -); - -#[doc(hidden)] -macro_rules! call ( - ($i:expr, $fun:expr) => ( $fun( $i ) ); - ($i:expr, $fun:expr, $($args:expr),* ) => ( $fun( $i, $($args),* ) ); -); - /// Recognizes an unsigned 1 byte integer. /// /// *Streaming version*: Will return `Err(nom::Err::Incomplete(_))` if there is not enough data. @@ -242,7 +222,7 @@ pub fn be_i8>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, be_u8, |x| x as i8) + be_u8.map(|x| x as i8).parse(input) } /// Recognizes a big endian signed 2 bytes integer. @@ -262,7 +242,7 @@ pub fn be_i16>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, be_u16, |x| x as i16) + be_u16.map(|x| x as i16).parse(input) } /// Recognizes a big endian signed 3 bytes integer. @@ -283,11 +263,15 @@ where I: Slice> + InputIter + InputLength, { // Same as the unsigned version but we need to sign-extend manually here - map!(input, be_u24, |x| if x & 0x80_00_00 != 0 { - (x | 0xff_00_00_00) as i32 - } else { - x as i32 - }) + be_u24 + .map(|x| { + if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + } + }) + .parse(input) } /// Recognizes a big endian signed 4 bytes integer. @@ -307,7 +291,7 @@ pub fn be_i32>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, be_u32, |x| x as i32) + be_u32.map(|x| x as i32).parse(input) } /// Recognizes a big endian signed 8 bytes integer. @@ -328,7 +312,7 @@ pub fn be_i64>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, be_u64, |x| x as i64) + be_u64.map(|x| x as i64).parse(input) } /// Recognizes a big endian signed 16 bytes integer. @@ -348,7 +332,7 @@ pub fn be_i128>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, be_u128, |x| x as i128) + be_u128.map(|x| x as i128).parse(input) } /// Recognizes an unsigned 1 byte integer. @@ -560,7 +544,7 @@ pub fn le_i8>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, le_u8, |x| x as i8) + le_u8.map(|x| x as i8).parse(input) } /// Recognizes a little endian signed 2 bytes integer. @@ -583,7 +567,7 @@ pub fn le_i16>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, le_u16, |x| x as i16) + le_u16.map(|x| x as i16).parse(input) } /// Recognizes a little endian signed 3 bytes integer. @@ -607,11 +591,15 @@ where I: Slice> + InputIter + InputLength, { // Same as the unsigned version but we need to sign-extend manually here - map!(input, le_u24, |x| if x & 0x80_00_00 != 0 { - (x | 0xff_00_00_00) as i32 - } else { - x as i32 - }) + le_u24 + .map(|x| { + if x & 0x80_00_00 != 0 { + (x | 0xff_00_00_00) as i32 + } else { + x as i32 + } + }) + .parse(input) } /// Recognizes a little endian signed 4 bytes integer. @@ -634,7 +622,7 @@ pub fn le_i32>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, le_u32, |x| x as i32) + le_u32.map(|x| x as i32).parse(input) } /// Recognizes a little endian signed 8 bytes integer. @@ -657,7 +645,7 @@ pub fn le_i64>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, le_u64, |x| x as i64) + le_u64.map(|x| x as i64).parse(input) } /// Recognizes a little endian signed 16 bytes integer. @@ -680,7 +668,7 @@ pub fn le_i128>(input: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(input, le_u128, |x| x as i128) + le_u128.map(|x| x as i128).parse(input) } /// Recognizes an unsigned 1 byte integer @@ -931,7 +919,7 @@ pub fn i8>(i: I) -> IResult where I: Slice> + InputIter + InputLength, { - map!(i, u8, |x| x as i8) + u8.map(|x| x as i8).parse(i) } /// Recognizes a signed 2 byte integer