Skip to content

Commit 29bc9c6

Browse files
committed
Move FromStr to core::str
1 parent 7e43f41 commit 29bc9c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+644
-671
lines changed

src/compiletest/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::from_str::FromStr;
1211
use std::fmt;
12+
use std::str::FromStr;
1313
use regex::Regex;
1414

1515
#[deriving(Clone, PartialEq)]

src/compiletest/compiletest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern crate regex;
2222
use std::os;
2323
use std::io;
2424
use std::io::fs;
25-
use std::from_str::FromStr;
25+
use std::str::FromStr;
2626
use getopts::{optopt, optflag, reqopt};
2727
use common::Config;
2828
use common::{Pretty, DebugInfoGdb, DebugInfoLldb, Codegen};

src/compiletest/header.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use common::Config;
1212
use common;
1313
use util;
1414

15-
use std::from_str::FromStr;
16-
1715
pub struct TestProps {
1816
// Lines that should be expected, in order, on standard out
1917
pub error_patterns: Vec<String> ,
@@ -353,8 +351,8 @@ pub fn gdb_version_to_int(version_string: &str) -> int {
353351
panic!("{}", error_string);
354352
}
355353

356-
let major: int = FromStr::from_str(components[0]).expect(error_string);
357-
let minor: int = FromStr::from_str(components[1]).expect(error_string);
354+
let major: int = from_str(components[0]).expect(error_string);
355+
let minor: int = from_str(components[1]).expect(error_string);
358356

359357
return major * 1000 + minor;
360358
}
@@ -364,6 +362,6 @@ pub fn lldb_version_to_int(version_string: &str) -> int {
364362
"Encountered LLDB version string with unexpected format: {}",
365363
version_string);
366364
let error_string = error_string.as_slice();
367-
let major: int = FromStr::from_str(version_string).expect(error_string);
365+
let major: int = from_str(version_string).expect(error_string);
368366
return major;
369367
}

src/doc/reference.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,11 +3108,10 @@ then the expression completes.
31083108
Some examples of call expressions:
31093109

31103110
```
3111-
# use std::from_str::FromStr;
31123111
# fn add(x: int, y: int) -> int { 0 }
31133112
31143113
let x: int = add(1, 2);
3115-
let pi: Option<f32> = FromStr::from_str("3.14");
3114+
let pi: Option<f32> = from_str("3.14");
31163115
```
31173116

31183117
### Lambda expressions

src/libcollections/str.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub use core::str::{CharSplitsN, AnyLines, MatchIndices, StrSplits};
7373
pub use core::str::{Utf16CodeUnits, eq_slice, is_utf8, is_utf16, Utf16Items};
7474
pub use core::str::{Utf16Item, ScalarValue, LoneSurrogate, utf16_items};
7575
pub use core::str::{truncate_utf16_at_nul, utf8_char_width, CharRange};
76+
pub use core::str::{FromStr, from_str};
7677
pub use core::str::{Str, StrPrelude};
7778
pub use unicode::str::{UnicodeStrPrelude, Words, Graphemes, GraphemeIndices};
7879

src/libcollections/string.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use core::raw::Slice as RawSlice;
2525
use hash;
2626
use slice::CloneSliceAllocPrelude;
2727
use str;
28-
use str::{CharRange, StrAllocating, MaybeOwned, Owned};
28+
use str::{CharRange, FromStr, StrAllocating, MaybeOwned, Owned};
2929
use str::Slice as MaybeOwnedSlice; // So many `Slice`s...
3030
use vec::{DerefVec, Vec, as_vec};
3131

@@ -795,6 +795,13 @@ pub fn as_string<'a>(x: &'a str) -> DerefString<'a> {
795795
DerefString { x: as_vec(x.as_bytes()) }
796796
}
797797

798+
impl FromStr for String {
799+
#[inline]
800+
fn from_str(s: &str) -> Option<String> {
801+
Some(String::from_str(s))
802+
}
803+
}
804+
798805
/// Unsafe operations
799806
#[unstable = "waiting on raw module conventions"]
800807
pub mod raw {

src/libcore/num/f32.rs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
use intrinsics;
1818
use mem;
1919
use num::{FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
20-
use num::Float;
20+
use num::{Float, FromStrRadix};
21+
use num::strconv;
22+
use str::FromStr;
2123
use option::Option;
2224

2325
pub const RADIX: uint = 2u;
@@ -424,3 +426,66 @@ impl Float for f32 {
424426
self * (value / 180.0f32)
425427
}
426428
}
429+
430+
#[inline]
431+
#[allow(missing_docs)]
432+
#[deprecated="Use `FromStrRadix::from_str_radix(src, 16)`"]
433+
pub fn from_str_hex(src: &str) -> Option<f32> {
434+
strconv::from_str_radix_float(src, 16)
435+
}
436+
437+
impl FromStr for f32 {
438+
/// Convert a string in base 10 to a float.
439+
/// Accepts an optional decimal exponent.
440+
///
441+
/// This function accepts strings such as
442+
///
443+
/// * '3.14'
444+
/// * '+3.14', equivalent to '3.14'
445+
/// * '-3.14'
446+
/// * '2.5E10', or equivalently, '2.5e10'
447+
/// * '2.5E-10'
448+
/// * '.' (understood as 0)
449+
/// * '5.'
450+
/// * '.5', or, equivalently, '0.5'
451+
/// * '+inf', 'inf', '-inf', 'NaN'
452+
///
453+
/// Leading and trailing whitespace represent an error.
454+
///
455+
/// # Arguments
456+
///
457+
/// * src - A string
458+
///
459+
/// # Return value
460+
///
461+
/// `None` if the string did not represent a valid number. Otherwise,
462+
/// `Some(n)` where `n` is the floating-point number represented by `src`.
463+
#[inline]
464+
fn from_str(src: &str) -> Option<f32> {
465+
strconv::from_str_radix_float(src, 10u)
466+
}
467+
}
468+
469+
impl FromStrRadix for f32 {
470+
/// Convert a string in a given base to a float.
471+
///
472+
/// Due to possible conflicts, this function does **not** accept
473+
/// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
474+
/// does it recognize exponents of any kind.
475+
///
476+
/// Leading and trailing whitespace represent an error.
477+
///
478+
/// # Arguments
479+
///
480+
/// * src - A string
481+
/// * radix - The base to use. Must lie in the range [2 .. 36]
482+
///
483+
/// # Return value
484+
///
485+
/// `None` if the string did not represent a valid number. Otherwise,
486+
/// `Some(n)` where `n` is the floating-point number represented by `src`.
487+
#[inline]
488+
fn from_str_radix(src: &str, radix: uint) -> Option<f32> {
489+
strconv::from_str_radix_float(src, radix)
490+
}
491+
}

src/libcore/num/f64.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
use intrinsics;
1818
use mem;
1919
use num::{FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
20-
use num::Float;
20+
use num::{Float, FromStrRadix};
21+
use num::strconv;
22+
use str::FromStr;
2123
use option::Option;
2224

2325
// FIXME(#5527): These constants should be deprecated once associated
@@ -430,3 +432,61 @@ impl Float for f64 {
430432
self * (value / 180.0)
431433
}
432434
}
435+
436+
#[inline]
437+
#[allow(missing_docs)]
438+
#[deprecated="Use `FromStrRadix::from_str_radix(src, 16)`"]
439+
pub fn from_str_hex(src: &str) -> Option<f64> {
440+
strconv::from_str_radix_float(src, 16)
441+
}
442+
443+
impl FromStr for f64 {
444+
/// Convert a string in base 10 to a float.
445+
/// Accepts an optional decimal exponent.
446+
///
447+
/// This function accepts strings such as:
448+
///
449+
/// * '3.14'
450+
/// * '-3.14'
451+
/// * '2.5E10', or equivalently, '2.5e10'
452+
/// * '2.5E-10'
453+
/// * '.' (understood as 0)
454+
/// * '5.'
455+
/// * '.5', or, equivalently, '0.5'
456+
/// * inf', '-inf', 'NaN'
457+
///
458+
/// Leading and trailing whitespace represent an error.
459+
///
460+
/// # Arguments
461+
///
462+
/// * src - A string
463+
///
464+
/// # Return value
465+
///
466+
/// `none` if the string did not represent a valid number. Otherwise,
467+
/// `Some(n)` where `n` is the floating-point number represented by `src`.
468+
#[inline]
469+
fn from_str(src: &str) -> Option<f64> {
470+
strconv::from_str_radix_float(src, 10u)
471+
}
472+
}
473+
474+
impl FromStrRadix for f64 {
475+
/// Convert a string in a given base to a float.
476+
///
477+
/// Leading and trailing whitespace represent an error.
478+
///
479+
/// # Arguments
480+
///
481+
/// * src - A string
482+
/// * radix - The base to use. Must lie in the range [2 .. 36]
483+
///
484+
/// # Return value
485+
///
486+
/// `None` if the string did not represent a valid number. Otherwise,
487+
/// `Some(n)` where `n` is the floating-point number represented by `src`.
488+
#[inline]
489+
fn from_str_radix(src: &str, radix: uint) -> Option<f64> {
490+
strconv::from_str_radix_float(src, radix)
491+
}
492+
}

src/libcore/num/int_macros.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,20 @@ pub const MIN: $T = (-1 as $T) << (BITS - 1);
3232
#[unstable]
3333
pub const MAX: $T = !MIN;
3434

35+
#[experimental = "might need to return Result"]
36+
impl ::str::FromStr for $T {
37+
#[inline]
38+
fn from_str(s: &str) -> ::option::Option<$T> {
39+
::num::strconv::from_str_radix_int(s, 10)
40+
}
41+
}
42+
43+
#[experimental = "might need to return Result"]
44+
impl ::num::FromStrRadix for $T {
45+
#[inline]
46+
fn from_str_radix(s: &str, radix: uint) -> ::option::Option<$T> {
47+
::num::strconv::from_str_radix_int(s, radix)
48+
}
49+
}
50+
3551
))

src/libcore/num/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use ops::{Add, Sub, Mul, Div, Rem, Neg};
2727
use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
2828
use option::{Option, Some, None};
2929

30+
pub mod strconv;
31+
3032
/// Simultaneous division and remainder
3133
#[inline]
3234
pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
@@ -1372,6 +1374,18 @@ pub trait Float
13721374
fn to_radians(self) -> Self;
13731375
}
13741376

1377+
/// A generic trait for converting a string with a radix (base) to a value
1378+
#[experimental = "might need to return Result"]
1379+
pub trait FromStrRadix {
1380+
fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
1381+
}
1382+
1383+
/// A utility function that just calls FromStrRadix::from_str_radix.
1384+
#[experimental = "might need to return Result"]
1385+
pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: uint) -> Option<T> {
1386+
FromStrRadix::from_str_radix(str, radix)
1387+
}
1388+
13751389
// DEPRECATED
13761390

13771391
macro_rules! trait_impl {

0 commit comments

Comments
 (0)