diff options
| author | bors <bors@rust-lang.org> | 2014-02-21 16:36:52 -0800 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-02-21 16:36:52 -0800 |
| commit | d2f73abf10ff72270f36d73e91851511fd1e0580 (patch) | |
| tree | 457abe392a3416bc600d1b167c91bbb71f5f15a9 /src/libstd/num | |
| parent | 87c7e1542c386a06a5fb1de63c36df8b95088231 (diff) | |
| parent | 6943acd1a50b0c4d0d95e298c151aa796ae05269 (diff) | |
| download | rust-d2f73abf10ff72270f36d73e91851511fd1e0580.tar.gz rust-d2f73abf10ff72270f36d73e91851511fd1e0580.zip | |
auto merge of #12382 : bjz/rust/fmt-int, r=alexcrichton
This is PR is the beginning of a complete rewrite and ultimate removal of the `std::num::strconv` module (see #6220), and the removal of the `ToStrRadix` trait in favour of using the `std::fmt` functionality directly. This should make for a cleaner API, encourage less allocation, and make the implementation more comprehensible .
The `Formatter::{pad_integral, with_padding}` methods have also been refactored make things easier to understand.
The formatting tests for integers have been moved out of `run-pass/ifmt.rs` in order to provide more immediate feedback when building using `make check-stage2-std NO_REBUILD=1`.
Arbitrary radixes are now easier to use in format strings. For example:
~~~rust
assert_eq!(format!("{:04}", radix(3, 2)), ~"0011");
~~~
The benchmarks have been standardised between `std::num::strconv` and `std::num::fmt` to make it easier to compare the performance of the different implementations.
~~~
type | radix | std::num::strconv | std::num::fmt
======|=======|========================|======================
int | bin | 1748 ns/iter (+/- 150) | 321 ns/iter (+/- 25)
int | oct | 706 ns/iter (+/- 53) | 179 ns/iter (+/- 22)
int | dec | 640 ns/iter (+/- 59) | 207 ns/iter (+/- 10)
int | hex | 637 ns/iter (+/- 77) | 205 ns/iter (+/- 19)
int | 36 | 446 ns/iter (+/- 30) | 309 ns/iter (+/- 20)
------|-------|------------------------|----------------------
uint | bin | 1724 ns/iter (+/- 159) | 322 ns/iter (+/- 13)
uint | oct | 663 ns/iter (+/- 25) | 175 ns/iter (+/- 7)
uint | dec | 613 ns/iter (+/- 30) | 186 ns/iter (+/- 6)
uint | hex | 519 ns/iter (+/- 44) | 207 ns/iter (+/- 20)
uint | 36 | 418 ns/iter (+/- 16) | 308 ns/iter (+/- 32)
~~~
Diffstat (limited to 'src/libstd/num')
| -rw-r--r-- | src/libstd/num/int_macros.rs | 2 | ||||
| -rw-r--r-- | src/libstd/num/mod.rs | 12 | ||||
| -rw-r--r-- | src/libstd/num/strconv.rs | 98 | ||||
| -rw-r--r-- | src/libstd/num/uint_macros.rs | 2 |
4 files changed, 93 insertions, 21 deletions
diff --git a/src/libstd/num/int_macros.rs b/src/libstd/num/int_macros.rs index 7f45b5deb18..43a70190812 100644 --- a/src/libstd/num/int_macros.rs +++ b/src/libstd/num/int_macros.rs @@ -277,7 +277,7 @@ impl ToStr for $T { /// Convert to a string in base 10. #[inline] fn to_str(&self) -> ~str { - self.to_str_radix(10) + format!("{:d}", *self) } } diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index 332eb62b0c6..b23e42ad1c6 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -17,10 +17,12 @@ use clone::{Clone, DeepClone}; use cmp::{Eq, Ord}; +use kinds::Pod; use mem::size_of; use ops::{Add, Sub, Mul, Div, Rem, Neg}; use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr}; use option::{Option, Some, None}; +use fmt::{Show, Binary, Octal, LowerHex, UpperHex}; pub mod strconv; @@ -243,7 +245,8 @@ pub trait Bitwise: Bounded /// Specifies the available operations common to all of Rust's core numeric primitives. /// These may not always make sense from a purely mathematical point of view, but /// may be useful for systems programming. -pub trait Primitive: Clone +pub trait Primitive: Pod + + Clone + DeepClone + Num + NumCast @@ -256,7 +259,12 @@ pub trait Int: Primitive + CheckedAdd + CheckedSub + CheckedMul - + CheckedDiv {} + + CheckedDiv + + Show + + Binary + + Octal + + LowerHex + + UpperHex {} /// Returns the smallest power of 2 greater than or equal to `n`. #[inline] diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index bc79ec9a4af..153c042c6a8 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -792,24 +792,88 @@ mod test { #[cfg(test)] mod bench { extern crate test; - use self::test::BenchHarness; - use rand::{XorShiftRng, Rng}; - use to_str::ToStr; - use f64; - - #[bench] - fn uint_to_str_rand(bh: &mut BenchHarness) { - let mut rng = XorShiftRng::new(); - bh.iter(|| { - rng.gen::<uint>().to_str(); - }) + + mod uint { + use super::test::BenchHarness; + use rand::{XorShiftRng, Rng}; + use num::ToStrRadix; + + #[bench] + fn to_str_bin(bh: &mut BenchHarness) { + let mut rng = XorShiftRng::new(); + bh.iter(|| { rng.gen::<uint>().to_str_radix(2); }) + } + + #[bench] + fn to_str_oct(bh: &mut BenchHarness) { + let mut rng = XorShiftRng::new(); + bh.iter(|| { rng.gen::<uint>().to_str_radix(8); }) + } + + #[bench] + fn to_str_dec(bh: &mut BenchHarness) { + let mut rng = XorShiftRng::new(); + bh.iter(|| { rng.gen::<uint>().to_str_radix(10); }) + } + + #[bench] + fn to_str_hex(bh: &mut BenchHarness) { + let mut rng = XorShiftRng::new(); + bh.iter(|| { rng.gen::<uint>().to_str_radix(16); }) + } + + #[bench] + fn to_str_base_36(bh: &mut BenchHarness) { + let mut rng = XorShiftRng::new(); + bh.iter(|| { rng.gen::<uint>().to_str_radix(36); }) + } } - #[bench] - fn float_to_str_rand(bh: &mut BenchHarness) { - let mut rng = XorShiftRng::new(); - bh.iter(|| { - f64::to_str(rng.gen()); - }) + mod int { + use super::test::BenchHarness; + use rand::{XorShiftRng, Rng}; + use num::ToStrRadix; + + #[bench] + fn to_str_bin(bh: &mut BenchHarness) { + let mut rng = XorShiftRng::new(); + bh.iter(|| { rng.gen::<int>().to_str_radix(2); }) + } + + #[bench] + fn to_str_oct(bh: &mut BenchHarness) { + let mut rng = XorShiftRng::new(); + bh.iter(|| { rng.gen::<int>().to_str_radix(8); }) + } + + #[bench] + fn to_str_dec(bh: &mut BenchHarness) { + let mut rng = XorShiftRng::new(); + bh.iter(|| { rng.gen::<int>().to_str_radix(10); }) + } + + #[bench] + fn to_str_hex(bh: &mut BenchHarness) { + let mut rng = XorShiftRng::new(); + bh.iter(|| { rng.gen::<int>().to_str_radix(16); }) + } + + #[bench] + fn to_str_base_36(bh: &mut BenchHarness) { + let mut rng = XorShiftRng::new(); + bh.iter(|| { rng.gen::<int>().to_str_radix(36); }) + } + } + + mod f64 { + use super::test::BenchHarness; + use rand::{XorShiftRng, Rng}; + use f64; + + #[bench] + fn float_to_str(bh: &mut BenchHarness) { + let mut rng = XorShiftRng::new(); + bh.iter(|| { f64::to_str(rng.gen()); }) + } } } diff --git a/src/libstd/num/uint_macros.rs b/src/libstd/num/uint_macros.rs index 33fcdcc426a..d60b5235446 100644 --- a/src/libstd/num/uint_macros.rs +++ b/src/libstd/num/uint_macros.rs @@ -191,7 +191,7 @@ impl ToStr for $T { /// Convert to a string in base 10. #[inline] fn to_str(&self) -> ~str { - self.to_str_radix(10u) + format!("{:u}", *self) } } |
