diff --git a/const_format/src/__str_methods/str_split.rs b/const_format/src/__str_methods/str_split.rs index c705d23..05566dd 100644 --- a/const_format/src/__str_methods/str_split.rs +++ b/const_format/src/__str_methods/str_split.rs @@ -45,11 +45,7 @@ impl SplitInput { } } -pub const fn count_splits( - SplitInput { - str, pattern, .. - }: SplitInput, -) -> usize { +pub const fn count_splits(SplitInput { str, pattern, .. }: SplitInput) -> usize { let mut count = 1; match pattern.normalize() { diff --git a/const_format/src/macros/helper_macros.rs b/const_format/src/macros/helper_macros.rs index a927f78..7c46340 100644 --- a/const_format/src/macros/helper_macros.rs +++ b/const_format/src/macros/helper_macros.rs @@ -15,13 +15,12 @@ macro_rules! identity { ($($tt:tt)*) => { $($tt)* }; } - #[cfg(not(feature = "rust_1_83"))] #[doc(hidden)] #[macro_export] macro_rules! __str_const { ($e:expr) => { - $crate::pmr::__AssertStr {x: $e}.x + $crate::pmr::__AssertStr { x: $e }.x }; } @@ -30,7 +29,7 @@ macro_rules! __str_const { #[macro_export] macro_rules! __str_const { ($e:expr) => { - const { $crate::pmr::__AssertStr {x: $e}.x } + const { $crate::pmr::__AssertStr { x: $e }.x } }; } @@ -39,7 +38,7 @@ macro_rules! __str_const { #[macro_export] macro_rules! __const { ($ty:ty => $e:expr) => { - $crate::pmr::__AssertType::<$ty> {x: $e}.x + $crate::pmr::__AssertType::<$ty> { x: $e }.x }; } @@ -48,11 +47,10 @@ macro_rules! __const { #[macro_export] macro_rules! __const { ($ty:ty => $e:expr) => { - const { $crate::pmr::__AssertType::<$ty> {x: $e}.x } + const { $crate::pmr::__AssertType::<$ty> { x: $e }.x } }; } - #[doc(hidden)] #[macro_export] macro_rules! iter_copy_slice{ diff --git a/const_format/src/macros/map_ascii_case.rs b/const_format/src/macros/map_ascii_case.rs index 00cf760..979b8fe 100644 --- a/const_format/src/macros/map_ascii_case.rs +++ b/const_format/src/macros/map_ascii_case.rs @@ -75,19 +75,21 @@ /// ``` #[macro_export] macro_rules! map_ascii_case { - ($case:expr, $str:expr) => {$crate::__str_const! {{ - const S_OSRCTFL4A: &$crate::pmr::str = $str; - const CASE_OSRCTFL4A: $crate::Case = $case; - { - const L: $crate::pmr::usize = - $crate::__ascii_case_conv::size_after_conversion(CASE_OSRCTFL4A, S_OSRCTFL4A); + ($case:expr, $str:expr) => { + $crate::__str_const! {{ + const S_OSRCTFL4A: &$crate::pmr::str = $str; + const CASE_OSRCTFL4A: $crate::Case = $case; + { + const L: $crate::pmr::usize = + $crate::__ascii_case_conv::size_after_conversion(CASE_OSRCTFL4A, S_OSRCTFL4A); - const OB: &[$crate::pmr::u8; L] = - &$crate::__ascii_case_conv::convert_str::(CASE_OSRCTFL4A, S_OSRCTFL4A); + const OB: &[$crate::pmr::u8; L] = + &$crate::__ascii_case_conv::convert_str::(CASE_OSRCTFL4A, S_OSRCTFL4A); - const OS: &$crate::pmr::str = unsafe { $crate::__priv_transmute_bytes_to_str!(OB) }; + const OS: &$crate::pmr::str = unsafe { $crate::__priv_transmute_bytes_to_str!(OB) }; - OS - } - }}}; + OS + } + }} + }; } diff --git a/const_format/src/macros/str_methods.rs b/const_format/src/macros/str_methods.rs index 07d12e0..79d68c2 100644 --- a/const_format/src/macros/str_methods.rs +++ b/const_format/src/macros/str_methods.rs @@ -65,19 +65,21 @@ /// [`str::replace`]: https://doc.rust-lang.org/std/primitive.str.html#method.replace #[macro_export] macro_rules! str_replace { - ($input:expr, $pattern:expr, $replace_with:expr $(,)*) => {$crate::__str_const! {{ - const ARGS_OSRCTFL4A: $crate::__str_methods::ReplaceInput = - $crate::__str_methods::ReplaceInputConv($input, $pattern, $replace_with).conv(); + ($input:expr, $pattern:expr, $replace_with:expr $(,)*) => { + $crate::__str_const! {{ + const ARGS_OSRCTFL4A: $crate::__str_methods::ReplaceInput = + $crate::__str_methods::ReplaceInputConv($input, $pattern, $replace_with).conv(); - { - const OB: &[$crate::pmr::u8; ARGS_OSRCTFL4A.replace_length()] = - &ARGS_OSRCTFL4A.replace(); + { + const OB: &[$crate::pmr::u8; ARGS_OSRCTFL4A.replace_length()] = + &ARGS_OSRCTFL4A.replace(); - const OS: &$crate::pmr::str = unsafe { $crate::__priv_transmute_bytes_to_str!(OB) }; + const OS: &$crate::pmr::str = unsafe { $crate::__priv_transmute_bytes_to_str!(OB) }; - OS - } - }}}; + OS + } + }} + }; } /// Creates a `&'static str` by repeating a `&'static str` constant `times` times @@ -121,31 +123,33 @@ const_format::str_repeat!("hello", usize::MAX.wrapping_add(4)); )] #[macro_export] macro_rules! str_repeat { - ($string:expr, $times:expr $(,)*) => {$crate::__str_const! {{ - const P_OSRCTFL4A: &$crate::__str_methods::StrRepeatArgs = - &$crate::__str_methods::StrRepeatArgs($string, $times); + ($string:expr, $times:expr $(,)*) => { + $crate::__str_const! {{ + const P_OSRCTFL4A: &$crate::__str_methods::StrRepeatArgs = + &$crate::__str_methods::StrRepeatArgs($string, $times); - { - use $crate::__hidden_utils::PtrToRef; - use $crate::pmr::{str, transmute, u8}; + { + use $crate::__hidden_utils::PtrToRef; + use $crate::pmr::{str, transmute, u8}; - const P: &$crate::__str_methods::StrRepeatArgs = P_OSRCTFL4A; + const P: &$crate::__str_methods::StrRepeatArgs = P_OSRCTFL4A; - $crate::pmr::respan_to! { - ($string) - const _ASSERT_VALID_LEN: () = P.assert_valid(); - } + $crate::pmr::respan_to! { + ($string) + const _ASSERT_VALID_LEN: () = P.assert_valid(); + } - const OUT_B: &[u8; P.out_len] = &unsafe { - let ptr = P.str.as_ptr() as *const [u8; P.str_len]; - transmute::<[[u8; P.str_len]; P.repeat], [u8; P.out_len]>( - [*PtrToRef { ptr }.reff; P.repeat], - ) - }; - const OUT_S: &str = unsafe { $crate::__priv_transmute_bytes_to_str!(OUT_B) }; - OUT_S - } - }}}; + const OUT_B: &[u8; P.out_len] = &unsafe { + let ptr = P.str.as_ptr() as *const [u8; P.str_len]; + transmute::<[[u8; P.str_len]; P.repeat], [u8; P.out_len]>( + [*PtrToRef { ptr }.reff; P.repeat], + ) + }; + const OUT_S: &str = unsafe { $crate::__priv_transmute_bytes_to_str!(OUT_B) }; + OUT_S + } + }} + }; } /// Replaces a substring in a `&'static str` constant. @@ -256,15 +260,17 @@ assert_eq!( /// [`SplicedStr`]: ./struct.SplicedStr.html #[macro_export] macro_rules! str_splice { - ($string:expr, $index:expr, $insert:expr $(,)*) => {$crate::__const! { - $crate::__str_methods::SplicedStr => - $crate::__str_splice!($string, $index, $insert) - }}; + ($string:expr, $index:expr, $insert:expr $(,)*) => { + $crate::__const! { + $crate::__str_methods::SplicedStr => + $crate::__str_splice!($string, $index, $insert) + } + }; } /// Alternative version of [`str_split`] which only returns the string /// with the applied replacement. -/// +/// /// # Example /// /// ```rust @@ -289,9 +295,11 @@ macro_rules! str_splice { /// #[macro_export] macro_rules! str_splice_out { - ($string:expr, $index:expr, $insert:expr $(,)*) => {$crate::__str_const ! { - $crate::__str_splice!($string, $index, $insert).output - }}; + ($string:expr, $index:expr, $insert:expr $(,)*) => { + $crate::__str_const! { + $crate::__str_splice!($string, $index, $insert).output + } + }; } #[doc(hidden)] @@ -348,7 +356,7 @@ macro_rules! __str_splice { OUT } - }} + }}; } /// Indexes a `&'static str` constant. @@ -418,36 +426,38 @@ assert_eq!(const_format::str_index!("効率的", 4..6), "率"); /// #[macro_export] macro_rules! str_index { - ($string:expr, $index:expr $(,)*) => {$crate::__str_const! {{ - const P_OSRCTFL4A: $crate::__str_methods::StrIndexArgs = - $crate::__str_methods::StrIndexArgsConv($string, $index).conv(); + ($string:expr, $index:expr $(,)*) => { + $crate::__str_const! {{ + const P_OSRCTFL4A: $crate::__str_methods::StrIndexArgs = + $crate::__str_methods::StrIndexArgsConv($string, $index).conv(); + + { + $crate::pmr::respan_to! { + ($string) + const _ASSERT_VALID_INDEX: () = + P_OSRCTFL4A.index_validity.assert_valid(); + } - { - $crate::pmr::respan_to! { - ($string) - const _ASSERT_VALID_INDEX: () = - P_OSRCTFL4A.index_validity.assert_valid(); - } + use $crate::__hidden_utils::PtrToRef; + use $crate::__str_methods::DecomposedString; + type DecompIn = DecomposedString< + [u8; P_OSRCTFL4A.used_rstart], + [u8; P_OSRCTFL4A.used_rlen], + [u8; 0], + >; - use $crate::__hidden_utils::PtrToRef; - use $crate::__str_methods::DecomposedString; - type DecompIn = DecomposedString< - [u8; P_OSRCTFL4A.used_rstart], - [u8; P_OSRCTFL4A.used_rlen], - [u8; 0], - >; - - const OUT: &'static str = unsafe { - let input = PtrToRef { - ptr: P_OSRCTFL4A.str.as_ptr() as *const DecompIn, - } - .reff; - $crate::__priv_transmute_raw_bytes_to_str!(&input.middle) - }; + const OUT: &'static str = unsafe { + let input = PtrToRef { + ptr: P_OSRCTFL4A.str.as_ptr() as *const DecompIn, + } + .reff; + $crate::__priv_transmute_raw_bytes_to_str!(&input.middle) + }; - OUT - } - }}}; + OUT + } + }} + }; } /// Indexes a `&'static str` constant, @@ -515,36 +525,38 @@ assert_eq!(const_format::str_get!("効率的", 4..6), None); /// #[macro_export] macro_rules! str_get { - ($string:expr, $index:expr $(,)*) => {$crate::__const! { - $crate::pmr::Option<&'static $crate::pmr::str> => { - const P_OSRCTFL4A: $crate::__str_methods::StrIndexArgs = - $crate::__str_methods::StrIndexArgsConv($string, $index).conv(); - - { - use $crate::__hidden_utils::PtrToRef; - use $crate::__str_methods::DecomposedString; - type DecompIn = DecomposedString< - [u8; P_OSRCTFL4A.used_rstart], - [u8; P_OSRCTFL4A.used_rlen], - [u8; 0], - >; - - const OUT: $crate::pmr::Option<&'static $crate::pmr::str> = unsafe { - if P_OSRCTFL4A.index_validity.is_valid() { - let input = PtrToRef { - ptr: P_OSRCTFL4A.str.as_ptr() as *const DecompIn, + ($string:expr, $index:expr $(,)*) => { + $crate::__const! { + $crate::pmr::Option<&'static $crate::pmr::str> => { + const P_OSRCTFL4A: $crate::__str_methods::StrIndexArgs = + $crate::__str_methods::StrIndexArgsConv($string, $index).conv(); + + { + use $crate::__hidden_utils::PtrToRef; + use $crate::__str_methods::DecomposedString; + type DecompIn = DecomposedString< + [u8; P_OSRCTFL4A.used_rstart], + [u8; P_OSRCTFL4A.used_rlen], + [u8; 0], + >; + + const OUT: $crate::pmr::Option<&'static $crate::pmr::str> = unsafe { + if P_OSRCTFL4A.index_validity.is_valid() { + let input = PtrToRef { + ptr: P_OSRCTFL4A.str.as_ptr() as *const DecompIn, + } + .reff; + + $crate::pmr::Some($crate::__priv_transmute_raw_bytes_to_str!(&input.middle)) + } else { + $crate::pmr::None } - .reff; - - $crate::pmr::Some($crate::__priv_transmute_raw_bytes_to_str!(&input.middle)) - } else { - $crate::pmr::None - } - }; + }; - OUT - } - }}}; + OUT + } + }} + }; } /// Splits `$string` (a `&'static str` constant) with `$splitter`, @@ -621,23 +633,22 @@ macro_rules! str_split { }}; } - /// Version of [`str_split`] which evaluates to a `&'static [&'static str]`. -/// +/// /// # Example -/// +/// /// Using the currently unstable [`inline_const_pat`] feature to use this macro in patterns: -/// +/// /// ```rust /// # /* /// #![feature(inline_const_pat)] /// # */ -/// +/// /// use const_format::str_split_pat; -/// +/// /// assert!(is_it(&["foo", "bar", "baz"])); /// assert!(!is_it(&["foo", "bar"])); -/// +/// /// fn is_it(slice: &[&str]) -> bool { /// const SEP: char = ' '; /// # /* @@ -646,19 +657,21 @@ macro_rules! str_split { /// # slice == str_split_pat!("foo bar baz", SEP) /// } /// ``` -/// +/// /// [`inline_const_pat`]: https://doc.rust-lang.org/1.83.0/unstable-book/language-features/inline-const-pat.html #[macro_export] #[cfg(feature = "rust_1_64")] #[cfg_attr(feature = "__docsrs", doc(cfg(feature = "rust_1_83")))] macro_rules! str_split_pat { - ($string:expr, $splitter:expr $(,)?) => {$crate::__const!{&'static [&'static $crate::pmr::str] => { - const ARGS_OSRCTFL4A: $crate::__str_methods::SplitInput = - $crate::__str_methods::SplitInputConv($string, $splitter).conv(); - - { - const OB: [&$crate::pmr::str; ARGS_OSRCTFL4A.length()] = ARGS_OSRCTFL4A.split_it(); - &OB - } - }}}; + ($string:expr, $splitter:expr $(,)?) => { + $crate::__const! {&'static [&'static $crate::pmr::str] => { + const ARGS_OSRCTFL4A: $crate::__str_methods::SplitInput = + $crate::__str_methods::SplitInputConv($string, $splitter).conv(); + + { + const OB: [&$crate::pmr::str; ARGS_OSRCTFL4A.length()] = ARGS_OSRCTFL4A.split_it(); + &OB + } + }} + }; } diff --git a/const_format/tests/misc_tests/derive_tests.rs b/const_format/tests/misc_tests/derive_tests.rs index ec3ae4a..9f25d95 100644 --- a/const_format/tests/misc_tests/derive_tests.rs +++ b/const_format/tests/misc_tests/derive_tests.rs @@ -30,11 +30,11 @@ struct Braced { " Tupled where U: 'static,", ))] struct Tupled( - u32, - #[cdeb(ignore)] + u32, + #[cdeb(ignore)] #[allow(dead_code)] - Option<&'static str>, - T, + Option<&'static str>, + T, PhantomData, ); @@ -108,7 +108,7 @@ enum Enum { }, Tupled( u32, - #[cdeb(ignore)] + #[cdeb(ignore)] #[allow(dead_code)] Option<&'static str>, u32, diff --git a/const_format/tests/misc_tests/inline_const_pattern_tests.rs b/const_format/tests/misc_tests/inline_const_pattern_tests.rs index aa9fd0e..d3d6d8d 100644 --- a/const_format/tests/misc_tests/inline_const_pattern_tests.rs +++ b/const_format/tests/misc_tests/inline_const_pattern_tests.rs @@ -1,20 +1,10 @@ use crate::cfmt_b; use crate::cfmt_b::{ - concatc, - concatcp, - formatc, - formatcp, - map_ascii_case, - str_get, - str_index, - str_repeat, - str_replace, - str_splice_out, - str_split_pat, + concatc, concatcp, formatc, formatcp, map_ascii_case, str_get, str_index, str_repeat, + str_replace, str_splice_out, str_split_pat, }; - #[test] fn concatc_inline_pat_tests() { assert!(matches!("foo", concatc!("fo", "o"))); @@ -42,7 +32,10 @@ fn formatcp_inline_pat_tests() { #[test] fn map_ascii_case_inline_pat_tests() { assert!(matches!("foo", map_ascii_case!(cfmt_b::Case::Lower, "FOO"))); - assert!(!matches!("bar", map_ascii_case!(cfmt_b::Case::Upper, "bar"))); + assert!(!matches!( + "bar", + map_ascii_case!(cfmt_b::Case::Upper, "bar") + )); } #[test] @@ -77,7 +70,12 @@ fn str_replace_inline_pat_tests() { #[test] fn str_split_pat_inline_pat_tests() { - assert!(matches!(&["foo", "bar", "baz"][..], str_split_pat!("foo bar baz", " "))); - assert!(!matches!(&["foo", "bar", "baz"][..], str_split_pat!("foo bar", " "))); + assert!(matches!( + &["foo", "bar", "baz"][..], + str_split_pat!("foo bar baz", " ") + )); + assert!(!matches!( + &["foo", "bar", "baz"][..], + str_split_pat!("foo bar", " ") + )); } - diff --git a/const_format/tests/str_methods_modules/str_splice.rs b/const_format/tests/str_methods_modules/str_splice.rs index 45c29bc..2ae3fc7 100644 --- a/const_format/tests/str_methods_modules/str_splice.rs +++ b/const_format/tests/str_methods_modules/str_splice.rs @@ -42,7 +42,6 @@ fn replacements() { assert_eq!(str_splice!("abcde", 2..4, "hello"), ss("abhelloe", "cd")); } - #[test] fn splice_out_ranges() { assert_eq!(str_splice_out!(IN, 2, RW), "ab_.-defghij"); diff --git a/const_format/tests/str_methods_modules/str_split_tests.rs b/const_format/tests/str_methods_modules/str_split_tests.rs index 1f4cb6d..380eb8d 100644 --- a/const_format/tests/str_methods_modules/str_split_tests.rs +++ b/const_format/tests/str_methods_modules/str_split_tests.rs @@ -5,8 +5,14 @@ fn test_str_split_pat_basic_equivalence() { assert_eq!(str_split_pat!("fob", "XY"), &["fob"][..]); assert_eq!(str_split_pat!("XYfob", "XY"), &["", "fob"][..]); assert_eq!(str_split_pat!("XYfobXY", "XY"), &["", "fob", ""][..]); - assert_eq!(str_split_pat!("fooXYbarXYbaz", "XY"), &["foo", "bar", "baz"][..]); - assert_eq!(str_split_pat!("fooXY bar XYbaz", "XY"), &["foo", " bar ", "baz"][..]); + assert_eq!( + str_split_pat!("fooXYbarXYbaz", "XY"), + &["foo", "bar", "baz"][..] + ); + assert_eq!( + str_split_pat!("fooXY bar XYbaz", "XY"), + &["foo", " bar ", "baz"][..] + ); } #[test]