Skip to content

Commit ee5d238

Browse files
committed
rollup merge of rust-lang#18536 : bjz/strconv
2 parents a5aba68 + 8bd37e6 commit ee5d238

File tree

8 files changed

+283
-552
lines changed

8 files changed

+283
-552
lines changed

src/librustc/metadata/decoder.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use std::io::extensions::u64_from_be_bytes;
3636
use std::io;
3737
use std::collections::hash_map::HashMap;
3838
use std::rc::Rc;
39-
use std::u64;
39+
use std::str;
4040
use rbml::reader;
4141
use rbml;
4242
use serialize::Decodable;
@@ -215,7 +215,9 @@ fn each_reexport(d: rbml::Doc, f: |rbml::Doc| -> bool) -> bool {
215215

216216
fn variant_disr_val(d: rbml::Doc) -> Option<ty::Disr> {
217217
reader::maybe_get_doc(d, tag_disr_val).and_then(|val_doc| {
218-
reader::with_doc_data(val_doc, |data| u64::parse_bytes(data, 10u))
218+
reader::with_doc_data(val_doc, |data| {
219+
str::from_utf8(data).and_then(from_str)
220+
})
219221
})
220222
}
221223

src/librustc/metadata/tydecode.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use middle::ty;
2323
use std::rc::Rc;
2424
use std::str;
2525
use std::string::String;
26-
use std::uint;
2726
use syntax::abi;
2827
use syntax::ast;
2928
use syntax::ast::*;
@@ -615,12 +614,12 @@ pub fn parse_def_id(buf: &[u8]) -> ast::DefId {
615614
let crate_part = buf[0u..colon_idx];
616615
let def_part = buf[colon_idx + 1u..len];
617616

618-
let crate_num = match uint::parse_bytes(crate_part, 10u) {
617+
let crate_num = match str::from_utf8(crate_part).and_then(from_str::<uint>) {
619618
Some(cn) => cn as ast::CrateNum,
620619
None => panic!("internal error: parse_def_id: crate number expected, found {}",
621620
crate_part)
622621
};
623-
let def_num = match uint::parse_bytes(def_part, 10u) {
622+
let def_num = match str::from_utf8(def_part).and_then(from_str::<uint>) {
624623
Some(dn) => dn as ast::NodeId,
625624
None => panic!("internal error: parse_def_id: id expected, found {}",
626625
def_part)

src/libstd/num/f32.rs

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -333,35 +333,10 @@ pub fn to_str_exp_digits(num: f32, dig: uint, upper: bool) -> String {
333333
r
334334
}
335335

336-
/// Convert a string in base 16 to a float.
337-
/// Accepts an optional binary exponent.
338-
///
339-
/// This function accepts strings such as
340-
///
341-
/// * 'a4.fe'
342-
/// * '+a4.fe', equivalent to 'a4.fe'
343-
/// * '-a4.fe'
344-
/// * '2b.aP128', or equivalently, '2b.ap128'
345-
/// * '2b.aP-128'
346-
/// * '.' (understood as 0)
347-
/// * 'c.'
348-
/// * '.c', or, equivalently, '0.c'
349-
/// * '+inf', 'inf', '-inf', 'NaN'
350-
///
351-
/// Leading and trailing whitespace represent an error.
352-
///
353-
/// # Arguments
354-
///
355-
/// * num - A string
356-
///
357-
/// # Return value
358-
///
359-
/// `None` if the string did not represent a valid number. Otherwise,
360-
/// `Some(n)` where `n` is the floating-point number represented by `[num]`.
361336
#[inline]
362-
pub fn from_str_hex(num: &str) -> Option<f32> {
363-
strconv::from_str_common(num, 16u, true, true, true,
364-
strconv::ExpBin, false, false)
337+
#[deprecated="Use `FromStrRadix::from_str_radix(src, 16)`"]
338+
pub fn from_str_hex(src: &str) -> Option<f32> {
339+
strconv::from_str_radix_float(src, 16)
365340
}
366341

367342
impl FromStr for f32 {
@@ -384,16 +359,15 @@ impl FromStr for f32 {
384359
///
385360
/// # Arguments
386361
///
387-
/// * num - A string
362+
/// * src - A string
388363
///
389364
/// # Return value
390365
///
391366
/// `None` if the string did not represent a valid number. Otherwise,
392-
/// `Some(n)` where `n` is the floating-point number represented by `num`.
367+
/// `Some(n)` where `n` is the floating-point number represented by `src`.
393368
#[inline]
394-
fn from_str(val: &str) -> Option<f32> {
395-
strconv::from_str_common(val, 10u, true, true, true,
396-
strconv::ExpDec, false, false)
369+
fn from_str(src: &str) -> Option<f32> {
370+
strconv::from_str_radix_float(src, 10u)
397371
}
398372
}
399373

@@ -408,17 +382,16 @@ impl num::FromStrRadix for f32 {
408382
///
409383
/// # Arguments
410384
///
411-
/// * num - A string
385+
/// * src - A string
412386
/// * radix - The base to use. Must lie in the range [2 .. 36]
413387
///
414388
/// # Return value
415389
///
416390
/// `None` if the string did not represent a valid number. Otherwise,
417-
/// `Some(n)` where `n` is the floating-point number represented by `num`.
391+
/// `Some(n)` where `n` is the floating-point number represented by `src`.
418392
#[inline]
419-
fn from_str_radix(val: &str, rdx: uint) -> Option<f32> {
420-
strconv::from_str_common(val, rdx, true, true, false,
421-
strconv::ExpNone, false, false)
393+
fn from_str_radix(src: &str, radix: uint) -> Option<f32> {
394+
strconv::from_str_radix_float(src, radix)
422395
}
423396
}
424397

@@ -710,8 +683,8 @@ mod tests {
710683
fn test_ldexp() {
711684
// We have to use from_str until base-2 exponents
712685
// are supported in floating-point literals
713-
let f1: f32 = from_str_hex("1p-123").unwrap();
714-
let f2: f32 = from_str_hex("1p-111").unwrap();
686+
let f1: f32 = FromStrRadix::from_str_radix("1p-123", 16).unwrap();
687+
let f2: f32 = FromStrRadix::from_str_radix("1p-111", 16).unwrap();
715688
assert_eq!(FloatMath::ldexp(1f32, -123), f1);
716689
assert_eq!(FloatMath::ldexp(1f32, -111), f2);
717690

@@ -730,8 +703,8 @@ mod tests {
730703
fn test_frexp() {
731704
// We have to use from_str until base-2 exponents
732705
// are supported in floating-point literals
733-
let f1: f32 = from_str_hex("1p-123").unwrap();
734-
let f2: f32 = from_str_hex("1p-111").unwrap();
706+
let f1: f32 = FromStrRadix::from_str_radix("1p-123", 16).unwrap();
707+
let f2: f32 = FromStrRadix::from_str_radix("1p-111", 16).unwrap();
735708
let (x1, exp1) = f1.frexp();
736709
let (x2, exp2) = f2.frexp();
737710
assert_eq!((x1, exp1), (0.5f32, -122));

src/libstd/num/f64.rs

Lines changed: 17 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -341,92 +341,60 @@ pub fn to_str_exp_digits(num: f64, dig: uint, upper: bool) -> String {
341341
r
342342
}
343343

344-
/// Convert a string in base 16 to a float.
345-
/// Accepts an optional binary exponent.
346-
///
347-
/// This function accepts strings such as
348-
///
349-
/// * 'a4.fe'
350-
/// * '+a4.fe', equivalent to 'a4.fe'
351-
/// * '-a4.fe'
352-
/// * '2b.aP128', or equivalently, '2b.ap128'
353-
/// * '2b.aP-128'
354-
/// * '.' (understood as 0)
355-
/// * 'c.'
356-
/// * '.c', or, equivalently, '0.c'
357-
/// * '+inf', 'inf', '-inf', 'NaN'
358-
///
359-
/// Leading and trailing whitespace represent an error.
360-
///
361-
/// # Arguments
362-
///
363-
/// * num - A string
364-
///
365-
/// # Return value
366-
///
367-
/// `None` if the string did not represent a valid number. Otherwise,
368-
/// `Some(n)` where `n` is the floating-point number represented by `[num]`.
369344
#[inline]
370-
pub fn from_str_hex(num: &str) -> Option<f64> {
371-
strconv::from_str_common(num, 16u, true, true, true,
372-
strconv::ExpBin, false, false)
345+
#[deprecated="Use `FromStrRadix::from_str_radix(src, 16)`"]
346+
pub fn from_str_hex(src: &str) -> Option<f64> {
347+
strconv::from_str_radix_float(src, 16)
373348
}
374349

375350
impl FromStr for f64 {
376351
/// Convert a string in base 10 to a float.
377352
/// Accepts an optional decimal exponent.
378353
///
379-
/// This function accepts strings such as
354+
/// This function accepts strings such as:
380355
///
381356
/// * '3.14'
382-
/// * '+3.14', equivalent to '3.14'
383357
/// * '-3.14'
384358
/// * '2.5E10', or equivalently, '2.5e10'
385359
/// * '2.5E-10'
386360
/// * '.' (understood as 0)
387361
/// * '5.'
388362
/// * '.5', or, equivalently, '0.5'
389-
/// * '+inf', 'inf', '-inf', 'NaN'
363+
/// * inf', '-inf', 'NaN'
390364
///
391365
/// Leading and trailing whitespace represent an error.
392366
///
393367
/// # Arguments
394368
///
395-
/// * num - A string
369+
/// * src - A string
396370
///
397371
/// # Return value
398372
///
399373
/// `none` if the string did not represent a valid number. Otherwise,
400-
/// `Some(n)` where `n` is the floating-point number represented by `num`.
374+
/// `Some(n)` where `n` is the floating-point number represented by `src`.
401375
#[inline]
402-
fn from_str(val: &str) -> Option<f64> {
403-
strconv::from_str_common(val, 10u, true, true, true,
404-
strconv::ExpDec, false, false)
376+
fn from_str(src: &str) -> Option<f64> {
377+
strconv::from_str_radix_float(src, 10u)
405378
}
406379
}
407380

408381
impl num::FromStrRadix for f64 {
409382
/// Convert a string in a given base to a float.
410383
///
411-
/// Due to possible conflicts, this function does **not** accept
412-
/// the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
413-
/// does it recognize exponents of any kind.
414-
///
415384
/// Leading and trailing whitespace represent an error.
416385
///
417386
/// # Arguments
418387
///
419-
/// * num - A string
388+
/// * src - A string
420389
/// * radix - The base to use. Must lie in the range [2 .. 36]
421390
///
422391
/// # Return value
423392
///
424393
/// `None` if the string did not represent a valid number. Otherwise,
425-
/// `Some(n)` where `n` is the floating-point number represented by `num`.
394+
/// `Some(n)` where `n` is the floating-point number represented by `src`.
426395
#[inline]
427-
fn from_str_radix(val: &str, rdx: uint) -> Option<f64> {
428-
strconv::from_str_common(val, rdx, true, true, false,
429-
strconv::ExpNone, false, false)
396+
fn from_str_radix(src: &str, radix: uint) -> Option<f64> {
397+
strconv::from_str_radix_float(src, radix)
430398
}
431399
}
432400

@@ -712,8 +680,8 @@ mod tests {
712680
fn test_ldexp() {
713681
// We have to use from_str until base-2 exponents
714682
// are supported in floating-point literals
715-
let f1: f64 = from_str_hex("1p-123").unwrap();
716-
let f2: f64 = from_str_hex("1p-111").unwrap();
683+
let f1: f64 = FromStrRadix::from_str_radix("1p-123", 16).unwrap();
684+
let f2: f64 = FromStrRadix::from_str_radix("1p-111", 16).unwrap();
717685
assert_eq!(FloatMath::ldexp(1f64, -123), f1);
718686
assert_eq!(FloatMath::ldexp(1f64, -111), f2);
719687

@@ -732,8 +700,8 @@ mod tests {
732700
fn test_frexp() {
733701
// We have to use from_str until base-2 exponents
734702
// are supported in floating-point literals
735-
let f1: f64 = from_str_hex("1p-123").unwrap();
736-
let f2: f64 = from_str_hex("1p-111").unwrap();
703+
let f1: f64 = FromStrRadix::from_str_radix("1p-123", 16).unwrap();
704+
let f2: f64 = FromStrRadix::from_str_radix("1p-111", 16).unwrap();
737705
let (x1, exp1) = f1.frexp();
738706
let (x2, exp2) = f2.frexp();
739707
assert_eq!((x1, exp1), (0.5f64, -122));

0 commit comments

Comments
 (0)