Skip to content

Commit

Permalink
shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed Dec 13, 2022
1 parent 7d1245f commit bb820c7
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 50 deletions.
4 changes: 2 additions & 2 deletions benches/base32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>> = map_collect(cases, |n| base32_simd::BASE32.encode_type(&rand_bytes(n)));
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base32_simd::BASE32.encode_type(rand_bytes(n)));

#[allow(clippy::type_complexity)]
let functions: &FnGroup<fn(&[u8], &mut [u8])> = &[
Expand Down Expand Up @@ -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<Vec<u8>> = map_collect(cases, |n| base32_simd::BASE32.encode_type(&rand_bytes(n)));
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base32_simd::BASE32.encode_type(rand_bytes(n)));

#[allow(clippy::type_complexity)]
let functions: &FnGroup<fn(&[u8])> = &[
Expand Down
6 changes: 3 additions & 3 deletions benches/base64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(&rand_bytes(n)));
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(rand_bytes(n)));

#[allow(clippy::type_complexity)]
let functions: &FnGroup<fn(&[u8], &mut [u8])> = &[
Expand Down Expand Up @@ -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<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(&rand_bytes(n)));
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(rand_bytes(n)));

#[allow(clippy::type_complexity)]
let functions: &FnGroup<fn(&[u8])> = &[
Expand All @@ -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<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(&rand_bytes(n)));
let inputs: Vec<Vec<u8>> = map_collect(cases, |n| base64_simd::STANDARD.encode_type(rand_bytes(n)));

#[allow(clippy::type_complexity)]
let functions: &FnGroup<fn(&[u8], &mut [u8])> = &[
Expand Down
47 changes: 35 additions & 12 deletions crates/base32-simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
//! let bytes = b"hello world";
//! let base32 = base32_simd::BASE32;
//!
//! let encoded = base32.encode_type::<String>(bytes);
//! assert_eq!(&*encoded, "NBSWY3DPEB3W64TMMQ======");
//! let encoded = base32.encode_to_string(bytes);
//! assert_eq!(encoded, "NBSWY3DPEB3W64TMMQ======");
//!
//! let decoded = base32.decode_type::<Vec<u8>>(encoded.as_bytes()).unwrap();
//! assert_eq!(&*decoded, bytes);
//! let decoded = base32.decode_to_vec(encoded).unwrap();
//! assert_eq!(decoded, bytes);
//! # }
//! ```
//!
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -244,32 +247,52 @@ impl Base32 {
/// Encodes bytes to a base32 string and returns a specified type.
#[inline]
#[must_use]
pub fn encode_type<T: FromBase32Encode>(&self, data: &[u8]) -> T {
T::from_base32_encode(self, data)
pub fn encode_type<T: FromBase32Encode>(&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.
///
/// # Errors
/// This function returns `Err` if the content of `data` is invalid.
#[inline]
pub fn decode_type<T: FromBase32Decode>(&self, data: &[u8]) -> Result<T, Error> {
T::from_base32_decode(self, data)
pub fn decode_type<T: FromBase32Decode>(&self, data: impl AsRef<[u8]>) -> Result<T, Error> {
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<T: AppendBase32Encode>(&self, src: &[u8], dst: &mut T) {
T::append_base32_encode(self, src, dst);
pub fn encode_append<T: AppendBase32Encode>(&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.
///
/// # Errors
/// This function returns `Err` if the content of `src` is invalid.
#[inline]
pub fn decode_append<T: AppendBase32Decode>(&self, src: &[u8], dst: &mut T) -> Result<(), Error> {
T::append_base32_decode(self, src, dst)
pub fn decode_append<T: AppendBase32Decode>(&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<Vec<u8>, Error> {
self.decode_type(data.as_ref())
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/base32-simd/tests/it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
47 changes: 35 additions & 12 deletions crates/base64-simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
//! let bytes = b"hello world";
//! let base64 = base64_simd::STANDARD;
//!
//! let encoded = base64.encode_type::<String>(bytes);
//! assert_eq!(&*encoded, "aGVsbG8gd29ybGQ=");
//! let encoded = base64.encode_to_string(bytes);
//! assert_eq!(encoded, "aGVsbG8gd29ybGQ=");
//!
//! let decoded = base64.decode_type::<Vec<u8>>(encoded.as_bytes()).unwrap();
//! assert_eq!(&*decoded, bytes);
//! let decoded = base64.decode_to_vec(encoded).unwrap();
//! assert_eq!(decoded, bytes);
//! # }
//! ```
//!
Expand Down Expand Up @@ -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-_";

Expand Down Expand Up @@ -311,32 +314,52 @@ impl Base64 {
/// Encodes bytes to a base64 string and returns a specified type.
#[inline]
#[must_use]
pub fn encode_type<T: FromBase64Encode>(&self, data: &[u8]) -> T {
T::from_base64_encode(self, data)
pub fn encode_type<T: FromBase64Encode>(&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.
///
/// # Errors
/// This function returns `Err` if the content of `data` is invalid.
#[inline]
pub fn decode_type<T: FromBase64Decode>(&self, data: &[u8]) -> Result<T, Error> {
T::from_base64_decode(self, data)
pub fn decode_type<T: FromBase64Decode>(&self, data: impl AsRef<[u8]>) -> Result<T, Error> {
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<T: AppendBase64Encode>(&self, src: &[u8], dst: &mut T) {
T::append_base64_encode(self, src, dst);
pub fn encode_append<T: AppendBase64Encode>(&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.
///
/// # Errors
/// This function returns `Err` if the content of `src` is invalid.
#[inline]
pub fn decode_append<T: AppendBase64Decode>(&self, src: &[u8], dst: &mut T) -> Result<(), Error> {
T::append_base64_decode(self, src, dst)
pub fn decode_append<T: AppendBase64Decode>(&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<Vec<u8>, Error> {
self.decode_type(data)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/base64-simd/tests/it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
47 changes: 35 additions & 12 deletions crates/hex-simd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
//!
//! let bytes = b"Hello world!";
//!
//! let encoded = hex_simd::encode_type::<String>(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::<Vec<u8>>(encoded.as_bytes()).unwrap();
//! assert_eq!(&*decoded, bytes);
//! let decoded = hex_simd::decode_to_vec(encoded).unwrap();
//! assert_eq!(decoded, bytes);
//! # }
//! ```
//!
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -182,17 +185,17 @@ pub trait FromHexEncode: Sized {
/// Encodes bytes to a hex string and returns a specified type.
#[inline]
#[must_use]
pub fn encode_type<T: FromHexEncode>(data: &[u8], case: AsciiCase) -> T {
T::from_hex_encode(data, case)
pub fn encode_type<T: FromHexEncode>(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.
///
/// # Errors
/// This function returns `Err` if the content of `data` is invalid.
#[inline]
pub fn decode_type<T: FromHexDecode>(data: &[u8]) -> Result<T, Error> {
T::from_hex_decode(data)
pub fn decode_type<T: FromHexDecode>(data: impl AsRef<[u8]>) -> Result<T, Error> {
T::from_hex_decode(data.as_ref())
}

/// Types that can append a hex string.
Expand All @@ -212,15 +215,35 @@ pub trait AppendHexDecode: FromHexDecode {

/// Encodes bytes to a hex string and appends to a specified type.
#[inline]
pub fn encode_append<T: AppendHexEncode>(src: &[u8], dst: &mut T, case: AsciiCase) {
T::append_hex_encode(src, dst, case);
pub fn encode_append<T: AppendHexEncode>(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.
///
/// # Errors
/// This function returns `Err` if the content of `src` is invalid.
#[inline]
pub fn decode_append<T: AppendHexDecode>(src: &[u8], dst: &mut T) -> Result<(), Error> {
T::append_hex_decode(src, dst)
pub fn decode_append<T: AppendHexDecode>(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<Vec<u8>, Error> {
decode_type(data)
}
10 changes: 5 additions & 5 deletions crates/hex-simd/tests/tests.rs → crates/hex-simd/tests/it.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ 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<u8> = hex_simd::decode_type(ans.as_bytes()).unwrap();
assert_eq!(&*ans, src);
let ans: Vec<u8> = hex_simd::decode_type(ans).unwrap();
assert_eq!(&*ans, src.as_bytes());
}

{
let src = [1, 2, 3];
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");
Expand Down

0 comments on commit bb820c7

Please sign in to comment.