Skip to content

Commit 606c0c1

Browse files
authored
Merge pull request #7837 from sargas/add-indexing-to-printf
printf: Add indexing to format strings
2 parents 7b6a262 + 7f98d98 commit 606c0c1

File tree

6 files changed

+567
-126
lines changed

6 files changed

+567
-126
lines changed

src/uu/printf/src/printf.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use clap::{Arg, ArgAction, Command};
66
use std::io::stdout;
77
use std::ops::ControlFlow;
88
use uucore::error::{UResult, UUsageError};
9-
use uucore::format::{FormatArgument, FormatItem, parse_spec_and_escape};
9+
use uucore::format::{FormatArgument, FormatArguments, FormatItem, parse_spec_and_escape};
1010
use uucore::{format_usage, help_about, help_section, help_usage, os_str_as_bytes, show_warning};
1111

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

4141
let mut format_seen = false;
42-
let mut args = values.iter().peekable();
43-
4442
// Parse and process the format string
43+
let mut args = FormatArguments::new(&values);
4544
for item in parse_spec_and_escape(format) {
4645
if let Ok(FormatItem::Spec(_)) = item {
4746
format_seen = true;
@@ -51,26 +50,28 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5150
ControlFlow::Break(()) => return Ok(()),
5251
};
5352
}
53+
args.start_next_batch();
5454

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

67-
while args.peek().is_some() {
67+
while !args.is_exhausted() {
6868
for item in parse_spec_and_escape(format) {
6969
match item?.write(stdout(), &mut args)? {
7070
ControlFlow::Continue(()) => {}
7171
ControlFlow::Break(()) => return Ok(()),
7272
};
7373
}
74+
args.start_next_batch();
7475
}
7576

7677
Ok(())

0 commit comments

Comments
 (0)