Skip to content

Cleaned up num <-> str conv functions #4957

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 7 commits 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
33 changes: 19 additions & 14 deletions src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use cmath;
use cmp;
use libc::{c_float, c_int};
use num::NumCast;
use num::strconv;
use num;
use ops;
use option::Option;
Expand Down Expand Up @@ -376,8 +377,8 @@ impl num::Round for f32 {
*/
#[inline(always)]
pub pure fn to_str(num: f32) -> ~str {
let (r, _) = num::to_str_common(
&num, 10u, true, true, num::SignNeg, num::DigAll);
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -390,8 +391,8 @@ pub pure fn to_str(num: f32) -> ~str {
*/
#[inline(always)]
pub pure fn to_str_hex(num: f32) -> ~str {
let (r, _) = num::to_str_common(
&num, 16u, true, true, num::SignNeg, num::DigAll);
let (r, _) = strconv::to_str_common(
&num, 16u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -411,8 +412,8 @@ pub pure fn to_str_hex(num: f32) -> ~str {
*/
#[inline(always)]
pub pure fn to_str_radix(num: f32, rdx: uint) -> ~str {
let (r, special) = num::to_str_common(
&num, rdx, true, true, num::SignNeg, num::DigAll);
let (r, special) = strconv::to_str_common(
&num, rdx, true, strconv::SignNeg, strconv::DigAll);
if special { fail!(~"number has a special value, \
try to_str_radix_special() if those are expected") }
r
Expand All @@ -429,7 +430,8 @@ pub pure fn to_str_radix(num: f32, rdx: uint) -> ~str {
*/
#[inline(always)]
pub pure fn to_str_radix_special(num: f32, rdx: uint) -> (~str, bool) {
num::to_str_common(&num, rdx, true, true, num::SignNeg, num::DigAll)
strconv::to_str_common(&num, rdx, true,
strconv::SignNeg, strconv::DigAll)
}

/**
Expand All @@ -443,8 +445,8 @@ pub pure fn to_str_radix_special(num: f32, rdx: uint) -> (~str, bool) {
*/
#[inline(always)]
pub pure fn to_str_exact(num: f32, dig: uint) -> ~str {
let (r, _) = num::to_str_common(
&num, 10u, true, true, num::SignNeg, num::DigExact(dig));
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
r
}

Expand All @@ -459,8 +461,8 @@ pub pure fn to_str_exact(num: f32, dig: uint) -> ~str {
*/
#[inline(always)]
pub pure fn to_str_digits(num: f32, dig: uint) -> ~str {
let (r, _) = num::to_str_common(
&num, 10u, true, true, num::SignNeg, num::DigMax(dig));
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
r
}

Expand Down Expand Up @@ -505,7 +507,8 @@ impl num::ToStrRadix for f32 {
*/
#[inline(always)]
pub pure fn from_str(num: &str) -> Option<f32> {
num::from_str_common(num, 10u, true, true, true, num::ExpDec, false)
strconv::from_str_common(num, 10u, true, true, true,
strconv::ExpDec, false)
}

/**
Expand Down Expand Up @@ -537,7 +540,8 @@ pub pure fn from_str(num: &str) -> Option<f32> {
*/
#[inline(always)]
pub pure fn from_str_hex(num: &str) -> Option<f32> {
num::from_str_common(num, 16u, true, true, true, num::ExpBin, false)
strconv::from_str_common(num, 16u, true, true, true,
strconv::ExpBin, false)
}

/**
Expand All @@ -561,7 +565,8 @@ pub pure fn from_str_hex(num: &str) -> Option<f32> {
*/
#[inline(always)]
pub pure fn from_str_radix(num: &str, rdx: uint) -> Option<f32> {
num::from_str_common(num, rdx, true, true, false, num::ExpNone, false)
strconv::from_str_common(num, rdx, true, true, false,
strconv::ExpNone, false)
}

impl from_str::FromStr for f32 {
Expand Down
33 changes: 19 additions & 14 deletions src/libcore/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use cmp;
use libc::{c_double, c_int};
use libc;
use num::NumCast;
use num::strconv;
use num;
use ops;
use option::Option;
Expand Down Expand Up @@ -401,8 +402,8 @@ impl num::Round for f64 {
*/
#[inline(always)]
pub pure fn to_str(num: f64) -> ~str {
let (r, _) = num::to_str_common(
&num, 10u, true, true, num::SignNeg, num::DigAll);
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -415,8 +416,8 @@ pub pure fn to_str(num: f64) -> ~str {
*/
#[inline(always)]
pub pure fn to_str_hex(num: f64) -> ~str {
let (r, _) = num::to_str_common(
&num, 16u, true, true, num::SignNeg, num::DigAll);
let (r, _) = strconv::to_str_common(
&num, 16u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -436,8 +437,8 @@ pub pure fn to_str_hex(num: f64) -> ~str {
*/
#[inline(always)]
pub pure fn to_str_radix(num: f64, rdx: uint) -> ~str {
let (r, special) = num::to_str_common(
&num, rdx, true, true, num::SignNeg, num::DigAll);
let (r, special) = strconv::to_str_common(
&num, rdx, true, strconv::SignNeg, strconv::DigAll);
if special { fail!(~"number has a special value, \
try to_str_radix_special() if those are expected") }
r
Expand All @@ -454,7 +455,8 @@ pub pure fn to_str_radix(num: f64, rdx: uint) -> ~str {
*/
#[inline(always)]
pub pure fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
num::to_str_common(&num, rdx, true, true, num::SignNeg, num::DigAll)
strconv::to_str_common(&num, rdx, true,
strconv::SignNeg, strconv::DigAll)
}

/**
Expand All @@ -468,8 +470,8 @@ pub pure fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
*/
#[inline(always)]
pub pure fn to_str_exact(num: f64, dig: uint) -> ~str {
let (r, _) = num::to_str_common(
&num, 10u, true, true, num::SignNeg, num::DigExact(dig));
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
r
}

Expand All @@ -484,8 +486,8 @@ pub pure fn to_str_exact(num: f64, dig: uint) -> ~str {
*/
#[inline(always)]
pub pure fn to_str_digits(num: f64, dig: uint) -> ~str {
let (r, _) = num::to_str_common(
&num, 10u, true, true, num::SignNeg, num::DigMax(dig));
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
r
}

Expand Down Expand Up @@ -530,7 +532,8 @@ impl num::ToStrRadix for f64 {
*/
#[inline(always)]
pub pure fn from_str(num: &str) -> Option<f64> {
num::from_str_common(num, 10u, true, true, true, num::ExpDec, false)
strconv::from_str_common(num, 10u, true, true, true,
strconv::ExpDec, false)
}

/**
Expand Down Expand Up @@ -562,7 +565,8 @@ pub pure fn from_str(num: &str) -> Option<f64> {
*/
#[inline(always)]
pub pure fn from_str_hex(num: &str) -> Option<f64> {
num::from_str_common(num, 16u, true, true, true, num::ExpBin, false)
strconv::from_str_common(num, 16u, true, true, true,
strconv::ExpBin, false)
}

/**
Expand All @@ -586,7 +590,8 @@ pub pure fn from_str_hex(num: &str) -> Option<f64> {
*/
#[inline(always)]
pub pure fn from_str_radix(num: &str, rdx: uint) -> Option<f64> {
num::from_str_common(num, rdx, true, true, false, num::ExpNone, false)
strconv::from_str_common(num, rdx, true, true, false,
strconv::ExpNone, false)
}

impl from_str::FromStr for f64 {
Expand Down
33 changes: 19 additions & 14 deletions src/libcore/num/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use cmp::{Eq, Ord};
use cmp;
use f64;
use num::NumCast;
use num::strconv;
use num;
use ops;
use option::{None, Option, Some};
Expand Down Expand Up @@ -107,8 +108,8 @@ pub mod consts {
*/
#[inline(always)]
pub pure fn to_str(num: float) -> ~str {
let (r, _) = num::to_str_common(
&num, 10u, true, true, num::SignNeg, num::DigAll);
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -121,8 +122,8 @@ pub pure fn to_str(num: float) -> ~str {
*/
#[inline(always)]
pub pure fn to_str_hex(num: float) -> ~str {
let (r, _) = num::to_str_common(
&num, 16u, true, true, num::SignNeg, num::DigAll);
let (r, _) = strconv::to_str_common(
&num, 16u, true, strconv::SignNeg, strconv::DigAll);
r
}

Expand All @@ -142,8 +143,8 @@ pub pure fn to_str_hex(num: float) -> ~str {
*/
#[inline(always)]
pub pure fn to_str_radix(num: float, radix: uint) -> ~str {
let (r, special) = num::to_str_common(
&num, radix, true, true, num::SignNeg, num::DigAll);
let (r, special) = strconv::to_str_common(
&num, radix, true, strconv::SignNeg, strconv::DigAll);
if special { fail!(~"number has a special value, \
try to_str_radix_special() if those are expected") }
r
Expand All @@ -160,7 +161,8 @@ pub pure fn to_str_radix(num: float, radix: uint) -> ~str {
*/
#[inline(always)]
pub pure fn to_str_radix_special(num: float, radix: uint) -> (~str, bool) {
num::to_str_common(&num, radix, true, true, num::SignNeg, num::DigAll)
strconv::to_str_common(&num, radix, true,
strconv::SignNeg, strconv::DigAll)
}

/**
Expand All @@ -174,8 +176,8 @@ pub pure fn to_str_radix_special(num: float, radix: uint) -> (~str, bool) {
*/
#[inline(always)]
pub pure fn to_str_exact(num: float, digits: uint) -> ~str {
let (r, _) = num::to_str_common(
&num, 10u, true, true, num::SignNeg, num::DigExact(digits));
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigExact(digits));
r
}

Expand All @@ -196,8 +198,8 @@ pub fn test_to_str_exact_do_decimal() {
*/
#[inline(always)]
pub pure fn to_str_digits(num: float, digits: uint) -> ~str {
let (r, _) = num::to_str_common(
&num, 10u, true, true, num::SignNeg, num::DigMax(digits));
let (r, _) = strconv::to_str_common(
&num, 10u, true, strconv::SignNeg, strconv::DigMax(digits));
r
}

Expand Down Expand Up @@ -242,7 +244,8 @@ impl num::ToStrRadix for float {
*/
#[inline(always)]
pub pure fn from_str(num: &str) -> Option<float> {
num::from_str_common(num, 10u, true, true, true, num::ExpDec, false)
strconv::from_str_common(num, 10u, true, true, true,
strconv::ExpDec, false)
}

/**
Expand Down Expand Up @@ -274,7 +277,8 @@ pub pure fn from_str(num: &str) -> Option<float> {
*/
#[inline(always)]
pub pure fn from_str_hex(num: &str) -> Option<float> {
num::from_str_common(num, 16u, true, true, true, num::ExpBin, false)
strconv::from_str_common(num, 16u, true, true, true,
strconv::ExpBin, false)
}

/**
Expand All @@ -298,7 +302,8 @@ pub pure fn from_str_hex(num: &str) -> Option<float> {
*/
#[inline(always)]
pub pure fn from_str_radix(num: &str, radix: uint) -> Option<float> {
num::from_str_common(num, radix, true, true, false, num::ExpNone, false)
strconv::from_str_common(num, radix, true, true, false,
strconv::ExpNone, false)
}

impl from_str::FromStr for float {
Expand Down
37 changes: 13 additions & 24 deletions src/libcore/num/int-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use cmp;
use to_str::ToStr;
use from_str::FromStr;
use num::{ToStrRadix, FromStrRadix};
use num::strconv;
use num;
use prelude::*;
use str;
Expand Down Expand Up @@ -176,18 +177,6 @@ impl num::One for T {
static pure fn one() -> T { 1 }
}

impl num::Round for T {
#[inline(always)]
pure fn round(&self, _: num::RoundMode) -> T { *self }

#[inline(always)]
pure fn floor(&self) -> T { *self }
#[inline(always)]
pure fn ceil(&self) -> T { *self }
#[inline(always)]
pure fn fract(&self) -> T { 0 }
}

#[cfg(notest)]
impl ops::Add<T,T> for T {
pure fn add(&self, other: &T) -> T { *self + *other }
Expand Down Expand Up @@ -218,22 +207,22 @@ impl ops::Neg<T> for T {
/// Parse a string as a number in base 10.
#[inline(always)]
pub pure fn from_str(s: &str) -> Option<T> {
num::from_str_common(s, 10u, true, false, false,
num::ExpNone, false)
strconv::from_str_common(s, 10u, true, false, false,
strconv::ExpNone, false)
}

/// Parse a string as a number in the given base.
#[inline(always)]
pub pure fn from_str_radix(s: &str, radix: uint) -> Option<T> {
num::from_str_common(s, radix, true, false, false,
num::ExpNone, false)
strconv::from_str_common(s, radix, true, false, false,
strconv::ExpNone, false)
}

/// Parse a byte slice as a number in the given base.
#[inline(always)]
pub pure fn parse_bytes(buf: &[u8], radix: uint) -> Option<T> {
num::from_str_bytes_common(buf, radix, true, false, false,
num::ExpNone, false)
strconv::from_str_bytes_common(buf, radix, true, false, false,
strconv::ExpNone, false)
}

impl FromStr for T {
Expand All @@ -255,24 +244,24 @@ impl FromStrRadix for T {
/// Convert to a string as a byte slice in a given base.
#[inline(always)]
pub pure fn to_str_bytes<U>(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U {
let (buf, _) = num::to_str_bytes_common(&n, radix, false, false,
num::SignNeg, num::DigAll);
let (buf, _) = strconv::to_str_bytes_common(&n, radix, false,
strconv::SignNeg, strconv::DigAll);
f(buf)
}

/// Convert to a string in base 10.
#[inline(always)]
pub pure fn to_str(num: T) -> ~str {
let (buf, _) = num::to_str_common(&num, 10u, false, false,
num::SignNeg, num::DigAll);
let (buf, _) = strconv::to_str_common(&num, 10u, false,
strconv::SignNeg, strconv::DigAll);
buf
}

/// Convert to a string in a given base.
#[inline(always)]
pub pure fn to_str_radix(num: T, radix: uint) -> ~str {
let (buf, _) = num::to_str_common(&num, radix, false, false,
num::SignNeg, num::DigAll);
let (buf, _) = strconv::to_str_common(&num, radix, false,
strconv::SignNeg, strconv::DigAll);
buf
}

Expand Down
Loading