From bb820c78aa8a91db3f3d2ee506d5324599098614 Mon Sep 17 00:00:00 2001 From: Nugine Date: Tue, 13 Dec 2022 10:35:15 +0800 Subject: [PATCH] shortcuts Related: + https://github.com/Nugine/simd/issues/28 + https://github.com/marshallpierce/rust-base64/issues/205 --- benches/base32.rs | 4 +- benches/base64.rs | 6 +-- crates/base32-simd/src/lib.rs | 47 +++++++++++++++++------ crates/base32-simd/tests/it.rs | 4 +- crates/base64-simd/src/lib.rs | 47 +++++++++++++++++------ crates/base64-simd/tests/it.rs | 4 +- crates/hex-simd/src/lib.rs | 47 +++++++++++++++++------ crates/hex-simd/tests/{tests.rs => it.rs} | 10 ++--- 8 files changed, 119 insertions(+), 50 deletions(-) rename crates/hex-simd/tests/{tests.rs => it.rs} (94%) diff --git a/benches/base32.rs b/benches/base32.rs index 526ff4b..1c10ebc 100644 --- a/benches/base32.rs +++ b/benches/base32.rs @@ -44,7 +44,7 @@ pub fn bench_decode(c: &mut Criterion) { group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic)); let cases = [16, 32, 64, 256, 1024, 4096, 65536]; - let inputs: Vec> = map_collect(cases, |n| base32_simd::BASE32.encode_type(&rand_bytes(n))); + let inputs: Vec> = map_collect(cases, |n| base32_simd::BASE32.encode_type(rand_bytes(n))); #[allow(clippy::type_complexity)] let functions: &FnGroup = &[ @@ -79,7 +79,7 @@ pub fn bench_check(c: &mut Criterion) { group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic)); let cases = [16, 32, 64, 256, 1024, 4096, 65536]; - let inputs: Vec> = map_collect(cases, |n| base32_simd::BASE32.encode_type(&rand_bytes(n))); + let inputs: Vec> = map_collect(cases, |n| base32_simd::BASE32.encode_type(rand_bytes(n))); #[allow(clippy::type_complexity)] let functions: &FnGroup = &[ diff --git a/benches/base64.rs b/benches/base64.rs index 3d15762..47638ac 100644 --- a/benches/base64.rs +++ b/benches/base64.rs @@ -60,7 +60,7 @@ pub fn bench_decode(c: &mut Criterion) { group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic)); let cases = [16, 32, 64, 256, 1024, 4096, 65536]; - let inputs: Vec> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(&rand_bytes(n))); + let inputs: Vec> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(rand_bytes(n))); #[allow(clippy::type_complexity)] let functions: &FnGroup = &[ @@ -102,7 +102,7 @@ pub fn bench_check(c: &mut Criterion) { group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic)); let cases = [16, 32, 64, 256, 1024, 4096, 65536]; - let inputs: Vec> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(&rand_bytes(n))); + let inputs: Vec> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(rand_bytes(n))); #[allow(clippy::type_complexity)] let functions: &FnGroup = &[ @@ -126,7 +126,7 @@ pub fn bench_forgiving_decode(c: &mut Criterion) { group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic)); let cases = [16, 32, 64, 256, 1024, 4096, 65536]; - let inputs: Vec> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(&rand_bytes(n))); + let inputs: Vec> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(rand_bytes(n))); #[allow(clippy::type_complexity)] let functions: &FnGroup = &[ diff --git a/crates/base32-simd/src/lib.rs b/crates/base32-simd/src/lib.rs index 3afec4f..27be7d2 100644 --- a/crates/base32-simd/src/lib.rs +++ b/crates/base32-simd/src/lib.rs @@ -8,11 +8,11 @@ //! let bytes = b"hello world"; //! let base32 = base32_simd::BASE32; //! -//! let encoded = base32.encode_type::(bytes); -//! assert_eq!(&*encoded, "NBSWY3DPEB3W64TMMQ======"); +//! let encoded = base32.encode_to_string(bytes); +//! assert_eq!(encoded, "NBSWY3DPEB3W64TMMQ======"); //! -//! let decoded = base32.decode_type::>(encoded.as_bytes()).unwrap(); -//! assert_eq!(&*decoded, bytes); +//! let decoded = base32.decode_to_vec(encoded).unwrap(); +//! assert_eq!(decoded, bytes); //! # } //! ``` //! @@ -70,6 +70,9 @@ use crate::encode::encoded_length_unchecked; use vsimd::tools::{slice_mut, slice_parts}; +#[cfg(feature = "alloc")] +use alloc::{string::String, vec::Vec}; + const BASE32_CHARSET: &[u8; 32] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; const BASE32HEX_CHARSET: &[u8; 32] = b"0123456789ABCDEFGHIJKLMNOPQRSTUV"; @@ -244,8 +247,8 @@ impl Base32 { /// Encodes bytes to a base32 string and returns a specified type. #[inline] #[must_use] - pub fn encode_type(&self, data: &[u8]) -> T { - T::from_base32_encode(self, data) + pub fn encode_type(&self, data: impl AsRef<[u8]>) -> T { + T::from_base32_encode(self, data.as_ref()) } /// Decodes a base32 string to bytes and returns a specified type. @@ -253,14 +256,14 @@ impl Base32 { /// # Errors /// This function returns `Err` if the content of `data` is invalid. #[inline] - pub fn decode_type(&self, data: &[u8]) -> Result { - T::from_base32_decode(self, data) + pub fn decode_type(&self, data: impl AsRef<[u8]>) -> Result { + T::from_base32_decode(self, data.as_ref()) } /// Encodes bytes to a base32 string and appends to a specified type. #[inline] - pub fn encode_append(&self, src: &[u8], dst: &mut T) { - T::append_base32_encode(self, src, dst); + pub fn encode_append(&self, src: impl AsRef<[u8]>, dst: &mut T) { + T::append_base32_encode(self, src.as_ref(), dst); } /// Decodes a base32 string to bytes and appends to a specified type. @@ -268,8 +271,28 @@ impl Base32 { /// # Errors /// This function returns `Err` if the content of `src` is invalid. #[inline] - pub fn decode_append(&self, src: &[u8], dst: &mut T) -> Result<(), Error> { - T::append_base32_decode(self, src, dst) + pub fn decode_append(&self, src: impl AsRef<[u8]>, dst: &mut T) -> Result<(), Error> { + T::append_base32_decode(self, src.as_ref(), dst) + } + + /// Encodes bytes to a base32 string. + #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] + #[cfg(feature = "alloc")] + #[inline] + #[must_use] + pub fn encode_to_string(&self, data: impl AsRef<[u8]>) -> String { + self.encode_type(data.as_ref()) + } + + /// Decodes a base32 string to bytes. + /// + /// # Errors + /// This function returns `Err` if the content of `data` is invalid. + #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] + #[cfg(feature = "alloc")] + #[inline] + pub fn decode_to_vec(&self, data: impl AsRef<[u8]>) -> Result, Error> { + self.decode_type(data.as_ref()) } } diff --git a/crates/base32-simd/tests/it.rs b/crates/base32-simd/tests/it.rs index 7b9931b..103f0f8 100644 --- a/crates/base32-simd/tests/it.rs +++ b/crates/base32-simd/tests/it.rs @@ -99,12 +99,12 @@ fn allocation() { let prefix = "data:;base32,"; let mut encode_buf = prefix.to_owned(); - BASE32.encode_append(src.as_bytes(), &mut encode_buf); + BASE32.encode_append(src, &mut encode_buf); assert_eq!(encode_buf, format!("{prefix}NBSWY3DPO5XXE3DE")); let mut decode_buf = b"123".to_vec(); - let src = encode_buf[prefix.len()..].as_bytes(); + let src = &encode_buf[prefix.len()..]; BASE32.decode_append(src, &mut decode_buf).unwrap(); assert_eq!(decode_buf, b"123helloworld"); diff --git a/crates/base64-simd/src/lib.rs b/crates/base64-simd/src/lib.rs index 16da8ad..59d964e 100644 --- a/crates/base64-simd/src/lib.rs +++ b/crates/base64-simd/src/lib.rs @@ -8,11 +8,11 @@ //! let bytes = b"hello world"; //! let base64 = base64_simd::STANDARD; //! -//! let encoded = base64.encode_type::(bytes); -//! assert_eq!(&*encoded, "aGVsbG8gd29ybGQ="); +//! let encoded = base64.encode_to_string(bytes); +//! assert_eq!(encoded, "aGVsbG8gd29ybGQ="); //! -//! let decoded = base64.decode_type::>(encoded.as_bytes()).unwrap(); -//! assert_eq!(&*decoded, bytes); +//! let decoded = base64.decode_to_vec(encoded).unwrap(); +//! assert_eq!(decoded, bytes); //! # } //! ``` //! @@ -75,6 +75,9 @@ use crate::encode::encoded_length_unchecked; use vsimd::tools::{slice_mut, slice_parts}; +#[cfg(feature = "alloc")] +use alloc::{string::String, vec::Vec}; + const STANDARD_CHARSET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; const URL_SAFE_CHARSET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; @@ -311,8 +314,8 @@ impl Base64 { /// Encodes bytes to a base64 string and returns a specified type. #[inline] #[must_use] - pub fn encode_type(&self, data: &[u8]) -> T { - T::from_base64_encode(self, data) + pub fn encode_type(&self, data: impl AsRef<[u8]>) -> T { + T::from_base64_encode(self, data.as_ref()) } /// Decodes a base64 string to bytes and returns a specified type. @@ -320,14 +323,14 @@ impl Base64 { /// # Errors /// This function returns `Err` if the content of `data` is invalid. #[inline] - pub fn decode_type(&self, data: &[u8]) -> Result { - T::from_base64_decode(self, data) + pub fn decode_type(&self, data: impl AsRef<[u8]>) -> Result { + T::from_base64_decode(self, data.as_ref()) } /// Encodes bytes to a base64 string and appends to a specified type. #[inline] - pub fn encode_append(&self, src: &[u8], dst: &mut T) { - T::append_base64_encode(self, src, dst); + pub fn encode_append(&self, src: impl AsRef<[u8]>, dst: &mut T) { + T::append_base64_encode(self, src.as_ref(), dst); } /// Decodes a base64 string to bytes and appends to a specified type. @@ -335,8 +338,28 @@ impl Base64 { /// # Errors /// This function returns `Err` if the content of `src` is invalid. #[inline] - pub fn decode_append(&self, src: &[u8], dst: &mut T) -> Result<(), Error> { - T::append_base64_decode(self, src, dst) + pub fn decode_append(&self, src: impl AsRef<[u8]>, dst: &mut T) -> Result<(), Error> { + T::append_base64_decode(self, src.as_ref(), dst) + } + + /// Encodes bytes to a base64 string. + #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] + #[cfg(feature = "alloc")] + #[inline] + #[must_use] + pub fn encode_to_string(&self, data: impl AsRef<[u8]>) -> String { + self.encode_type(data) + } + + /// Decodes a base64 string to bytes. + /// + /// # Errors + /// This function returns `Err` if the content of `data` is invalid. + #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] + #[cfg(feature = "alloc")] + #[inline] + pub fn decode_to_vec(&self, data: impl AsRef<[u8]>) -> Result, Error> { + self.decode_type(data) } } diff --git a/crates/base64-simd/tests/it.rs b/crates/base64-simd/tests/it.rs index 3b38fc0..5329283 100644 --- a/crates/base64-simd/tests/it.rs +++ b/crates/base64-simd/tests/it.rs @@ -56,12 +56,12 @@ fn allocation() { let prefix = "data:;base64,"; let mut encode_buf = prefix.to_owned(); - STANDARD.encode_append(src.as_bytes(), &mut encode_buf); + STANDARD.encode_append(src, &mut encode_buf); assert_eq!(encode_buf, format!("{prefix}aGVsbG93b3JsZA==")); let mut decode_buf = b"123".to_vec(); - let src = encode_buf[prefix.len()..].as_bytes(); + let src = &encode_buf[prefix.len()..]; STANDARD.decode_append(src, &mut decode_buf).unwrap(); assert_eq!(decode_buf, b"123helloworld"); diff --git a/crates/hex-simd/src/lib.rs b/crates/hex-simd/src/lib.rs index 31c8976..a1c9b0a 100644 --- a/crates/hex-simd/src/lib.rs +++ b/crates/hex-simd/src/lib.rs @@ -9,11 +9,11 @@ //! //! let bytes = b"Hello world!"; //! -//! let encoded = hex_simd::encode_type::(bytes, AsciiCase::Lower); -//! assert_eq!(&*encoded, "48656c6c6f20776f726c6421"); +//! let encoded = hex_simd::encode_to_string(bytes, AsciiCase::Lower); +//! assert_eq!(encoded, "48656c6c6f20776f726c6421"); //! -//! let decoded = hex_simd::decode_type::>(encoded.as_bytes()).unwrap(); -//! assert_eq!(&*decoded, bytes); +//! let decoded = hex_simd::decode_to_vec(encoded).unwrap(); +//! assert_eq!(decoded, bytes); //! # } //! ``` //! @@ -65,6 +65,9 @@ pub use vsimd::ascii::AsciiCase; use vsimd::tools::{slice_mut, slice_parts}; +#[cfg(feature = "alloc")] +use alloc::{string::String, vec::Vec}; + /// Calculates the encoded length. /// /// # Panics @@ -182,8 +185,8 @@ pub trait FromHexEncode: Sized { /// Encodes bytes to a hex string and returns a specified type. #[inline] #[must_use] -pub fn encode_type(data: &[u8], case: AsciiCase) -> T { - T::from_hex_encode(data, case) +pub fn encode_type(data: impl AsRef<[u8]>, case: AsciiCase) -> T { + T::from_hex_encode(data.as_ref(), case) } /// Decodes a hex string to bytes case-insensitively and returns a specified type. @@ -191,8 +194,8 @@ pub fn encode_type(data: &[u8], case: AsciiCase) -> T { /// # Errors /// This function returns `Err` if the content of `data` is invalid. #[inline] -pub fn decode_type(data: &[u8]) -> Result { - T::from_hex_decode(data) +pub fn decode_type(data: impl AsRef<[u8]>) -> Result { + T::from_hex_decode(data.as_ref()) } /// Types that can append a hex string. @@ -212,8 +215,8 @@ pub trait AppendHexDecode: FromHexDecode { /// Encodes bytes to a hex string and appends to a specified type. #[inline] -pub fn encode_append(src: &[u8], dst: &mut T, case: AsciiCase) { - T::append_hex_encode(src, dst, case); +pub fn encode_append(src: impl AsRef<[u8]>, dst: &mut T, case: AsciiCase) { + T::append_hex_encode(src.as_ref(), dst, case); } /// Decodes a hex string to bytes case-insensitively and appends to a specified type. @@ -221,6 +224,26 @@ pub fn encode_append(src: &[u8], dst: &mut T, case: AsciiCas /// # Errors /// This function returns `Err` if the content of `src` is invalid. #[inline] -pub fn decode_append(src: &[u8], dst: &mut T) -> Result<(), Error> { - T::append_hex_decode(src, dst) +pub fn decode_append(src: impl AsRef<[u8]>, dst: &mut T) -> Result<(), Error> { + T::append_hex_decode(src.as_ref(), dst) +} + +/// Encodes bytes to a hex string. +#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] +#[cfg(feature = "alloc")] +#[inline] +#[must_use] +pub fn encode_to_string(data: impl AsRef<[u8]>, case: AsciiCase) -> String { + encode_type(data, case) +} + +/// Decodes a hex string to bytes case-insensitively. +/// +/// # Errors +/// This function returns `Err` if the content of `data` is invalid. +#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] +#[cfg(feature = "alloc")] +#[inline] +pub fn decode_to_vec(data: impl AsRef<[u8]>) -> Result, Error> { + decode_type(data) } diff --git a/crates/hex-simd/tests/tests.rs b/crates/hex-simd/tests/it.rs similarity index 94% rename from crates/hex-simd/tests/tests.rs rename to crates/hex-simd/tests/it.rs index 27b186e..d7bb69b 100644 --- a/crates/hex-simd/tests/tests.rs +++ b/crates/hex-simd/tests/it.rs @@ -34,13 +34,13 @@ fn as_str() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] fn allocation() { { - let src = "hello".as_bytes(); + let src = "hello"; let ans: String = hex_simd::encode_type(src, AsciiCase::Lower); assert_eq!(&*ans, "68656c6c6f"); - let ans: Vec = hex_simd::decode_type(ans.as_bytes()).unwrap(); - assert_eq!(&*ans, src); + let ans: Vec = hex_simd::decode_type(ans).unwrap(); + assert_eq!(&*ans, src.as_bytes()); } { @@ -48,12 +48,12 @@ fn allocation() { let prefix = "0x"; let mut encode_buf = prefix.to_owned(); - hex_simd::encode_append(&src, &mut encode_buf, AsciiCase::Lower); + hex_simd::encode_append(src, &mut encode_buf, AsciiCase::Lower); assert_eq!(encode_buf, format!("{prefix}010203")); let mut decode_buf = b"123".to_vec(); - let src = encode_buf[prefix.len()..].as_bytes(); + let src = &encode_buf[prefix.len()..]; hex_simd::decode_append(src, &mut decode_buf).unwrap(); assert_eq!(decode_buf, b"123\x01\x02\x03");