Skip to content

Commit 3e97cce

Browse files
committed
auto merge of #4957 : Kimundi/rust/incoming, r=catamorphism
Moved them into own module and made them not depend on an Round trait impl for integers and generic math functions that can fail on integers any more.
2 parents 1b87ded + 4673686 commit 3e97cce

File tree

7 files changed

+732
-691
lines changed

7 files changed

+732
-691
lines changed

src/libcore/num/f32.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use cmath;
1414
use cmp;
1515
use libc::{c_float, c_int};
1616
use num::NumCast;
17+
use num::strconv;
1718
use num;
1819
use ops;
1920
use option::Option;
@@ -376,8 +377,8 @@ impl num::Round for f32 {
376377
*/
377378
#[inline(always)]
378379
pub pure fn to_str(num: f32) -> ~str {
379-
let (r, _) = num::to_str_common(
380-
&num, 10u, true, true, num::SignNeg, num::DigAll);
380+
let (r, _) = strconv::to_str_common(
381+
&num, 10u, true, strconv::SignNeg, strconv::DigAll);
381382
r
382383
}
383384

@@ -390,8 +391,8 @@ pub pure fn to_str(num: f32) -> ~str {
390391
*/
391392
#[inline(always)]
392393
pub pure fn to_str_hex(num: f32) -> ~str {
393-
let (r, _) = num::to_str_common(
394-
&num, 16u, true, true, num::SignNeg, num::DigAll);
394+
let (r, _) = strconv::to_str_common(
395+
&num, 16u, true, strconv::SignNeg, strconv::DigAll);
395396
r
396397
}
397398

@@ -411,8 +412,8 @@ pub pure fn to_str_hex(num: f32) -> ~str {
411412
*/
412413
#[inline(always)]
413414
pub pure fn to_str_radix(num: f32, rdx: uint) -> ~str {
414-
let (r, special) = num::to_str_common(
415-
&num, rdx, true, true, num::SignNeg, num::DigAll);
415+
let (r, special) = strconv::to_str_common(
416+
&num, rdx, true, strconv::SignNeg, strconv::DigAll);
416417
if special { fail!(~"number has a special value, \
417418
try to_str_radix_special() if those are expected") }
418419
r
@@ -429,7 +430,8 @@ pub pure fn to_str_radix(num: f32, rdx: uint) -> ~str {
429430
*/
430431
#[inline(always)]
431432
pub pure fn to_str_radix_special(num: f32, rdx: uint) -> (~str, bool) {
432-
num::to_str_common(&num, rdx, true, true, num::SignNeg, num::DigAll)
433+
strconv::to_str_common(&num, rdx, true,
434+
strconv::SignNeg, strconv::DigAll)
433435
}
434436

435437
/**
@@ -443,8 +445,8 @@ pub pure fn to_str_radix_special(num: f32, rdx: uint) -> (~str, bool) {
443445
*/
444446
#[inline(always)]
445447
pub pure fn to_str_exact(num: f32, dig: uint) -> ~str {
446-
let (r, _) = num::to_str_common(
447-
&num, 10u, true, true, num::SignNeg, num::DigExact(dig));
448+
let (r, _) = strconv::to_str_common(
449+
&num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
448450
r
449451
}
450452

@@ -459,8 +461,8 @@ pub pure fn to_str_exact(num: f32, dig: uint) -> ~str {
459461
*/
460462
#[inline(always)]
461463
pub pure fn to_str_digits(num: f32, dig: uint) -> ~str {
462-
let (r, _) = num::to_str_common(
463-
&num, 10u, true, true, num::SignNeg, num::DigMax(dig));
464+
let (r, _) = strconv::to_str_common(
465+
&num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
464466
r
465467
}
466468

@@ -505,7 +507,8 @@ impl num::ToStrRadix for f32 {
505507
*/
506508
#[inline(always)]
507509
pub pure fn from_str(num: &str) -> Option<f32> {
508-
num::from_str_common(num, 10u, true, true, true, num::ExpDec, false)
510+
strconv::from_str_common(num, 10u, true, true, true,
511+
strconv::ExpDec, false)
509512
}
510513

511514
/**
@@ -537,7 +540,8 @@ pub pure fn from_str(num: &str) -> Option<f32> {
537540
*/
538541
#[inline(always)]
539542
pub pure fn from_str_hex(num: &str) -> Option<f32> {
540-
num::from_str_common(num, 16u, true, true, true, num::ExpBin, false)
543+
strconv::from_str_common(num, 16u, true, true, true,
544+
strconv::ExpBin, false)
541545
}
542546

543547
/**
@@ -561,7 +565,8 @@ pub pure fn from_str_hex(num: &str) -> Option<f32> {
561565
*/
562566
#[inline(always)]
563567
pub pure fn from_str_radix(num: &str, rdx: uint) -> Option<f32> {
564-
num::from_str_common(num, rdx, true, true, false, num::ExpNone, false)
568+
strconv::from_str_common(num, rdx, true, true, false,
569+
strconv::ExpNone, false)
565570
}
566571

567572
impl from_str::FromStr for f32 {

src/libcore/num/f64.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use cmp;
1515
use libc::{c_double, c_int};
1616
use libc;
1717
use num::NumCast;
18+
use num::strconv;
1819
use num;
1920
use ops;
2021
use option::Option;
@@ -401,8 +402,8 @@ impl num::Round for f64 {
401402
*/
402403
#[inline(always)]
403404
pub pure fn to_str(num: f64) -> ~str {
404-
let (r, _) = num::to_str_common(
405-
&num, 10u, true, true, num::SignNeg, num::DigAll);
405+
let (r, _) = strconv::to_str_common(
406+
&num, 10u, true, strconv::SignNeg, strconv::DigAll);
406407
r
407408
}
408409

@@ -415,8 +416,8 @@ pub pure fn to_str(num: f64) -> ~str {
415416
*/
416417
#[inline(always)]
417418
pub pure fn to_str_hex(num: f64) -> ~str {
418-
let (r, _) = num::to_str_common(
419-
&num, 16u, true, true, num::SignNeg, num::DigAll);
419+
let (r, _) = strconv::to_str_common(
420+
&num, 16u, true, strconv::SignNeg, strconv::DigAll);
420421
r
421422
}
422423

@@ -436,8 +437,8 @@ pub pure fn to_str_hex(num: f64) -> ~str {
436437
*/
437438
#[inline(always)]
438439
pub pure fn to_str_radix(num: f64, rdx: uint) -> ~str {
439-
let (r, special) = num::to_str_common(
440-
&num, rdx, true, true, num::SignNeg, num::DigAll);
440+
let (r, special) = strconv::to_str_common(
441+
&num, rdx, true, strconv::SignNeg, strconv::DigAll);
441442
if special { fail!(~"number has a special value, \
442443
try to_str_radix_special() if those are expected") }
443444
r
@@ -454,7 +455,8 @@ pub pure fn to_str_radix(num: f64, rdx: uint) -> ~str {
454455
*/
455456
#[inline(always)]
456457
pub pure fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
457-
num::to_str_common(&num, rdx, true, true, num::SignNeg, num::DigAll)
458+
strconv::to_str_common(&num, rdx, true,
459+
strconv::SignNeg, strconv::DigAll)
458460
}
459461

460462
/**
@@ -468,8 +470,8 @@ pub pure fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
468470
*/
469471
#[inline(always)]
470472
pub pure fn to_str_exact(num: f64, dig: uint) -> ~str {
471-
let (r, _) = num::to_str_common(
472-
&num, 10u, true, true, num::SignNeg, num::DigExact(dig));
473+
let (r, _) = strconv::to_str_common(
474+
&num, 10u, true, strconv::SignNeg, strconv::DigExact(dig));
473475
r
474476
}
475477

@@ -484,8 +486,8 @@ pub pure fn to_str_exact(num: f64, dig: uint) -> ~str {
484486
*/
485487
#[inline(always)]
486488
pub pure fn to_str_digits(num: f64, dig: uint) -> ~str {
487-
let (r, _) = num::to_str_common(
488-
&num, 10u, true, true, num::SignNeg, num::DigMax(dig));
489+
let (r, _) = strconv::to_str_common(
490+
&num, 10u, true, strconv::SignNeg, strconv::DigMax(dig));
489491
r
490492
}
491493

@@ -530,7 +532,8 @@ impl num::ToStrRadix for f64 {
530532
*/
531533
#[inline(always)]
532534
pub pure fn from_str(num: &str) -> Option<f64> {
533-
num::from_str_common(num, 10u, true, true, true, num::ExpDec, false)
535+
strconv::from_str_common(num, 10u, true, true, true,
536+
strconv::ExpDec, false)
534537
}
535538

536539
/**
@@ -562,7 +565,8 @@ pub pure fn from_str(num: &str) -> Option<f64> {
562565
*/
563566
#[inline(always)]
564567
pub pure fn from_str_hex(num: &str) -> Option<f64> {
565-
num::from_str_common(num, 16u, true, true, true, num::ExpBin, false)
568+
strconv::from_str_common(num, 16u, true, true, true,
569+
strconv::ExpBin, false)
566570
}
567571

568572
/**
@@ -586,7 +590,8 @@ pub pure fn from_str_hex(num: &str) -> Option<f64> {
586590
*/
587591
#[inline(always)]
588592
pub pure fn from_str_radix(num: &str, rdx: uint) -> Option<f64> {
589-
num::from_str_common(num, rdx, true, true, false, num::ExpNone, false)
593+
strconv::from_str_common(num, rdx, true, true, false,
594+
strconv::ExpNone, false)
590595
}
591596

592597
impl from_str::FromStr for f64 {

src/libcore/num/float.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use cmp::{Eq, Ord};
2626
use cmp;
2727
use f64;
2828
use num::NumCast;
29+
use num::strconv;
2930
use num;
3031
use ops;
3132
use option::{None, Option, Some};
@@ -107,8 +108,8 @@ pub mod consts {
107108
*/
108109
#[inline(always)]
109110
pub pure fn to_str(num: float) -> ~str {
110-
let (r, _) = num::to_str_common(
111-
&num, 10u, true, true, num::SignNeg, num::DigAll);
111+
let (r, _) = strconv::to_str_common(
112+
&num, 10u, true, strconv::SignNeg, strconv::DigAll);
112113
r
113114
}
114115

@@ -121,8 +122,8 @@ pub pure fn to_str(num: float) -> ~str {
121122
*/
122123
#[inline(always)]
123124
pub pure fn to_str_hex(num: float) -> ~str {
124-
let (r, _) = num::to_str_common(
125-
&num, 16u, true, true, num::SignNeg, num::DigAll);
125+
let (r, _) = strconv::to_str_common(
126+
&num, 16u, true, strconv::SignNeg, strconv::DigAll);
126127
r
127128
}
128129

@@ -142,8 +143,8 @@ pub pure fn to_str_hex(num: float) -> ~str {
142143
*/
143144
#[inline(always)]
144145
pub pure fn to_str_radix(num: float, radix: uint) -> ~str {
145-
let (r, special) = num::to_str_common(
146-
&num, radix, true, true, num::SignNeg, num::DigAll);
146+
let (r, special) = strconv::to_str_common(
147+
&num, radix, true, strconv::SignNeg, strconv::DigAll);
147148
if special { fail!(~"number has a special value, \
148149
try to_str_radix_special() if those are expected") }
149150
r
@@ -160,7 +161,8 @@ pub pure fn to_str_radix(num: float, radix: uint) -> ~str {
160161
*/
161162
#[inline(always)]
162163
pub pure fn to_str_radix_special(num: float, radix: uint) -> (~str, bool) {
163-
num::to_str_common(&num, radix, true, true, num::SignNeg, num::DigAll)
164+
strconv::to_str_common(&num, radix, true,
165+
strconv::SignNeg, strconv::DigAll)
164166
}
165167
166168
/**
@@ -174,8 +176,8 @@ pub pure fn to_str_radix_special(num: float, radix: uint) -> (~str, bool) {
174176
*/
175177
#[inline(always)]
176178
pub pure fn to_str_exact(num: float, digits: uint) -> ~str {
177-
let (r, _) = num::to_str_common(
178-
&num, 10u, true, true, num::SignNeg, num::DigExact(digits));
179+
let (r, _) = strconv::to_str_common(
180+
&num, 10u, true, strconv::SignNeg, strconv::DigExact(digits));
179181
r
180182
}
181183
@@ -196,8 +198,8 @@ pub fn test_to_str_exact_do_decimal() {
196198
*/
197199
#[inline(always)]
198200
pub pure fn to_str_digits(num: float, digits: uint) -> ~str {
199-
let (r, _) = num::to_str_common(
200-
&num, 10u, true, true, num::SignNeg, num::DigMax(digits));
201+
let (r, _) = strconv::to_str_common(
202+
&num, 10u, true, strconv::SignNeg, strconv::DigMax(digits));
201203
r
202204
}
203205
@@ -242,7 +244,8 @@ impl num::ToStrRadix for float {
242244
*/
243245
#[inline(always)]
244246
pub pure fn from_str(num: &str) -> Option<float> {
245-
num::from_str_common(num, 10u, true, true, true, num::ExpDec, false)
247+
strconv::from_str_common(num, 10u, true, true, true,
248+
strconv::ExpDec, false)
246249
}
247250
248251
/**
@@ -274,7 +277,8 @@ pub pure fn from_str(num: &str) -> Option<float> {
274277
*/
275278
#[inline(always)]
276279
pub pure fn from_str_hex(num: &str) -> Option<float> {
277-
num::from_str_common(num, 16u, true, true, true, num::ExpBin, false)
280+
strconv::from_str_common(num, 16u, true, true, true,
281+
strconv::ExpBin, false)
278282
}
279283
280284
/**
@@ -298,7 +302,8 @@ pub pure fn from_str_hex(num: &str) -> Option<float> {
298302
*/
299303
#[inline(always)]
300304
pub pure fn from_str_radix(num: &str, radix: uint) -> Option<float> {
301-
num::from_str_common(num, radix, true, true, false, num::ExpNone, false)
305+
strconv::from_str_common(num, radix, true, true, false,
306+
strconv::ExpNone, false)
302307
}
303308
304309
impl from_str::FromStr for float {

src/libcore/num/int-template.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use cmp;
1616
use to_str::ToStr;
1717
use from_str::FromStr;
1818
use num::{ToStrRadix, FromStrRadix};
19+
use num::strconv;
1920
use num;
2021
use prelude::*;
2122
use str;
@@ -176,18 +177,6 @@ impl num::One for T {
176177
static pure fn one() -> T { 1 }
177178
}
178179
179-
impl num::Round for T {
180-
#[inline(always)]
181-
pure fn round(&self, _: num::RoundMode) -> T { *self }
182-
183-
#[inline(always)]
184-
pure fn floor(&self) -> T { *self }
185-
#[inline(always)]
186-
pure fn ceil(&self) -> T { *self }
187-
#[inline(always)]
188-
pure fn fract(&self) -> T { 0 }
189-
}
190-
191180
#[cfg(notest)]
192181
impl ops::Add<T,T> for T {
193182
pure fn add(&self, other: &T) -> T { *self + *other }
@@ -218,22 +207,22 @@ impl ops::Neg<T> for T {
218207
/// Parse a string as a number in base 10.
219208
#[inline(always)]
220209
pub pure fn from_str(s: &str) -> Option<T> {
221-
num::from_str_common(s, 10u, true, false, false,
222-
num::ExpNone, false)
210+
strconv::from_str_common(s, 10u, true, false, false,
211+
strconv::ExpNone, false)
223212
}
224213
225214
/// Parse a string as a number in the given base.
226215
#[inline(always)]
227216
pub pure fn from_str_radix(s: &str, radix: uint) -> Option<T> {
228-
num::from_str_common(s, radix, true, false, false,
229-
num::ExpNone, false)
217+
strconv::from_str_common(s, radix, true, false, false,
218+
strconv::ExpNone, false)
230219
}
231220
232221
/// Parse a byte slice as a number in the given base.
233222
#[inline(always)]
234223
pub pure fn parse_bytes(buf: &[u8], radix: uint) -> Option<T> {
235-
num::from_str_bytes_common(buf, radix, true, false, false,
236-
num::ExpNone, false)
224+
strconv::from_str_bytes_common(buf, radix, true, false, false,
225+
strconv::ExpNone, false)
237226
}
238227
239228
impl FromStr for T {
@@ -255,24 +244,24 @@ impl FromStrRadix for T {
255244
/// Convert to a string as a byte slice in a given base.
256245
#[inline(always)]
257246
pub pure fn to_str_bytes<U>(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U {
258-
let (buf, _) = num::to_str_bytes_common(&n, radix, false, false,
259-
num::SignNeg, num::DigAll);
247+
let (buf, _) = strconv::to_str_bytes_common(&n, radix, false,
248+
strconv::SignNeg, strconv::DigAll);
260249
f(buf)
261250
}
262251
263252
/// Convert to a string in base 10.
264253
#[inline(always)]
265254
pub pure fn to_str(num: T) -> ~str {
266-
let (buf, _) = num::to_str_common(&num, 10u, false, false,
267-
num::SignNeg, num::DigAll);
255+
let (buf, _) = strconv::to_str_common(&num, 10u, false,
256+
strconv::SignNeg, strconv::DigAll);
268257
buf
269258
}
270259
271260
/// Convert to a string in a given base.
272261
#[inline(always)]
273262
pub pure fn to_str_radix(num: T, radix: uint) -> ~str {
274-
let (buf, _) = num::to_str_common(&num, radix, false, false,
275-
num::SignNeg, num::DigAll);
263+
let (buf, _) = strconv::to_str_common(&num, radix, false,
264+
strconv::SignNeg, strconv::DigAll);
276265
buf
277266
}
278267

0 commit comments

Comments
 (0)