Skip to content

[Move] Moved str_to_float, float_to_str from compiler to lib #1024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/comp/driver/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/comp/middle/typeck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_) {
Expand Down
40 changes: 3 additions & 37 deletions src/comp/util/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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 } }
}
Expand Down Expand Up @@ -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:
Expand Down
44 changes: 44 additions & 0 deletions src/lib/float.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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:
//
1 change: 1 addition & 0 deletions src/lib/std.rc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod u8;
mod u64;
mod vec;
mod str;
mod float;

// General io and system-services modules.

Expand Down