rm obsolete integer to_str{,_radix} free functions

This commit is contained in:
Daniel Micay 2013-08-17 22:47:54 -04:00
parent 0d72f604b7
commit 46fc549fa9
27 changed files with 104 additions and 152 deletions

View file

@ -27,8 +27,8 @@ use option::{Some, None};
use rt::io::Writer;
use str::OwnedStr;
use to_bytes::IterBytes;
use uint;
use vec::ImmutableVector;
use num::ToStrRadix;
// Alias `SipState` to `State`.
pub use State = hash::SipState;
@ -386,7 +386,7 @@ impl Streaming for SipState {
let r = self.result_bytes();
let mut s = ~"";
for b in r.iter() {
s.push_str(uint::to_str_radix(*b as uint, 16u));
s.push_str((*b as uint).to_str_radix(16u));
}
s
}
@ -407,8 +407,6 @@ mod tests {
use super::*;
use prelude::*;
use uint;
// Hash just the bytes of the slice, without length prefix
struct Bytes<'self>(&'self [u8]);
impl<'self> IterBytes for Bytes<'self> {
@ -496,7 +494,7 @@ mod tests {
fn to_hex_str(r: &[u8, ..8]) -> ~str {
let mut s = ~"";
for b in r.iter() {
s.push_str(uint::to_str_radix(*b as uint, 16u));
s.push_str((*b as uint).to_str_radix(16u));
}
s
}

View file

@ -525,35 +525,25 @@ pub fn to_str_bytes<U>(n: $T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
f(buf.slice(0, cur))
}
/// Convert to a string in base 10.
#[inline]
pub fn to_str(num: $T) -> ~str {
to_str_radix(num, 10u)
}
/// Convert to a string in a given base.
#[inline]
pub fn to_str_radix(num: $T, radix: uint) -> ~str {
let mut buf: ~[u8] = ~[];
do strconv::int_to_str_bytes_common(num, radix, strconv::SignNeg) |i| {
buf.push(i);
}
// We know we generated valid utf-8, so we don't need to go through that
// check.
unsafe { str::raw::from_bytes_owned(buf) }
}
impl ToStr for $T {
/// Convert to a string in base 10.
#[inline]
fn to_str(&self) -> ~str {
to_str(*self)
self.to_str_radix(10)
}
}
impl ToStrRadix for $T {
/// Convert to a string in a given base.
#[inline]
fn to_str_radix(&self, radix: uint) -> ~str {
to_str_radix(*self, radix)
let mut buf: ~[u8] = ~[];
do strconv::int_to_str_bytes_common(*self, radix, strconv::SignNeg) |i| {
buf.push(i);
}
// We know we generated valid utf-8, so we don't need to go through that
// check.
unsafe { str::raw::from_bytes_owned(buf) }
}
}
@ -813,39 +803,39 @@ mod tests {
#[test]
fn test_to_str() {
assert_eq!(to_str_radix(0 as $T, 10u), ~"0");
assert_eq!(to_str_radix(1 as $T, 10u), ~"1");
assert_eq!(to_str_radix(-1 as $T, 10u), ~"-1");
assert_eq!(to_str_radix(127 as $T, 16u), ~"7f");
assert_eq!(to_str_radix(100 as $T, 10u), ~"100");
assert_eq!((0 as $T).to_str_radix(10u), ~"0");
assert_eq!((1 as $T).to_str_radix(10u), ~"1");
assert_eq!((-1 as $T).to_str_radix(10u), ~"-1");
assert_eq!((127 as $T).to_str_radix(16u), ~"7f");
assert_eq!((100 as $T).to_str_radix(10u), ~"100");
}
#[test]
fn test_int_to_str_overflow() {
let mut i8_val: i8 = 127_i8;
assert_eq!(i8::to_str(i8_val), ~"127");
assert_eq!(i8_val.to_str(), ~"127");
i8_val += 1 as i8;
assert_eq!(i8::to_str(i8_val), ~"-128");
assert_eq!(i8_val.to_str(), ~"-128");
let mut i16_val: i16 = 32_767_i16;
assert_eq!(i16::to_str(i16_val), ~"32767");
assert_eq!(i16_val.to_str(), ~"32767");
i16_val += 1 as i16;
assert_eq!(i16::to_str(i16_val), ~"-32768");
assert_eq!(i16_val.to_str(), ~"-32768");
let mut i32_val: i32 = 2_147_483_647_i32;
assert_eq!(i32::to_str(i32_val), ~"2147483647");
assert_eq!(i32_val.to_str(), ~"2147483647");
i32_val += 1 as i32;
assert_eq!(i32::to_str(i32_val), ~"-2147483648");
assert_eq!(i32_val.to_str(), ~"-2147483648");
let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
assert_eq!(i64::to_str(i64_val), ~"9223372036854775807");
assert_eq!(i64_val.to_str(), ~"9223372036854775807");
i64_val += 1 as i64;
assert_eq!(i64::to_str(i64_val), ~"-9223372036854775808");
assert_eq!(i64_val.to_str(), ~"-9223372036854775808");
}
#[test]

View file

@ -708,14 +708,14 @@ mod test {
mod bench {
use extra::test::BenchHarness;
use rand::{XorShiftRng,RngUtil};
use uint;
use float;
use to_str::ToStr;
#[bench]
fn uint_to_str_rand(bh: &mut BenchHarness) {
let mut rng = XorShiftRng::new();
do bh.iter {
uint::to_str(rng.gen());
rng.gen::<uint>().to_str();
}
}

View file

@ -380,35 +380,25 @@ pub fn to_str_bytes<U>(n: $T, radix: uint, f: &fn(v: &[u8]) -> U) -> U {
f(buf.slice(0, cur))
}
/// Convert to a string in base 10.
#[inline]
pub fn to_str(num: $T) -> ~str {
to_str_radix(num, 10u)
}
/// Convert to a string in a given base.
#[inline]
pub fn to_str_radix(num: $T, radix: uint) -> ~str {
let mut buf = ~[];
do strconv::int_to_str_bytes_common(num, radix, strconv::SignNone) |i| {
buf.push(i);
}
// We know we generated valid utf-8, so we don't need to go through that
// check.
unsafe { str::raw::from_bytes_owned(buf) }
}
impl ToStr for $T {
/// Convert to a string in base 10.
#[inline]
fn to_str(&self) -> ~str {
to_str(*self)
self.to_str_radix(10u)
}
}
impl ToStrRadix for $T {
/// Convert to a string in a given base.
#[inline]
fn to_str_radix(&self, radix: uint) -> ~str {
to_str_radix(*self, radix)
let mut buf = ~[];
do strconv::int_to_str_bytes_common(*self, radix, strconv::SignNone) |i| {
buf.push(i);
}
// We know we generated valid utf-8, so we don't need to go through that
// check.
unsafe { str::raw::from_bytes_owned(buf) }
}
}
@ -451,7 +441,6 @@ mod tests {
use u32;
use u64;
use u8;
use uint;
#[test]
fn test_num() {
@ -536,13 +525,13 @@ mod tests {
#[test]
pub fn test_to_str() {
assert_eq!(to_str_radix(0 as $T, 10u), ~"0");
assert_eq!(to_str_radix(1 as $T, 10u), ~"1");
assert_eq!(to_str_radix(2 as $T, 10u), ~"2");
assert_eq!(to_str_radix(11 as $T, 10u), ~"11");
assert_eq!(to_str_radix(11 as $T, 16u), ~"b");
assert_eq!(to_str_radix(255 as $T, 16u), ~"ff");
assert_eq!(to_str_radix(0xff as $T, 10u), ~"255");
assert_eq!((0 as $T).to_str_radix(10u), ~"0");
assert_eq!((1 as $T).to_str_radix(10u), ~"1");
assert_eq!((2 as $T).to_str_radix(10u), ~"2");
assert_eq!((11 as $T).to_str_radix(10u), ~"11");
assert_eq!((11 as $T).to_str_radix(16u), ~"b");
assert_eq!((255 as $T).to_str_radix(16u), ~"ff");
assert_eq!((0xff as $T).to_str_radix(10u), ~"255");
}
#[test]
@ -575,28 +564,28 @@ mod tests {
#[test]
fn test_uint_to_str_overflow() {
let mut u8_val: u8 = 255_u8;
assert_eq!(u8::to_str(u8_val), ~"255");
assert_eq!(u8_val.to_str(), ~"255");
u8_val += 1 as u8;
assert_eq!(u8::to_str(u8_val), ~"0");
assert_eq!(u8_val.to_str(), ~"0");
let mut u16_val: u16 = 65_535_u16;
assert_eq!(u16::to_str(u16_val), ~"65535");
assert_eq!(u16_val.to_str(), ~"65535");
u16_val += 1 as u16;
assert_eq!(u16::to_str(u16_val), ~"0");
assert_eq!(u16_val.to_str(), ~"0");
let mut u32_val: u32 = 4_294_967_295_u32;
assert_eq!(u32::to_str(u32_val), ~"4294967295");
assert_eq!(u32_val.to_str(), ~"4294967295");
u32_val += 1 as u32;
assert_eq!(u32::to_str(u32_val), ~"0");
assert_eq!(u32_val.to_str(), ~"0");
let mut u64_val: u64 = 18_446_744_073_709_551_615_u64;
assert_eq!(u64::to_str(u64_val), ~"18446744073709551615");
assert_eq!(u64_val.to_str(), ~"18446744073709551615");
u64_val += 1 as u64;
assert_eq!(u64::to_str(u64_val), ~"0");
assert_eq!(u64_val.to_str(), ~"0");
}
#[test]
@ -638,14 +627,14 @@ mod tests {
#[should_fail]
#[ignore(cfg(windows))]
pub fn to_str_radix1() {
uint::to_str_radix(100u, 1u);
100u.to_str_radix(1u);
}
#[test]
#[should_fail]
#[ignore(cfg(windows))]
pub fn to_str_radix37() {
uint::to_str_radix(100u, 37u);
100u.to_str_radix(37u);
}
#[test]

View file

@ -58,7 +58,7 @@ pub use num::{Orderable, Signed, Unsigned, Round};
pub use num::{Algebraic, Trigonometric, Exponential, Hyperbolic};
pub use num::{Integer, Fractional, Real, RealExt};
pub use num::{Bitwise, BitCount, Bounded};
pub use num::{Primitive, Int, Float};
pub use num::{Primitive, Int, Float, ToStrRadix};
pub use path::GenericPath;
pub use path::Path;
pub use path::PosixPath;

View file

@ -480,7 +480,6 @@ pub mod rt {
use str;
use sys;
use num;
use uint;
use vec;
use option::{Some, None, Option};
@ -593,7 +592,7 @@ pub mod rt {
return if prec == 0u && num == 0u {
~""
} else {
let s = uint::to_str_radix(num, radix);
let s = num.to_str_radix(radix);
let len = s.char_len();
if len < prec {
let diff = prec - len;