Move run/getcount to functions

These are only called from one place and don't generally support being called
from other places; furthermore, they're the only formatter functions that look
at the `args` field (which a future commit will remove).
This commit is contained in:
Mark Rousskov 2020-01-01 13:58:57 -05:00
parent fdef4f185e
commit 4919b96f81

View File

@ -1060,7 +1060,7 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
// a string piece.
for (arg, piece) in fmt.iter().zip(args.pieces.iter()) {
formatter.buf.write_str(*piece)?;
formatter.run(arg)?;
run(&mut formatter, arg)?;
idx += 1;
}
}
@ -1074,6 +1074,40 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
Ok(())
}
fn run(fmt: &mut Formatter<'_>, arg: &rt::v1::Argument) -> Result {
// Fill in the format parameters into the formatter
fmt.fill = arg.format.fill;
fmt.align = arg.format.align;
fmt.flags = arg.format.flags;
fmt.width = getcount(&fmt.args, &arg.format.width);
fmt.precision = getcount(&fmt.args, &arg.format.precision);
// Extract the correct argument
let value = {
#[cfg(bootstrap)]
{
match arg.position {
rt::v1::Position::At(i) => fmt.args[i],
}
}
#[cfg(not(bootstrap))]
{
fmt.args[arg.position]
}
};
// Then actually do some printing
(value.formatter)(value.value, fmt)
}
fn getcount(args: &[ArgumentV1<'_>], cnt: &rt::v1::Count) -> Option<usize> {
match *cnt {
rt::v1::Count::Is(n) => Some(n),
rt::v1::Count::Implied => None,
rt::v1::Count::Param(i) => args[i].as_usize(),
}
}
/// Padding after the end of something. Returned by `Formatter::padding`.
#[must_use = "don't forget to write the post padding"]
struct PostPadding {
@ -1118,43 +1152,6 @@ impl<'a> Formatter<'a> {
}
}
// First up is the collection of functions used to execute a format string
// at runtime. This consumes all of the compile-time statics generated by
// the format! syntax extension.
fn run(&mut self, arg: &rt::v1::Argument) -> Result {
// Fill in the format parameters into the formatter
self.fill = arg.format.fill;
self.align = arg.format.align;
self.flags = arg.format.flags;
self.width = self.getcount(&arg.format.width);
self.precision = self.getcount(&arg.format.precision);
// Extract the correct argument
let value = {
#[cfg(bootstrap)]
{
match arg.position {
rt::v1::Position::At(i) => self.args[i],
}
}
#[cfg(not(bootstrap))]
{
self.args[arg.position]
}
};
// Then actually do some printing
(value.formatter)(value.value, self)
}
fn getcount(&mut self, cnt: &rt::v1::Count) -> Option<usize> {
match *cnt {
rt::v1::Count::Is(n) => Some(n),
rt::v1::Count::Implied => None,
rt::v1::Count::Param(i) => self.args[i].as_usize(),
}
}
// Helper methods used for padding and processing formatting arguments that
// all formatting traits can use.