diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-02-22 03:52:32 +1100 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-02-22 03:56:16 +1100 |
| commit | e37327bfee6217e46921a294f1a321e2d71300ca (patch) | |
| tree | bf179a6d7d458f18e4a78becd67a70cf6fe9079b /src/libstd/num | |
| parent | 9abff54d61babab8050c108f7cb6b957b1636337 (diff) | |
| download | rust-e37327bfee6217e46921a294f1a321e2d71300ca.tar.gz rust-e37327bfee6217e46921a294f1a321e2d71300ca.zip | |
Decouple integer formatting from std::num::strconv
This works towards 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 far more comprehensible.
The `Formatter::pad_integral` method has also been refactored make it 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`.
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.
Arbitrary radixes are now easier to use in format strings. For example:
~~~
assert_eq!(format!("{:04}", radix(3, 2)), ~"0011");
~~~
Diffstat (limited to 'src/libstd/num')
| -rw-r--r-- | src/libstd/num/mod.rs | 8 | ||||
| -rw-r--r-- | src/libstd/num/strconv.rs | 98 |
2 files changed, 88 insertions, 18 deletions
diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index 767faa30f24..150c7bdab29 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -22,6 +22,7 @@ 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; @@ -278,7 +279,12 @@ pub trait Int: Integer + 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 6be829f51d7..c0c3074be63 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -804,24 +804,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()); }) + } } } |
