fmt: simplify parse_flags
This commit is contained in:
parent
51b141e3e4
commit
f4bc9e6d57
1 changed files with 16 additions and 23 deletions
|
@ -243,31 +243,24 @@ pub mod ct {
|
||||||
}
|
}
|
||||||
pub fn parse_flags(s: &str, i: uint, lim: uint) ->
|
pub fn parse_flags(s: &str, i: uint, lim: uint) ->
|
||||||
Parsed<~[Flag]> {
|
Parsed<~[Flag]> {
|
||||||
let noflags: ~[Flag] = ~[];
|
let mut i = i;
|
||||||
if i >= lim { return Parsed::new(move noflags, i); }
|
let mut flags = ~[];
|
||||||
|
|
||||||
fn more(f: Flag, s: &str, i: uint, lim: uint) ->
|
while i < lim {
|
||||||
Parsed<~[Flag]> {
|
let f = match s[i] {
|
||||||
let next = parse_flags(s, i + 1u, lim);
|
'-' as u8 => FlagLeftJustify,
|
||||||
let rest = copy next.val;
|
'0' as u8 => FlagLeftZeroPad,
|
||||||
let j = next.next;
|
' ' as u8 => FlagSpaceForSign,
|
||||||
let curr: ~[Flag] = ~[f];
|
'+' as u8 => FlagSignAlways,
|
||||||
return Parsed::new(vec::append(move curr, rest), j);
|
'#' 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); }
|
Parsed::new(flags, i)
|
||||||
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) };
|
|
||||||
}
|
}
|
||||||
pub fn parse_count(s: &str, i: uint, lim: uint)
|
pub fn parse_count(s: &str, i: uint, lim: uint)
|
||||||
-> Parsed<Count> {
|
-> Parsed<Count> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue