From fc06f7922db0b4d1063f4f29157635117d853426 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 12 Oct 2013 20:00:58 -0700 Subject: Build a few extra features into format! parsing * Allow named parameters to specify width/precision * Intepret the format string '0$' as "width is the 0th argument" instead of thinking the lone '0' was the sign-aware-zero-padding flag. To get both you'd need to put '00$' which makes more sense if you want both to happen. Closes #9669 --- src/libstd/fmt/mod.rs | 52 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) (limited to 'src/libstd/fmt/mod.rs') diff --git a/src/libstd/fmt/mod.rs b/src/libstd/fmt/mod.rs index a03f21d69c8..4032515f985 100644 --- a/src/libstd/fmt/mod.rs +++ b/src/libstd/fmt/mod.rs @@ -647,21 +647,6 @@ impl<'self> Formatter<'self> { // the format! syntax extension. fn run(&mut self, piece: &rt::Piece, cur: Option<&str>) { - let setcount = |slot: &mut Option, cnt: &parse::Count| { - match *cnt { - parse::CountIs(n) => { *slot = Some(n); } - parse::CountImplied => { *slot = None; } - parse::CountIsParam(i) => { - let v = self.args[i].value; - unsafe { *slot = Some(*(v as *util::Void as *uint)); } - } - parse::CountIsNextParam => { - let v = self.curarg.next().unwrap().value; - unsafe { *slot = Some(*(v as *util::Void as *uint)); } - } - } - }; - match *piece { rt::String(s) => { self.buf.write(s.as_bytes()); } rt::CurrentArgument(()) => { self.buf.write(cur.unwrap().as_bytes()); } @@ -670,8 +655,8 @@ impl<'self> Formatter<'self> { self.fill = arg.format.fill; self.align = arg.format.align; self.flags = arg.format.flags; - setcount(&mut self.width, &arg.format.width); - setcount(&mut self.precision, &arg.format.precision); + self.width = self.getcount(&arg.format.width); + self.precision = self.getcount(&arg.format.precision); // Extract the correct argument let value = match arg.position { @@ -688,6 +673,39 @@ impl<'self> Formatter<'self> { } } + #[cfg(stage0)] + fn getcount(&mut self, cnt: &parse::Count) -> Option { + match *cnt { + parse::CountIs(n) => { Some(n) } + parse::CountImplied => { None } + parse::CountIsParam(i) => { + let v = self.args[i].value; + unsafe { Some(*(v as *util::Void as *uint)) } + } + parse::CountIsNextParam => { + let v = self.curarg.next().unwrap().value; + unsafe { Some(*(v as *util::Void as *uint)) } + } + parse::CountIsName(*) => unreachable!() + } + } + + #[cfg(not(stage0))] + fn getcount(&mut self, cnt: &rt::Count) -> Option { + match *cnt { + rt::CountIs(n) => { Some(n) } + rt::CountImplied => { None } + rt::CountIsParam(i) => { + let v = self.args[i].value; + unsafe { Some(*(v as *util::Void as *uint)) } + } + rt::CountIsNextParam => { + let v = self.curarg.next().unwrap().value; + unsafe { Some(*(v as *util::Void as *uint)) } + } + } + } + fn execute(&mut self, method: &rt::Method, arg: Argument) { match *method { // Pluralization is selection upon a numeric value specified as the -- cgit 1.4.1-3-g733a5