diff --git a/src/libcore/extfmt.rs b/src/libcore/extfmt.rs index fba8968103c3..0475ff420ce0 100644 --- a/src/libcore/extfmt.rs +++ b/src/libcore/extfmt.rs @@ -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} { diff --git a/src/test/run-pass/syntax-extension-fmt.rs b/src/test/run-pass/syntax-extension-fmt.rs index a74d1884e390..bdf839d8b1f8 100644 --- a/src/test/run-pass/syntax-extension-fmt.rs +++ b/src/test/run-pass/syntax-extension-fmt.rs @@ -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