Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/uu/printf/src/printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::{Arg, ArgAction, Command};
use std::io::stdout;
use std::ops::ControlFlow;
use uucore::error::{UResult, UUsageError};
use uucore::format::{FormatArgument, FormatItem, parse_spec_and_escape};
use uucore::format::{FormatArgument, FormatArguments, FormatItem, parse_spec_and_escape};
use uucore::{format_usage, help_about, help_section, help_usage, os_str_as_bytes, show_warning};

const VERSION: &str = "version";
Expand Down Expand Up @@ -39,9 +39,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
};

let mut format_seen = false;
let mut args = values.iter().peekable();

// Parse and process the format string
let mut args = FormatArguments::new(&values);
for item in parse_spec_and_escape(format) {
if let Ok(FormatItem::Spec(_)) = item {
format_seen = true;
Expand All @@ -51,26 +50,28 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
ControlFlow::Break(()) => return Ok(()),
};
}
args.start_next_batch();

// Without format specs in the string, the iter would not consume any args,
// leading to an infinite loop. Thus, we exit early.
if !format_seen {
if let Some(arg) = args.next() {
let FormatArgument::Unparsed(arg_str) = arg else {
if !args.is_exhausted() {
let Some(FormatArgument::Unparsed(arg_str)) = args.peek_arg() else {
unreachable!("All args are transformed to Unparsed")
};
show_warning!("ignoring excess arguments, starting with '{arg_str}'");
}
return Ok(());
}

while args.peek().is_some() {
while !args.is_exhausted() {
for item in parse_spec_and_escape(format) {
match item?.write(stdout(), &mut args)? {
ControlFlow::Continue(()) => {}
ControlFlow::Break(()) => return Ok(()),
};
}
args.start_next_batch();
}

Ok(())
Expand Down
Loading
Loading