Skip to content

Commit 71bc267

Browse files
committed
Fix number-peek code in fmt!, close #1610.
1 parent 800de26 commit 71bc267

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/libcore/extfmt.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,25 @@ mod ct {
118118
}
119119
fn peek_num(s: ~str, i: uint, lim: uint) ->
120120
option<{num: uint, next: uint}> {
121-
if i >= lim { return none; }
122-
let c = s[i];
123-
if !('0' as u8 <= c && c <= '9' as u8) { return option::none; }
124-
let n = (c - ('0' as u8)) as uint;
125-
return match peek_num(s, i + 1u, lim) {
126-
none => some({num: n, next: i + 1u}),
127-
some(next) => {
128-
let m = next.num;
129-
let j = next.next;
130-
some({num: n * 10u + m, next: j})
131-
}
132-
};
121+
let mut j = i;
122+
let mut accum = 0u;
123+
let mut found = false;
124+
while j < lim {
125+
match char::to_digit(s[j] as char, 10) {
126+
some(x) => {
127+
found = true;
128+
accum *= 10;
129+
accum += x;
130+
j += 1;
131+
},
132+
none => break
133+
}
134+
}
135+
if found {
136+
some({num: accum, next: j})
137+
} else {
138+
none
139+
}
133140
}
134141
fn parse_conversion(s: ~str, i: uint, lim: uint, error: error_fn) ->
135142
{piece: piece, next: uint} {

src/test/run-pass/syntax-extension-fmt.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ fn part4() {
138138
test(fmt!{"%.5c", 'A'}, ~"A");
139139
test(fmt!{"%.5f", 5.82}, ~"5.82000");
140140
test(fmt!{"%.5f", 5.0}, ~"5.00000");
141+
test(fmt!{"%.100f", 1.1}, ~"1.1000000000000000888178419700125232338905334472656250000000000000000000000000000000000000000000000000");
142+
141143
// Bool precision. I'm not sure if it's good or bad to have bool
142144
// conversions support precision - it's not standard printf so we
143145
// can do whatever. For now I'm making it behave the same as string

0 commit comments

Comments
 (0)