Skip to content

Commit 251fdc8

Browse files
committed
Remove unnecessary features from strconv
1 parent 138b76b commit 251fdc8

File tree

3 files changed

+33
-121
lines changed

3 files changed

+33
-121
lines changed

src/libstd/num/f32.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,7 @@ pub fn to_str_exp_digits(num: f32, dig: uint, upper: bool) -> String {
360360
/// `Some(n)` where `n` is the floating-point number represented by `[num]`.
361361
#[inline]
362362
pub fn from_str_hex(num: &str) -> Option<f32> {
363-
strconv::from_str_common(num, 16u, true, true, true,
364-
strconv::ExpBin, false)
363+
strconv::from_str_float(num, 16u, true, strconv::ExpBin)
365364
}
366365

367366
impl FromStr for f32 {
@@ -392,8 +391,7 @@ impl FromStr for f32 {
392391
/// `Some(n)` where `n` is the floating-point number represented by `num`.
393392
#[inline]
394393
fn from_str(val: &str) -> Option<f32> {
395-
strconv::from_str_common(val, 10u, true, true, true,
396-
strconv::ExpDec, false)
394+
strconv::from_str_float(val, 10u, true, strconv::ExpDec)
397395
}
398396
}
399397

@@ -417,8 +415,7 @@ impl num::FromStrRadix for f32 {
417415
/// `Some(n)` where `n` is the floating-point number represented by `num`.
418416
#[inline]
419417
fn from_str_radix(val: &str, rdx: uint) -> Option<f32> {
420-
strconv::from_str_common(val, rdx, true, true, false,
421-
strconv::ExpNone, false)
418+
strconv::from_str_float(val, rdx, false, strconv::ExpNone)
422419
}
423420
}
424421

src/libstd/num/f64.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ pub fn to_str_exp_digits(num: f64, dig: uint, upper: bool) -> String {
368368
/// `Some(n)` where `n` is the floating-point number represented by `[num]`.
369369
#[inline]
370370
pub fn from_str_hex(num: &str) -> Option<f64> {
371-
strconv::from_str_common(num, 16u, true, true, true,
372-
strconv::ExpBin, false)
371+
strconv::from_str_float(num, 16u, true, strconv::ExpBin)
373372
}
374373

375374
impl FromStr for f64 {
@@ -400,8 +399,7 @@ impl FromStr for f64 {
400399
/// `Some(n)` where `n` is the floating-point number represented by `num`.
401400
#[inline]
402401
fn from_str(val: &str) -> Option<f64> {
403-
strconv::from_str_common(val, 10u, true, true, true,
404-
strconv::ExpDec, false)
402+
strconv::from_str_float(val, 10u, true, strconv::ExpDec)
405403
}
406404
}
407405

@@ -425,8 +423,7 @@ impl num::FromStrRadix for f64 {
425423
/// `Some(n)` where `n` is the floating-point number represented by `num`.
426424
#[inline]
427425
fn from_str_radix(val: &str, rdx: uint) -> Option<f64> {
428-
strconv::from_str_common(val, rdx, true, true, false,
429-
strconv::ExpNone, false)
426+
strconv::from_str_float(val, rdx, false, strconv::ExpNone)
430427
}
431428
}
432429

src/libstd/num/strconv.rs

+27-109
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ use char::Char;
1717
use clone::Clone;
1818
use from_str::from_str;
1919
use iter::Iterator;
20-
use num::{NumCast, Zero, One, cast, Int, Bounded};
21-
use num::{Float, FPNaN, FPInfinite, ToPrimitive};
2220
use num;
23-
use ops::{Add, Sub, Mul, Div, Rem, Neg};
21+
use num::{Zero, One, cast, Int, Bounded};
22+
use num::{Float, FPNaN, FPInfinite, ToPrimitive};
2423
use option::{None, Option, Some};
2524
use slice::{ImmutableSlice, MutableSlice, CloneableVector};
26-
use std::cmp::{PartialOrd, PartialEq};
2725
use str::{Str, StrSlice};
2826
use string::String;
2927
use vec::Vec;
@@ -70,51 +68,6 @@ pub enum SignFormat {
7068
SignAll,
7169
}
7270

73-
/// Encompasses functions used by the string converter.
74-
pub trait NumStrConv {
75-
/// Returns the NaN value.
76-
fn nan() -> Option<Self>;
77-
78-
/// Returns the infinite value.
79-
fn inf() -> Option<Self>;
80-
81-
/// Returns the negative infinite value.
82-
fn neg_inf() -> Option<Self>;
83-
84-
/// Returns -0.0.
85-
fn neg_zero() -> Option<Self>;
86-
87-
/// Rounds the number toward zero.
88-
fn round_to_zero(&self) -> Self;
89-
90-
/// Returns the fractional part of the number.
91-
fn fractional_part(&self) -> Self;
92-
}
93-
94-
macro_rules! impl_NumStrConv_Floating (($t:ty) => (
95-
impl NumStrConv for $t {
96-
#[inline]
97-
fn nan() -> Option<$t> { Some( 0.0 / 0.0) }
98-
#[inline]
99-
fn inf() -> Option<$t> { Some( 1.0 / 0.0) }
100-
#[inline]
101-
fn neg_inf() -> Option<$t> { Some(-1.0 / 0.0) }
102-
#[inline]
103-
fn neg_zero() -> Option<$t> { Some(-0.0 ) }
104-
105-
#[inline]
106-
fn round_to_zero(&self) -> $t { self.trunc() }
107-
#[inline]
108-
fn fractional_part(&self) -> $t { self.fract() }
109-
}
110-
))
111-
112-
// FIXME: #4955
113-
// Replace by two generic impls for traits 'Integral' and 'Floating'
114-
impl_NumStrConv_Floating!(f32)
115-
impl_NumStrConv_Floating!(f64)
116-
117-
11871
// Special value strings as [u8] consts.
11972
static INF_BUF: [u8, ..3] = [b'i', b'n', b'f'];
12073
static POS_INF_BUF: [u8, ..4] = [b'+', b'i', b'n', b'f'];
@@ -234,8 +187,7 @@ fn int_to_str_bytes_common<T: Int>(num: T, radix: uint, sign: SignFormat, f: |u8
234187
* - Fails if `radix` > 25 and `exp_format` is `ExpBin` due to conflict
235188
* between digit and exponent sign `'p'`.
236189
*/
237-
pub fn float_to_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Float+
238-
Div<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
190+
pub fn float_to_str_bytes_common<T: Float>(
239191
num: T, radix: uint, negative_zero: bool,
240192
sign: SignFormat, digits: SignificantDigits, exp_format: ExponentFormat, exp_upper: bool
241193
) -> (Vec<u8>, bool) {
@@ -467,8 +419,7 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Float+
467419
* `to_str_bytes_common()`, for details see there.
468420
*/
469421
#[inline]
470-
pub fn float_to_str_common<T:NumCast+Zero+One+PartialEq+PartialOrd+NumStrConv+Float+
471-
Div<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
422+
pub fn float_to_str_common<T: Float>(
472423
num: T, radix: uint, negative_zero: bool,
473424
sign: SignFormat, digits: SignificantDigits, exp_format: ExponentFormat, exp_capital: bool
474425
) -> (String, bool) {
@@ -484,15 +435,13 @@ static DIGIT_I_RADIX: uint = ('i' as uint) - ('a' as uint) + 11u;
484435
static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u;
485436

486437
/**
487-
* Parses a byte slice as a number. This is meant to
438+
* Parses a string as a number. This is meant to
488439
* be a common base implementation for all numeric string conversion
489440
* functions like `from_str()` or `from_str_radix()`.
490441
*
491442
* # Arguments
492-
* - `buf` - The byte slice to parse.
443+
* - `src` - The string to parse.
493444
* - `radix` - Which base to parse the number as. Accepts 2-36.
494-
* - `negative` - Whether to accept negative numbers.
495-
* - `fractional` - Whether to accept numbers with fractional parts.
496445
* - `special` - Whether to accept special values like `inf`
497446
* and `NaN`. Can conflict with `radix`, see Failure.
498447
* - `exponent` - Which exponent format to accept. Options are:
@@ -504,7 +453,6 @@ static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u;
504453
* - `ExpBin`: Accepts numbers with a binary exponent like `42P-8` or
505454
* `FFp128`. The exponent string itself is always base 10.
506455
* Can conflict with `radix`, see Failure.
507-
* - `empty_zero` - Whether to accept an empty `buf` as a 0 or not.
508456
*
509457
* # Return value
510458
* Returns `Some(n)` if `buf` parses to a number n without overflowing, and
@@ -520,11 +468,8 @@ static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u;
520468
* - Fails if `radix` > 18 and `special == true` due to conflict
521469
* between digit and lowest first character in `inf` and `NaN`, the `'i'`.
522470
*/
523-
pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+
524-
Mul<T,T>+Sub<T,T>+Neg<T>+Add<T,T>+
525-
NumStrConv+Clone>(
526-
buf: &[u8], radix: uint, negative: bool, fractional: bool,
527-
special: bool, exponent: ExponentFormat, empty_zero: bool,
471+
pub fn from_str_float<T: Float>(
472+
src: &str, radix: uint, special: bool, exponent: ExponentFormat,
528473
) -> Option<T> {
529474
match exponent {
530475
ExpDec if radix >= DIGIT_E_RADIX // decimal exponent 'e'
@@ -548,33 +493,25 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+
548493
let _0: T = Zero::zero();
549494
let _1: T = One::one();
550495
let radix_gen: T = cast(radix as int).unwrap();
496+
let buf = src.as_bytes();
551497

552498
let len = buf.len();
553499

554500
if len == 0 {
555-
if empty_zero {
556-
return Some(_0);
557-
} else {
558-
return None;
559-
}
501+
return None;
560502
}
561503

562504
if special {
563505
if buf == INF_BUF || buf == POS_INF_BUF {
564-
return NumStrConv::inf();
506+
return Some(Float::infinity());
565507
} else if buf == NEG_INF_BUF {
566-
if negative {
567-
return NumStrConv::neg_inf();
568-
} else {
569-
return None;
570-
}
508+
return Some(Float::neg_infinity());
571509
} else if buf == NAN_BUF {
572-
return NumStrConv::nan();
510+
return Some(Float::nan());
573511
}
574512
}
575513

576514
let (start, accum_positive) = match buf[0] as char {
577-
'-' if !negative => return None,
578515
'-' => (1u, false),
579516
'+' => (1u, true),
580517
_ => (0u, true)
@@ -606,17 +543,17 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+
606543
// Detect overflow by comparing to last value, except
607544
// if we've not seen any non-zero digits.
608545
if last_accum != _0 {
609-
if accum_positive && accum <= last_accum { return NumStrConv::inf(); }
610-
if !accum_positive && accum >= last_accum { return NumStrConv::neg_inf(); }
546+
if accum_positive && accum <= last_accum { return Some(Float::infinity()); }
547+
if !accum_positive && accum >= last_accum { return Some(Float::neg_infinity()); }
611548

612549
// Detect overflow by reversing the shift-and-add process
613550
if accum_positive &&
614551
(last_accum != ((accum - cast(digit as int).unwrap())/radix_gen.clone())) {
615-
return NumStrConv::inf();
552+
return Some(Float::infinity());
616553
}
617554
if !accum_positive &&
618555
(last_accum != ((accum + cast(digit as int).unwrap())/radix_gen.clone())) {
619-
return NumStrConv::neg_inf();
556+
return Some(Float::neg_infinity());
620557
}
621558
}
622559
last_accum = accum.clone();
@@ -626,7 +563,7 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+
626563
exp_found = true;
627564
break; // start of exponent
628565
}
629-
'.' if fractional => {
566+
'.' => {
630567
i += 1u; // skip the '.'
631568
break; // start of fractional part
632569
}
@@ -660,8 +597,8 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+
660597
}
661598

662599
// Detect overflow by comparing to last value
663-
if accum_positive && accum < last_accum { return NumStrConv::inf(); }
664-
if !accum_positive && accum > last_accum { return NumStrConv::neg_inf(); }
600+
if accum_positive && accum < last_accum { return Some(Float::infinity()); }
601+
if !accum_positive && accum > last_accum { return Some(Float::neg_infinity()); }
665602
last_accum = accum.clone();
666603
}
667604
None => match c {
@@ -680,11 +617,7 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+
680617
// Special case: buf not empty, but does not contain any digit in front
681618
// of the exponent sign -> number is empty string
682619
if i == start {
683-
if empty_zero {
684-
return Some(_0);
685-
} else {
686-
return None;
687-
}
620+
return None;
688621
}
689622

690623
let mut multiplier = _1.clone();
@@ -717,20 +650,6 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+
717650
Some(accum * multiplier)
718651
}
719652

720-
/**
721-
* Parses a string as a number. This is a wrapper for
722-
* `from_str_bytes_common()`, for details see there.
723-
*/
724-
#[inline]
725-
pub fn from_str_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+Mul<T,T>+
726-
Sub<T,T>+Neg<T>+Add<T,T>+NumStrConv+Clone>(
727-
buf: &str, radix: uint, negative: bool, fractional: bool,
728-
special: bool, exponent: ExponentFormat, empty_zero: bool,
729-
) -> Option<T> {
730-
from_str_bytes_common(buf.as_bytes(), radix, negative,
731-
fractional, special, exponent, empty_zero)
732-
}
733-
734653
pub fn from_str_radix_int<T: Int>(src: &str, radix: uint) -> Option<T> {
735654
fn cast<T: Int>(x: uint) -> T {
736655
num::cast(x).unwrap()
@@ -791,20 +710,19 @@ pub fn from_str_radix_int<T: Int>(src: &str, radix: uint) -> Option<T> {
791710
mod test {
792711
use super::*;
793712
use option::*;
713+
use num::Float;
794714

795715
#[test]
796716
fn from_str_issue7588() {
797717
let u : Option<u8> = from_str_radix_int("1000", 10);
798718
assert_eq!(u, None);
799719
let s : Option<i16> = from_str_radix_int("80000", 10);
800720
assert_eq!(s, None);
801-
let f : Option<f32> = from_str_common(
802-
"10000000000000000000000000000000000000000", 10, false, false, false,
803-
ExpNone, false);
804-
assert_eq!(f, NumStrConv::inf())
805-
let fe : Option<f32> = from_str_common("1e40", 10, false, false, false,
806-
ExpDec, false);
807-
assert_eq!(fe, NumStrConv::inf())
721+
let f : Option<f32> = from_str_float(
722+
"10000000000000000000000000000000000000000", 10, false, ExpNone);
723+
assert_eq!(f, Some(Float::infinity()))
724+
let fe : Option<f32> = from_str_float("1e40", 10, false, ExpDec);
725+
assert_eq!(fe, Some(Float::infinity()))
808726
}
809727
}
810728

0 commit comments

Comments
 (0)