diff --git a/src/lib.rs b/src/lib.rs index c6ceade..0eef094 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,46 +68,44 @@ mod simd; mod test; pub use crate::error::AtoiSimdError; -use crate::linker::{Parser, ParserNeg, ParserPos}; +use crate::linker::{Parse, ParseNeg}; /// Parses slice of digits, and checks first '-' char for signed integers. #[inline] -pub fn parse + ParserPos>(s: &[u8]) -> Result { +pub fn parse(s: &[u8]) -> Result { T::atoi_simd_parse(s) } /// Parses positive integer. #[inline] -pub fn parse_pos>(s: &[u8]) -> Result { +pub fn parse_pos(s: &[u8]) -> Result { T::atoi_simd_parse_pos(s) } /// Parses negative integer. Slice must not contain '-' sign. #[inline] -pub fn parse_neg>(s: &[u8]) -> Result { +pub fn parse_neg(s: &[u8]) -> Result { T::atoi_simd_parse_neg(s) } /// Parses slice of digits until it reaches invalid character, and checks first '-' char for signed integers. /// Returns parsed value and parsed size of the slice. #[inline] -pub fn parse_until_invalid + ParserPos>( - s: &[u8], -) -> Result<(T, usize), AtoiSimdError> { +pub fn parse_until_invalid(s: &[u8]) -> Result<(T, usize), AtoiSimdError> { T::atoi_simd_parse_until_invalid(s) } /// Parses positive integer until it reaches invalid character. /// Returns parsed value and parsed size of the slice. #[inline] -pub fn parse_until_invalid_pos>(s: &[u8]) -> Result<(T, usize), AtoiSimdError> { +pub fn parse_until_invalid_pos(s: &[u8]) -> Result<(T, usize), AtoiSimdError> { T::atoi_simd_parse_until_invalid_pos(s) } /// Parses negative integer until it reaches invalid character. Slice must not contain '-' sign. /// Returns parsed value and parsed size of the slice. #[inline] -pub fn parse_until_invalid_neg>(s: &[u8]) -> Result<(T, usize), AtoiSimdError> { +pub fn parse_until_invalid_neg(s: &[u8]) -> Result<(T, usize), AtoiSimdError> { T::atoi_simd_parse_until_invalid_neg(s) } @@ -116,6 +114,6 @@ pub fn parse_until_invalid_neg>(s: &[u8]) -> Result<(T, usize), /// Skips '+' char and extra zeroes at the beginning. /// It's slower than `parse()`. #[inline] -pub fn parse_skipped + ParserPos>(s: &[u8]) -> Result { +pub fn parse_skipped(s: &[u8]) -> Result { T::atoi_simd_parse_skipped(s) } diff --git a/src/linker/fb_32.rs b/src/linker/fb_32.rs index efee603..18f6c87 100644 --- a/src/linker/fb_32.rs +++ b/src/linker/fb_32.rs @@ -1,7 +1,7 @@ use super::*; use crate::fallback::*; -impl ParserPos for u8 { +impl ParsePos for u8 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_pos::<{ u8::MAX as u64 }>(s).map(|v| v as u8) @@ -13,7 +13,7 @@ impl ParserPos for u8 { } } -impl ParserPos for i8 { +impl ParsePos for i8 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_pos::<{ i8::MAX as u64 }>(s).map(|v| v as i8) @@ -25,7 +25,7 @@ impl ParserPos for i8 { } } -impl ParserNeg for i8 { +impl ParseNeg for i8 { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_fb_checked_neg::<{ i8::MIN as i64 }>(s).map(|v| v as i8) @@ -37,7 +37,7 @@ impl ParserNeg for i8 { } } -impl ParserPos for u16 { +impl ParsePos for u16 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_pos::<{ u16::MAX as u64 }>(s).map(|v| v as u16) @@ -49,7 +49,7 @@ impl ParserPos for u16 { } } -impl ParserPos for i16 { +impl ParsePos for i16 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_pos::<{ i16::MAX as u64 }>(s).map(|v| v as i16) @@ -61,7 +61,7 @@ impl ParserPos for i16 { } } -impl ParserNeg for i16 { +impl ParseNeg for i16 { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_fb_checked_neg::<{ i16::MIN as i64 }>(s).map(|v| v as i16) @@ -73,7 +73,7 @@ impl ParserNeg for i16 { } } -impl ParserPos for u32 { +impl ParsePos for u32 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_pos::<{ u32::MAX as u64 }>(s).map(|v| v as u32) @@ -85,7 +85,7 @@ impl ParserPos for u32 { } } -impl ParserPos for i32 { +impl ParsePos for i32 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_pos::<{ i32::MAX as u64 }>(s).map(|v| v as i32) @@ -97,7 +97,7 @@ impl ParserPos for i32 { } } -impl ParserNeg for i32 { +impl ParseNeg for i32 { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_fb_checked_neg::<{ i32::MIN as i64 }>(s).map(|v| v as i32) @@ -110,7 +110,7 @@ impl ParserNeg for i32 { } #[cfg(target_pointer_width = "32")] -impl ParserPos for usize { +impl ParsePos for usize { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_pos::<{ u32::MAX as u64 }>(s).map(|v| v as usize) @@ -123,7 +123,7 @@ impl ParserPos for usize { } #[cfg(target_pointer_width = "32")] -impl ParserPos for isize { +impl ParsePos for isize { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_pos::<{ i32::MAX as u64 }>(s).map(|v| v as isize) @@ -136,7 +136,7 @@ impl ParserPos for isize { } #[cfg(target_pointer_width = "32")] -impl ParserNeg for isize { +impl ParseNeg for isize { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_fb_checked_neg::<{ i32::MIN as i64 }>(s).map(|v| v as isize) diff --git a/src/linker/fb_64.rs b/src/linker/fb_64.rs index 17d6b99..2b26a11 100644 --- a/src/linker/fb_64.rs +++ b/src/linker/fb_64.rs @@ -2,7 +2,7 @@ use super::*; use crate::fallback::*; #[cfg(target_pointer_width = "64")] -impl ParserPos for usize { +impl ParsePos for usize { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_64_pos::<{ u64::MAX }, 4>(s).map(|v| v as usize) @@ -15,7 +15,7 @@ impl ParserPos for usize { } #[cfg(target_pointer_width = "64")] -impl ParserPos for isize { +impl ParsePos for isize { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_64_pos::<{ i64::MAX as u64 }, 3>(s).map(|v| v as isize) @@ -28,7 +28,7 @@ impl ParserPos for isize { } #[cfg(target_pointer_width = "64")] -impl ParserNeg for isize { +impl ParseNeg for isize { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_fb_checked_64_neg(s).map(|v| v as isize) @@ -40,7 +40,7 @@ impl ParserNeg for isize { } } -impl ParserPos for u64 { +impl ParsePos for u64 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_64_pos::<{ u64::MAX }, 4>(s) @@ -52,7 +52,7 @@ impl ParserPos for u64 { } } -impl ParserPos for i64 { +impl ParsePos for i64 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_64_pos::<{ i64::MAX as u64 }, 3>(s).map(|v| v as i64) @@ -64,7 +64,7 @@ impl ParserPos for i64 { } } -impl ParserNeg for i64 { +impl ParseNeg for i64 { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_fb_checked_64_neg(s) @@ -76,7 +76,7 @@ impl ParserNeg for i64 { } } -impl ParserPos for u128 { +impl ParsePos for u128 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_128_pos::<{ u128::MAX }>(s) @@ -88,7 +88,7 @@ impl ParserPos for u128 { } } -impl ParserPos for i128 { +impl ParsePos for i128 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_fb_checked_128_pos::<{ i128::MAX as u128 }>(s).map(|v| v as i128) @@ -100,7 +100,7 @@ impl ParserPos for i128 { } } -impl ParserNeg for i128 { +impl ParseNeg for i128 { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_fb_checked_128_neg(s) diff --git a/src/linker/mod.rs b/src/linker/mod.rs index 0483836..ec0f5ff 100644 --- a/src/linker/mod.rs +++ b/src/linker/mod.rs @@ -46,29 +46,29 @@ mod fb_64; use crate::{safe_unchecked::SliceGetter, AtoiSimdError}; -pub trait ParserPos: Sized { - fn atoi_simd_parse_pos(s: &[u8]) -> Result; - fn atoi_simd_parse_until_invalid_pos(s: &[u8]) -> Result<(T, usize), AtoiSimdError>; +pub trait ParsePos: Sized { + fn atoi_simd_parse_pos(s: &[u8]) -> Result; + fn atoi_simd_parse_until_invalid_pos(s: &[u8]) -> Result<(Self, usize), AtoiSimdError>; } -pub trait ParserNeg: Sized { - fn atoi_simd_parse_neg(s: &[u8]) -> Result; - fn atoi_simd_parse_until_invalid_neg(s: &[u8]) -> Result<(T, usize), AtoiSimdError>; +pub trait ParseNeg: Sized { + fn atoi_simd_parse_neg(s: &[u8]) -> Result; + fn atoi_simd_parse_until_invalid_neg(s: &[u8]) -> Result<(Self, usize), AtoiSimdError>; } -pub trait Parser>: Sized { +pub trait Parse: ParsePos { #[inline(always)] - fn atoi_simd_parse(s: &[u8]) -> Result { - T::atoi_simd_parse_pos(s) + fn atoi_simd_parse(s: &[u8]) -> Result { + Self::atoi_simd_parse_pos(s) } #[inline(always)] - fn atoi_simd_parse_until_invalid(s: &[u8]) -> Result<(T, usize), AtoiSimdError> { - T::atoi_simd_parse_until_invalid_pos(s) + fn atoi_simd_parse_until_invalid(s: &[u8]) -> Result<(Self, usize), AtoiSimdError> { + Self::atoi_simd_parse_until_invalid_pos(s) } #[inline(always)] - fn atoi_simd_parse_skipped(s: &[u8]) -> Result { + fn atoi_simd_parse_skipped(s: &[u8]) -> Result { let mut i = 0; if *s.first().ok_or(AtoiSimdError::Empty)? == b'+' { i = 1; @@ -78,12 +78,12 @@ pub trait Parser>: Sized { i += 1; } - T::atoi_simd_parse_pos(s.get_safe_unchecked(i..)) + Self::atoi_simd_parse_pos(s.get_safe_unchecked(i..)) } } #[inline(always)] -fn atoi_simd_parse_signed + ParserNeg>(s: &[u8]) -> Result { +fn atoi_simd_parse_signed(s: &[u8]) -> Result { if *s.first().ok_or(AtoiSimdError::Empty)? == b'-' { T::atoi_simd_parse_neg(s.get_safe_unchecked(1..)) } else { @@ -92,7 +92,7 @@ fn atoi_simd_parse_signed + ParserNeg>(s: &[u8]) -> Result + ParserNeg>( +fn atoi_simd_parse_until_invalid_signed( s: &[u8], ) -> Result<(T, usize), AtoiSimdError> { if *s.first().ok_or(AtoiSimdError::Empty)? == b'-' { @@ -103,9 +103,7 @@ fn atoi_simd_parse_until_invalid_signed + ParserNeg>( } #[inline(always)] -fn atoi_simd_parse_skipped_signed + ParserNeg>( - s: &[u8], -) -> Result { +fn atoi_simd_parse_skipped_signed(s: &[u8]) -> Result { let mut neg = false; let mut i = match *s.first().ok_or(AtoiSimdError::Empty)? { b'+' => 1, @@ -128,47 +126,32 @@ fn atoi_simd_parse_skipped_signed + ParserNeg>( } } -impl Parser for u8 {} -impl Parser for u16 {} -impl Parser for u32 {} -impl Parser for usize {} -impl Parser for u64 {} -impl Parser for u128 {} - -macro_rules! impl_signed { - () => { - #[inline(always)] - fn atoi_simd_parse(s: &[u8]) -> Result { - atoi_simd_parse_signed(s) - } - - #[inline(always)] - fn atoi_simd_parse_until_invalid(s: &[u8]) -> Result<(Self, usize), AtoiSimdError> { - atoi_simd_parse_until_invalid_signed(s) - } - - #[inline(always)] - fn atoi_simd_parse_skipped(s: &[u8]) -> Result { - atoi_simd_parse_skipped_signed(s) +impl Parse for u8 {} +impl Parse for u16 {} +impl Parse for u32 {} +impl Parse for usize {} +impl Parse for u64 {} +impl Parse for u128 {} + +macro_rules! parse_impl_signed { + ($($t:ty)*) => {$( + impl Parse for $t { + #[inline(always)] + fn atoi_simd_parse(s: &[u8]) -> Result { + atoi_simd_parse_signed(s) + } + + #[inline(always)] + fn atoi_simd_parse_until_invalid(s: &[u8]) -> Result<(Self, usize), AtoiSimdError> { + atoi_simd_parse_until_invalid_signed(s) + } + + #[inline(always)] + fn atoi_simd_parse_skipped(s: &[u8]) -> Result { + atoi_simd_parse_skipped_signed(s) + } } - }; + )*}; } -impl Parser for i8 { - impl_signed!(); -} -impl Parser for i16 { - impl_signed!(); -} -impl Parser for i32 { - impl_signed!(); -} -impl Parser for isize { - impl_signed!(); -} -impl Parser for i64 { - impl_signed!(); -} -impl Parser for i128 { - impl_signed!(); -} +parse_impl_signed!(i8 i16 i32 isize i64 i128); diff --git a/src/linker/simd_32.rs b/src/linker/simd_32.rs index a69c873..60bb6a1 100644 --- a/src/linker/simd_32.rs +++ b/src/linker/simd_32.rs @@ -1,7 +1,7 @@ use super::*; use crate::simd::shared_32::*; -impl ParserPos for u8 { +impl ParsePos for u8 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked::<{ u8::MAX as u64 }>(s).map(|v| v as u8) @@ -13,7 +13,7 @@ impl ParserPos for u8 { } } -impl ParserPos for i8 { +impl ParsePos for i8 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked::<{ i8::MAX as u64 }>(s).map(|v| v as i8) @@ -25,7 +25,7 @@ impl ParserPos for i8 { } } -impl ParserNeg for i8 { +impl ParseNeg for i8 { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_simd_checked_neg::<{ i8::MIN as i64 }>(s).map(|v| v as i8) @@ -37,7 +37,7 @@ impl ParserNeg for i8 { } } -impl ParserPos for u16 { +impl ParsePos for u16 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked::<{ u16::MAX as u64 }>(s).map(|v| v as u16) @@ -49,7 +49,7 @@ impl ParserPos for u16 { } } -impl ParserPos for i16 { +impl ParsePos for i16 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked::<{ i16::MAX as u64 }>(s).map(|v| v as i16) @@ -61,7 +61,7 @@ impl ParserPos for i16 { } } -impl ParserNeg for i16 { +impl ParseNeg for i16 { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_simd_checked_neg::<{ i16::MIN as i64 }>(s).map(|v| v as i16) @@ -73,7 +73,7 @@ impl ParserNeg for i16 { } } -impl ParserPos for u32 { +impl ParsePos for u32 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked::<{ u32::MAX as u64 }>(s).map(|v| v as u32) @@ -85,7 +85,7 @@ impl ParserPos for u32 { } } -impl ParserPos for i32 { +impl ParsePos for i32 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked::<{ i32::MAX as u64 }>(s).map(|v| v as i32) @@ -97,7 +97,7 @@ impl ParserPos for i32 { } } -impl ParserNeg for i32 { +impl ParseNeg for i32 { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_simd_checked_neg::<{ i32::MIN as i64 }>(s).map(|v| v as i32) @@ -110,7 +110,7 @@ impl ParserNeg for i32 { } #[cfg(target_pointer_width = "32")] -impl ParserPos for usize { +impl ParsePos for usize { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked::<{ u32::MAX as u64 }>(s).map(|v| v as usize) @@ -123,7 +123,7 @@ impl ParserPos for usize { } #[cfg(target_pointer_width = "32")] -impl ParserPos for isize { +impl ParsePos for isize { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked::<{ i32::MAX as u64 }>(s).map(|v| v as isize) @@ -136,7 +136,7 @@ impl ParserPos for isize { } #[cfg(target_pointer_width = "32")] -impl ParserNeg for isize { +impl ParseNeg for isize { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_simd_checked_neg::<{ i32::MIN as i64 }>(s).map(|v| v as isize) diff --git a/src/linker/simd_64.rs b/src/linker/simd_64.rs index 8823183..0360fc7 100644 --- a/src/linker/simd_64.rs +++ b/src/linker/simd_64.rs @@ -2,7 +2,7 @@ use super::*; use crate::simd::shared_64::*; #[cfg(target_pointer_width = "64")] -impl ParserPos for usize { +impl ParsePos for usize { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked_u64(s).map(|v| v as usize) @@ -15,7 +15,7 @@ impl ParserPos for usize { } #[cfg(target_pointer_width = "64")] -impl ParserPos for isize { +impl ParsePos for isize { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked_i64(s).map(|v| v as isize) @@ -28,7 +28,7 @@ impl ParserPos for isize { } #[cfg(target_pointer_width = "64")] -impl ParserNeg for isize { +impl ParseNeg for isize { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_simd_checked_i64_neg(s).map(|v| v as isize) @@ -40,7 +40,7 @@ impl ParserNeg for isize { } } -impl ParserPos for u64 { +impl ParsePos for u64 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked_u64(s) @@ -52,7 +52,7 @@ impl ParserPos for u64 { } } -impl ParserPos for i64 { +impl ParsePos for i64 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked_i64(s) @@ -64,7 +64,7 @@ impl ParserPos for i64 { } } -impl ParserNeg for i64 { +impl ParseNeg for i64 { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_simd_checked_i64_neg(s) @@ -76,7 +76,7 @@ impl ParserNeg for i64 { } } -impl ParserPos for u128 { +impl ParsePos for u128 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked_u128(s) @@ -88,7 +88,7 @@ impl ParserPos for u128 { } } -impl ParserPos for i128 { +impl ParsePos for i128 { #[inline(always)] fn atoi_simd_parse_pos(s: &[u8]) -> Result { parse_simd_checked_i128(s) @@ -100,7 +100,7 @@ impl ParserPos for i128 { } } -impl ParserNeg for i128 { +impl ParseNeg for i128 { #[inline(always)] fn atoi_simd_parse_neg(s: &[u8]) -> Result { parse_simd_checked_i128_neg(s) diff --git a/src/test.rs b/src/test.rs index 96b5984..eb3d567 100644 --- a/src/test.rs +++ b/src/test.rs @@ -87,7 +87,7 @@ fn test_each_position_until_invalid( } fn parse_tester< - T: Copy + Debug + PartialEq + FromStr + Parser + ParserPos, + T: Copy + Debug + PartialEq + FromStr + Parse, const LEN: usize, const LEN_NEG: usize, I, @@ -118,7 +118,7 @@ fn parse_tester< } fn parse_until_invalid_tester< - T: Copy + Debug + PartialEq + FromStr + Parser + ParserPos, + T: Copy + Debug + PartialEq + FromStr + Parse, const LEN: usize, const LEN_NEG: usize, I,