Skip to content

Commit 2ec8513

Browse files
David Rajchenbach-Tellermarijnh
David Rajchenbach-Teller
authored andcommitted
[Move] Moved str_to_float, float_to_str from compiler to lib
1 parent 33167f7 commit 2ec8513

File tree

5 files changed

+51
-40
lines changed

5 files changed

+51
-40
lines changed

src/comp/driver/rustc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn time<@T>(do_it: bool, what: str, thunk: fn() -> T) -> T {
9494
let rv = thunk();
9595
let end = std::time::precise_time_s();
9696
log_err #fmt["time: %s took %s s", what,
97-
common::float_to_str(end - start, 3u)];
97+
std::float::float_to_str(end - start, 3u)];
9898
ret rv;
9999
}
100100

src/comp/middle/typeck.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,8 +1303,8 @@ fn valid_range_bounds(l1: @ast::lit, l2: @ast::lit) -> bool {
13031303
alt l1.node {
13041304
ast::lit_float(s1) | ast::lit_mach_float(_, s1) {
13051305
let s2 = lit_as_float(l2);
1306-
let f1 = util::common::str_to_float(s1);
1307-
let f2 = util::common::str_to_float(s2);
1306+
let f1 = std::float::str_to_float(s1);
1307+
let f2 = std::float::str_to_float(s2);
13081308
ret *util::common::min(f1, f2) == f1
13091309
}
13101310
ast::lit_uint(_) | ast::lit_char(_) {

src/comp/util/common.rs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ fn lit_in_range(l: @ast::lit, m1: @ast::lit, m2: @ast::lit) -> bool {
156156
frange(f1, f2) {
157157
alt l.node {
158158
ast::lit_float(f3) | ast::lit_mach_float(_, f3) {
159-
str_to_float(f3) >= *min(f1, f2) &&
160-
str_to_float(f3) <= *max(f1, f2)
159+
std::float::str_to_float(f3) >= *min(f1, f2) &&
160+
std::float::str_to_float(f3) <= *max(f1, f2)
161161
}
162162
_ { fail }
163163
}
@@ -232,7 +232,7 @@ fn lits_to_range(l: @ast::lit, r: @ast::lit) -> range {
232232
}
233233
ast::lit_float(f1) | ast::lit_mach_float(_, f1) {
234234
alt r.node { ast::lit_float(f2) | ast::lit_mach_float(_, f2) {
235-
frange(str_to_float(f1), str_to_float(f2))
235+
frange(std::float::str_to_float(f1), std::float::str_to_float(f2))
236236
}
237237
_ { fail } }
238238
}
@@ -293,41 +293,7 @@ fn is_main_name(path: [ast::ident]) -> bool {
293293
str::eq(option::get(std::vec::last(path)), "main")
294294
}
295295

296-
// FIXME mode this to std::float when editing the stdlib no longer
297-
// requires a snapshot
298-
fn float_to_str(num: float, digits: uint) -> str {
299-
let accum = if num < 0.0 { num = -num; "-" } else { "" };
300-
let trunc = num as uint;
301-
let frac = num - (trunc as float);
302-
accum += uint::str(trunc);
303-
if frac == 0.0 || digits == 0u { ret accum; }
304-
accum += ".";
305-
while digits > 0u && frac > 0.0 {
306-
frac *= 10.0;
307-
let digit = frac as uint;
308-
accum += uint::str(digit);
309-
frac -= digit as float;
310-
digits -= 1u;
311-
}
312-
ret accum;
313-
}
314-
315-
fn str_to_float(num: str) -> float {
316-
let digits = str::split(num, '.' as u8);
317-
let total = int::from_str(digits[0]) as float;
318296

319-
fn dec_val(c: char) -> int { ret (c as int) - ('0' as int); }
320-
321-
let right = digits[1];
322-
let len = str::char_len(digits[1]);
323-
let i = 1u;
324-
while (i < len) {
325-
total += dec_val(str::pop_char(right)) as float /
326-
(int::pow(10, i) as float);
327-
i += 1u;
328-
}
329-
ret total;
330-
}
331297

332298
//
333299
// Local Variables:

src/lib/float.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
fn float_to_str(num: float, digits: uint) -> str {
2+
let accum = if num < 0.0 { num = -num; "-" } else { "" };
3+
let trunc = num as uint;
4+
let frac = num - (trunc as float);
5+
accum += uint::str(trunc);
6+
if frac == 0.0 || digits == 0u { ret accum; }
7+
accum += ".";
8+
while digits > 0u && frac > 0.0 {
9+
frac *= 10.0;
10+
let digit = frac as uint;
11+
accum += uint::str(digit);
12+
frac -= digit as float;
13+
digits -= 1u;
14+
}
15+
ret accum;
16+
}
17+
18+
fn str_to_float(num: str) -> float {
19+
let digits = str::split(num, '.' as u8);
20+
let total = int::from_str(digits[0]) as float;
21+
22+
fn dec_val(c: char) -> int { ret (c as int) - ('0' as int); }
23+
24+
let right = digits[1];
25+
let len = str::char_len(digits[1]);
26+
let i = 1u;
27+
while (i < len) {
28+
total += dec_val(str::pop_char(right)) as float /
29+
(int::pow(10, i) as float);
30+
i += 1u;
31+
}
32+
ret total;
33+
}
34+
35+
//
36+
// Local Variables:
37+
// mode: rust
38+
// fill-column: 78;
39+
// indent-tabs-mode: nil
40+
// c-basic-offset: 4
41+
// buffer-file-coding-system: utf-8-unix
42+
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
43+
// End:
44+
//

src/lib/std.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod u8;
1616
mod u64;
1717
mod vec;
1818
mod str;
19+
mod float;
1920

2021
// General io and system-services modules.
2122

0 commit comments

Comments
 (0)