]> git.lizzy.rs Git - rust.git/commitdiff
fmt: simplify parse_flags
authorAndrew Paseltiner <apaseltiner@gmail.com>
Mon, 7 Jan 2013 19:39:45 +0000 (14:39 -0500)
committerAndrew Paseltiner <apaseltiner@gmail.com>
Mon, 7 Jan 2013 22:22:01 +0000 (17:22 -0500)
src/libcore/extfmt.rs

index 3cc7e2458b25cf69d2d8579e05fc90c83cb1ff4e..25e349f0dc321a24029d659505bbc9e9fce88020 100644 (file)
@@ -243,31 +243,24 @@ pub fn parse_parameter(s: &str, i: uint, lim: uint) ->
     }
     pub fn parse_flags(s: &str, i: uint, lim: uint) ->
        Parsed<~[Flag]> {
-        let noflags: ~[Flag] = ~[];
-        if i >= lim { return Parsed::new(move noflags, i); }
-
-        fn more(f: Flag, s: &str, i: uint, lim: uint) ->
-           Parsed<~[Flag]> {
-            let next = parse_flags(s, i + 1u, lim);
-            let rest = copy next.val;
-            let j = next.next;
-            let curr: ~[Flag] = ~[f];
-            return Parsed::new(vec::append(move curr, rest), j);
+        let mut i = i;
+        let mut flags = ~[];
+
+        while i < lim {
+            let f = match s[i] {
+                '-' as u8 => FlagLeftJustify,
+                '0' as u8 => FlagLeftZeroPad,
+                ' ' as u8 => FlagSpaceForSign,
+                '+' as u8 => FlagSignAlways,
+                '#' as u8 => FlagAlternate,
+                _ => break
+            };
+
+            flags.push(f);
+            i += 1;
         }
-        // Unfortunate, but because s is borrowed, can't use a closure
-     //   fn more(f: Flag, s: &str) { more_(f, s, i, lim); }
-        let f = s[i];
-        return if f == '-' as u8 {
-                more(FlagLeftJustify, s, i, lim)
-            } else if f == '0' as u8 {
-                more(FlagLeftZeroPad, s, i, lim)
-            } else if f == ' ' as u8 {
-                more(FlagSpaceForSign, s, i, lim)
-            } else if f == '+' as u8 {
-                more(FlagSignAlways, s, i, lim)
-            } else if f == '#' as u8 {
-                more(FlagAlternate, s, i, lim)
-            } else { Parsed::new(move noflags, i) };
+
+        Parsed::new(flags, i)
     }
         pub fn parse_count(s: &str, i: uint, lim: uint)
         -> Parsed<Count> {