1
Fork 0

fmt: simplify parse_fmt_string

This commit is contained in:
Andrew Paseltiner 2013-01-07 15:00:03 -05:00
parent 9e5a4166d5
commit 428abb3d97

View file

@ -154,39 +154,41 @@ pub mod ct {
pub type ErrorFn = fn@(&str) -> ! ; pub type ErrorFn = fn@(&str) -> ! ;
pub fn parse_fmt_string(s: &str, err: ErrorFn) -> ~[Piece] { pub fn parse_fmt_string(s: &str, err: ErrorFn) -> ~[Piece] {
let mut pieces: ~[Piece] = ~[]; fn push_slice(ps: &mut ~[Piece], s: &str, from: uint, to: uint) {
let lim = str::len(s); if to > from {
let mut buf = ~""; ps.push(PieceString(s.slice(from, to)));
fn flush_buf(buf: ~str, pieces: &mut ~[Piece]) -> ~str {
if buf.len() > 0 {
let piece = PieceString(move buf);
pieces.push(move piece);
} }
return ~"";
} }
let lim = s.len();
let mut h = 0;
let mut i = 0; let mut i = 0;
let mut pieces = ~[];
while i < lim { while i < lim {
let size = str::utf8_char_width(s[i]); if s[i] == '%' as u8 {
let curr = str::slice(s, i, i+size);
if curr == ~"%" {
i += 1; i += 1;
if i >= lim { if i >= lim {
err(~"unterminated conversion at end of string"); err(~"unterminated conversion at end of string");
} } else if s[i] == '%' as u8 {
let curr2 = str::slice(s, i, i+1); push_slice(&mut pieces, s, h, i);
if curr2 == ~"%" {
buf += curr2;
i += 1; i += 1;
} else { } else {
buf = flush_buf(move buf, &mut pieces); push_slice(&mut pieces, s, h, i - 1);
let rs = parse_conversion(s, i, lim, err); let Parsed {val, next} = parse_conversion(s, i, lim, err);
pieces.push(copy rs.val); pieces.push(val);
i = rs.next; i = next;
} }
} else { buf += curr; i += size; }
h = i;
} else {
i += str::utf8_char_width(s[i]);
}
} }
flush_buf(move buf, &mut pieces);
move pieces push_slice(&mut pieces, s, h, i);
pieces
} }
pub fn peek_num(s: &str, i: uint, lim: uint) -> pub fn peek_num(s: &str, i: uint, lim: uint) ->
Option<Parsed<uint>> { Option<Parsed<uint>> {