提交 2ec85135 编写于 作者: D David Rajchenbach-Teller 提交者: Marijn Haverbeke

[Move] Moved str_to_float, float_to_str from compiler to lib

上级 33167f7d
......@@ -94,7 +94,7 @@ fn time<@T>(do_it: bool, what: str, thunk: fn() -> T) -> T {
let rv = thunk();
let end = std::time::precise_time_s();
log_err #fmt["time: %s took %s s", what,
common::float_to_str(end - start, 3u)];
std::float::float_to_str(end - start, 3u)];
ret rv;
}
......
......@@ -1303,8 +1303,8 @@ fn valid_range_bounds(l1: @ast::lit, l2: @ast::lit) -> bool {
alt l1.node {
ast::lit_float(s1) | ast::lit_mach_float(_, s1) {
let s2 = lit_as_float(l2);
let f1 = util::common::str_to_float(s1);
let f2 = util::common::str_to_float(s2);
let f1 = std::float::str_to_float(s1);
let f2 = std::float::str_to_float(s2);
ret *util::common::min(f1, f2) == f1
}
ast::lit_uint(_) | ast::lit_char(_) {
......
......@@ -156,8 +156,8 @@ fn lit_in_range(l: @ast::lit, m1: @ast::lit, m2: @ast::lit) -> bool {
frange(f1, f2) {
alt l.node {
ast::lit_float(f3) | ast::lit_mach_float(_, f3) {
str_to_float(f3) >= *min(f1, f2) &&
str_to_float(f3) <= *max(f1, f2)
std::float::str_to_float(f3) >= *min(f1, f2) &&
std::float::str_to_float(f3) <= *max(f1, f2)
}
_ { fail }
}
......@@ -232,7 +232,7 @@ fn lits_to_range(l: @ast::lit, r: @ast::lit) -> range {
}
ast::lit_float(f1) | ast::lit_mach_float(_, f1) {
alt r.node { ast::lit_float(f2) | ast::lit_mach_float(_, f2) {
frange(str_to_float(f1), str_to_float(f2))
frange(std::float::str_to_float(f1), std::float::str_to_float(f2))
}
_ { fail } }
}
......@@ -293,41 +293,7 @@ fn is_main_name(path: [ast::ident]) -> bool {
str::eq(option::get(std::vec::last(path)), "main")
}
// FIXME mode this to std::float when editing the stdlib no longer
// requires a snapshot
fn float_to_str(num: float, digits: uint) -> str {
let accum = if num < 0.0 { num = -num; "-" } else { "" };
let trunc = num as uint;
let frac = num - (trunc as float);
accum += uint::str(trunc);
if frac == 0.0 || digits == 0u { ret accum; }
accum += ".";
while digits > 0u && frac > 0.0 {
frac *= 10.0;
let digit = frac as uint;
accum += uint::str(digit);
frac -= digit as float;
digits -= 1u;
}
ret accum;
}
fn str_to_float(num: str) -> float {
let digits = str::split(num, '.' as u8);
let total = int::from_str(digits[0]) as float;
fn dec_val(c: char) -> int { ret (c as int) - ('0' as int); }
let right = digits[1];
let len = str::char_len(digits[1]);
let i = 1u;
while (i < len) {
total += dec_val(str::pop_char(right)) as float /
(int::pow(10, i) as float);
i += 1u;
}
ret total;
}
//
// Local Variables:
......
fn float_to_str(num: float, digits: uint) -> str {
let accum = if num < 0.0 { num = -num; "-" } else { "" };
let trunc = num as uint;
let frac = num - (trunc as float);
accum += uint::str(trunc);
if frac == 0.0 || digits == 0u { ret accum; }
accum += ".";
while digits > 0u && frac > 0.0 {
frac *= 10.0;
let digit = frac as uint;
accum += uint::str(digit);
frac -= digit as float;
digits -= 1u;
}
ret accum;
}
fn str_to_float(num: str) -> float {
let digits = str::split(num, '.' as u8);
let total = int::from_str(digits[0]) as float;
fn dec_val(c: char) -> int { ret (c as int) - ('0' as int); }
let right = digits[1];
let len = str::char_len(digits[1]);
let i = 1u;
while (i < len) {
total += dec_val(str::pop_char(right)) as float /
(int::pow(10, i) as float);
i += 1u;
}
ret total;
}
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
//
......@@ -16,6 +16,7 @@ mod u8;
mod u64;
mod vec;
mod str;
mod float;
// General io and system-services modules.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册