mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-03-24 03:35:13 +08:00
Allow sprintf! macros to accept format string only
Some checks are pending
make fish_run_tests / ubuntu (push) Waiting to run
make fish_run_tests / ubuntu-32bit-static-pcre2 (push) Waiting to run
make fish_run_tests / ubuntu-asan (push) Waiting to run
make fish_run_tests / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
Some checks are pending
make fish_run_tests / ubuntu (push) Waiting to run
make fish_run_tests / ubuntu-32bit-static-pcre2 (push) Waiting to run
make fish_run_tests / ubuntu-asan (push) Waiting to run
make fish_run_tests / macos (push) Waiting to run
Rust checks / rustfmt (push) Waiting to run
Rust checks / clippy (push) Waiting to run
This allows something like `sprintf!("foo")`. Previously this was a compile time error. Fixes #11243
This commit is contained in:
parent
ad0feab86c
commit
7d3b157f13
@ -32,13 +32,12 @@ macro_rules! sprintf {
|
|||||||
// Write to a newly allocated String, and return it.
|
// Write to a newly allocated String, and return it.
|
||||||
// This panics if the format string or arguments are invalid.
|
// This panics if the format string or arguments are invalid.
|
||||||
(
|
(
|
||||||
$fmt:expr, // Format string, which should implement FormatString.
|
$fmt:expr // Format string, which should implement FormatString.
|
||||||
$($arg:expr),* // arguments
|
$(, $($arg:expr),*)? // arguments
|
||||||
$(,)? // optional trailing comma
|
|
||||||
) => {
|
) => {
|
||||||
{
|
{
|
||||||
let mut target = String::new();
|
let mut target = String::new();
|
||||||
$crate::sprintf!(=> &mut target, $fmt, $($arg),*);
|
$crate::sprintf!(=> &mut target, $fmt $(, $($arg),*)?);
|
||||||
target
|
target
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -47,9 +46,8 @@ macro_rules! sprintf {
|
|||||||
// The target should implement std::fmt::Write.
|
// The target should implement std::fmt::Write.
|
||||||
(
|
(
|
||||||
=> $target:expr, // target string
|
=> $target:expr, // target string
|
||||||
$fmt:expr, // format string
|
$fmt:expr // format string
|
||||||
$($arg:expr),* // arguments
|
$(, $($arg:expr),*)? // arguments
|
||||||
$(,)? // optional trailing comma
|
|
||||||
) => {
|
) => {
|
||||||
{
|
{
|
||||||
// May be no args!
|
// May be no args!
|
||||||
@ -58,7 +56,7 @@ macro_rules! sprintf {
|
|||||||
$crate::printf_c_locale(
|
$crate::printf_c_locale(
|
||||||
$target,
|
$target,
|
||||||
$fmt,
|
$fmt,
|
||||||
&mut [$($arg.to_arg()),*],
|
&mut [$( $($arg.to_arg()),* )?],
|
||||||
).unwrap()
|
).unwrap()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -659,6 +659,18 @@ fn test_crate_macros() {
|
|||||||
|
|
||||||
target = crate::sprintf!("%d ok %d", 3, 4);
|
target = crate::sprintf!("%d ok %d", 3, 4);
|
||||||
assert_eq!(target, "3 ok 4");
|
assert_eq!(target, "3 ok 4");
|
||||||
|
|
||||||
|
let target = crate::sprintf!("noargs1");
|
||||||
|
assert_eq!(target, "noargs1");
|
||||||
|
let target = crate::sprintf!("noargs1",);
|
||||||
|
assert_eq!(target, "noargs1");
|
||||||
|
|
||||||
|
let mut target = String::new();
|
||||||
|
crate::sprintf!(=> &mut target, "noargs2");
|
||||||
|
assert_eq!(target, "noargs2");
|
||||||
|
let mut target = String::new();
|
||||||
|
crate::sprintf!(=> &mut target, "noargs2", );
|
||||||
|
assert_eq!(target, "noargs2");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user