about summary refs log tree commit diff
path: root/src/libstd/fmt/mod.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2013-10-12 20:00:58 -0700
committerAlex Crichton <alex@alexcrichton.com>2013-10-15 22:27:10 -0700
commitfc06f7922db0b4d1063f4f29157635117d853426 (patch)
treeab33cac38ef9012c89c84a28054eab7bd364cabc /src/libstd/fmt/mod.rs
parenta84c2999c91f6ea43291006288ea6dd8c4852c3b (diff)
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
Diffstat (limited to 'src/libstd/fmt/mod.rs')
-rw-r--r--src/libstd/fmt/mod.rs52
1 files changed, 35 insertions, 17 deletions
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<uint>, 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<uint> {
+        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<uint> {
+        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