Fix number-peek code in fmt!, close #1610.

This commit is contained in:
Graydon Hoare 2012-08-08 17:09:24 -07:00
parent 800de26372
commit 71bc2673ed
2 changed files with 21 additions and 12 deletions

View file

@ -118,18 +118,25 @@ mod ct {
}
fn peek_num(s: ~str, i: uint, lim: uint) ->
option<{num: uint, next: uint}> {
if i >= lim { return none; }
let c = s[i];
if !('0' as u8 <= c && c <= '9' as u8) { return option::none; }
let n = (c - ('0' as u8)) as uint;
return match peek_num(s, i + 1u, lim) {
none => some({num: n, next: i + 1u}),
some(next) => {
let m = next.num;
let j = next.next;
some({num: n * 10u + m, next: j})
}
};
let mut j = i;
let mut accum = 0u;
let mut found = false;
while j < lim {
match char::to_digit(s[j] as char, 10) {
some(x) => {
found = true;
accum *= 10;
accum += x;
j += 1;
},
none => break
}
}
if found {
some({num: accum, next: j})
} else {
none
}
}
fn parse_conversion(s: ~str, i: uint, lim: uint, error: error_fn) ->
{piece: piece, next: uint} {

View file

@ -138,6 +138,8 @@ fn part4() {
test(fmt!{"%.5c", 'A'}, ~"A");
test(fmt!{"%.5f", 5.82}, ~"5.82000");
test(fmt!{"%.5f", 5.0}, ~"5.00000");
test(fmt!{"%.100f", 1.1}, ~"1.1000000000000000888178419700125232338905334472656250000000000000000000000000000000000000000000000000");
// Bool precision. I'm not sure if it's good or bad to have bool
// conversions support precision - it's not standard printf so we
// can do whatever. For now I'm making it behave the same as string