Skip to content

Commit

Permalink
Move run/getcount to functions
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Mark-Simulacrum committed Jan 20, 2020
1 parent fdef4f1 commit 4919b96
Showing 1 changed file with 35 additions and 38 deletions.
73 changes: 35 additions & 38 deletions src/libcore/fmt/mod.rs
Expand Up @@ -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;
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit 4919b96

Please sign in to comment.