mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-15 22:44:01 +00:00
Allow sprintf! to work with literal format strings
Now sprintf! has two modes: - Literal format string - Widechar runtime-format string
This commit is contained in:
parent
aa46e7b27c
commit
389d25e30f
1 changed files with 25 additions and 0 deletions
|
@ -115,6 +115,16 @@ fn vsprintfp(format: &[FormatElement], args: &[&dyn Printf]) -> Result<WString>
|
|||
///
|
||||
/// Wrapper around [vsprintf].
|
||||
macro_rules! sprintf {
|
||||
// Variant which allows a string literal.
|
||||
(
|
||||
$fmt:literal, // format string
|
||||
$($arg:expr),* // arguments
|
||||
$(,)? // optional trailing comma
|
||||
) => {
|
||||
crate::wutil::format::printf::vsprintf(&crate::wchar::L!($fmt), &[$( &($arg) as &dyn crate::wutil::format::printf::Printf),* ][..]).expect("Invalid format string and/or arguments")
|
||||
};
|
||||
|
||||
// Variant which allows a runtime format string, which must be of type &wstr.
|
||||
(
|
||||
$fmt:expr, // format string
|
||||
$($arg:expr),* // arguments
|
||||
|
@ -123,4 +133,19 @@ macro_rules! sprintf {
|
|||
crate::wutil::format::printf::vsprintf($fmt, &[$( &($arg) as &dyn crate::wutil::format::printf::Printf),* ][..]).expect("Invalid format string and/or arguments")
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) use sprintf;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::wchar::L;
|
||||
|
||||
// Test basic printf with both literals and wide strings.
|
||||
#[test]
|
||||
fn test_sprintf() {
|
||||
assert_eq!(sprintf!("Hello, %s!", "world"), "Hello, world!");
|
||||
assert_eq!(sprintf!(L!("Hello, %ls!"), "world"), "Hello, world!");
|
||||
assert_eq!(sprintf!(L!("Hello, %ls!"), L!("world")), "Hello, world!");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue