pub use const_str_proc_macro::format_parts;
#[cfg_attr(docsrs, doc(cfg(feature = "proc")))]
#[macro_export]
macro_rules! format {
($fmt: literal $($args:tt)*) => {{
use ::core::primitive::{str, usize};
use $crate::__ctfe::FmtSpec;
#[allow(unused_imports)]
use $crate::{__fmt_debug, __fmt_display, __fmt_lowerhex, __fmt_upperhex, __fmt_binary};
const STRS: &[&str] = $crate::__proc::format_parts!($fmt $($args)*);
const OUTPUT_LEN: usize = $crate::__ctfe::Concat(STRS).output_len();
const OUTPUT_BUF: $crate::__ctfe::StrBuf<OUTPUT_LEN> = $crate::__ctfe::Concat(STRS).const_eval();
OUTPUT_BUF.as_str()
}};
}
#[allow(clippy::uninlined_format_args)]
#[test]
fn test_const_format() {
use crate::format as const_format;
{
const A: usize = 1;
const X: &str = const_format!("{:?}", A);
let ans = std::format!("{:?}", A);
assert_eq!(X, ans);
}
{
const A: bool = true;
const B: bool = false;
const X: &str = const_format!("{1:?} {0:?} {:?}", A, B);
let ans = std::format!("{1:?} {0:?} {:?}", A, B);
assert_eq!(X, ans);
}
{
const A: char = '我';
const X: &str = const_format!("{a:?} {0}", A, a = A);
let ans = std::format!("{a:?} {0}", A, a = A);
assert_eq!(X, ans);
}
{
const A: &str = "团长\0\t\r\n\"'and希望之花";
const X: &str = const_format!("{:?}", A);
let ans = format!("{:?}", A);
assert_eq!(X, ans)
}
{
const A: u32 = 42;
const X: &str = const_format!("{0:x} {0:X} {0:#x} {0:#X} {0:b} {0:#b}", A);
let ans = std::format!("{0:x} {0:X} {0:#x} {0:#X} {0:b} {0:#b}", A);
assert_eq!(X, ans)
}
{
const A: i32 = -42;
const X: &str = const_format!("{A:x} {A:X} {A:#x} {A:#X} {A:b} {A:#b}");
let ans = std::format!("{0:x} {0:X} {0:#x} {0:#X} {0:b} {0:#b}", A);
assert_eq!(X, ans)
}
}