Skip to content

Commit 138b76b

Browse files
committed
Separate string->integer implementation in strconv
1 parent 3327ecc commit 138b76b

File tree

8 files changed

+138
-189
lines changed

8 files changed

+138
-189
lines changed

src/librustc/metadata/decoder.rs

+4-2
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::hashmap::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

+2-3
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ pub fn to_str_exp_digits(num: f32, dig: uint, upper: bool) -> String {
361361
#[inline]
362362
pub fn from_str_hex(num: &str) -> Option<f32> {
363363
strconv::from_str_common(num, 16u, true, true, true,
364-
strconv::ExpBin, false, false)
364+
strconv::ExpBin, false)
365365
}
366366

367367
impl FromStr for f32 {
@@ -393,7 +393,7 @@ impl FromStr for f32 {
393393
#[inline]
394394
fn from_str(val: &str) -> Option<f32> {
395395
strconv::from_str_common(val, 10u, true, true, true,
396-
strconv::ExpDec, false, false)
396+
strconv::ExpDec, false)
397397
}
398398
}
399399

@@ -418,7 +418,7 @@ impl num::FromStrRadix for f32 {
418418
#[inline]
419419
fn from_str_radix(val: &str, rdx: uint) -> Option<f32> {
420420
strconv::from_str_common(val, rdx, true, true, false,
421-
strconv::ExpNone, false, false)
421+
strconv::ExpNone, false)
422422
}
423423
}
424424

src/libstd/num/f64.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ pub fn to_str_exp_digits(num: f64, dig: uint, upper: bool) -> String {
369369
#[inline]
370370
pub fn from_str_hex(num: &str) -> Option<f64> {
371371
strconv::from_str_common(num, 16u, true, true, true,
372-
strconv::ExpBin, false, false)
372+
strconv::ExpBin, false)
373373
}
374374

375375
impl FromStr for f64 {
@@ -401,7 +401,7 @@ impl FromStr for f64 {
401401
#[inline]
402402
fn from_str(val: &str) -> Option<f64> {
403403
strconv::from_str_common(val, 10u, true, true, true,
404-
strconv::ExpDec, false, false)
404+
strconv::ExpDec, false)
405405
}
406406
}
407407

@@ -426,7 +426,7 @@ impl num::FromStrRadix for f64 {
426426
#[inline]
427427
fn from_str_radix(val: &str, rdx: uint) -> Option<f64> {
428428
strconv::from_str_common(val, rdx, true, true, false,
429-
strconv::ExpNone, false, false)
429+
strconv::ExpNone, false)
430430
}
431431
}
432432

src/libstd/num/int_macros.rs

+35-59
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,26 @@
1414

1515
macro_rules! int_module (($T:ty) => (
1616

17-
// String conversion functions and impl str -> num
18-
19-
/// Parse a byte slice as a number in the given base
20-
///
21-
/// Yields an `Option` because `buf` may or may not actually be parseable.
22-
///
23-
/// # Examples
24-
///
25-
/// ```
26-
/// let num = std::i64::parse_bytes([49,50,51,52,53,54,55,56,57], 10);
27-
/// assert!(num == Some(123456789));
28-
/// ```
29-
#[inline]
30-
#[experimental = "might need to return Result"]
31-
pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<$T> {
32-
strconv::from_str_bytes_common(buf, radix, true, false, false,
33-
strconv::ExpNone, false, false)
34-
}
35-
3617
#[experimental = "might need to return Result"]
3718
impl FromStr for $T {
3819
#[inline]
3920
fn from_str(s: &str) -> Option<$T> {
40-
strconv::from_str_common(s, 10u, true, false, false,
41-
strconv::ExpNone, false, false)
21+
strconv::from_str_radix_int(s, 10)
4222
}
4323
}
4424

4525
#[experimental = "might need to return Result"]
4626
impl FromStrRadix for $T {
4727
#[inline]
4828
fn from_str_radix(s: &str, radix: uint) -> Option<$T> {
49-
strconv::from_str_common(s, radix, true, false, false,
50-
strconv::ExpNone, false, false)
29+
strconv::from_str_radix_int(s, radix)
5130
}
5231
}
5332

5433
#[cfg(test)]
5534
mod tests {
5635
use prelude::*;
57-
use super::*;
58-
59-
use i32;
60-
use str::StrSlice;
36+
use num::FromStrRadix;
6137

6238
#[test]
6339
fn test_from_str() {
@@ -73,33 +49,33 @@ mod tests {
7349
assert_eq!(from_str::<i32>("-123456789"), Some(-123456789 as i32));
7450
assert_eq!(from_str::<$T>("-00100"), Some(-100 as $T));
7551

76-
assert!(from_str::<$T>(" ").is_none());
77-
assert!(from_str::<$T>("x").is_none());
52+
assert_eq!(from_str::<$T>(""), None);
53+
assert_eq!(from_str::<$T>(" "), None);
54+
assert_eq!(from_str::<$T>("x"), None);
7855
}
7956

8057
#[test]
81-
fn test_parse_bytes() {
82-
use str::StrSlice;
83-
assert_eq!(parse_bytes("123".as_bytes(), 10u), Some(123 as $T));
84-
assert_eq!(parse_bytes("1001".as_bytes(), 2u), Some(9 as $T));
85-
assert_eq!(parse_bytes("123".as_bytes(), 8u), Some(83 as $T));
86-
assert_eq!(i32::parse_bytes("123".as_bytes(), 16u), Some(291 as i32));
87-
assert_eq!(i32::parse_bytes("ffff".as_bytes(), 16u), Some(65535 as i32));
88-
assert_eq!(i32::parse_bytes("FFFF".as_bytes(), 16u), Some(65535 as i32));
89-
assert_eq!(parse_bytes("z".as_bytes(), 36u), Some(35 as $T));
90-
assert_eq!(parse_bytes("Z".as_bytes(), 36u), Some(35 as $T));
91-
92-
assert_eq!(parse_bytes("-123".as_bytes(), 10u), Some(-123 as $T));
93-
assert_eq!(parse_bytes("-1001".as_bytes(), 2u), Some(-9 as $T));
94-
assert_eq!(parse_bytes("-123".as_bytes(), 8u), Some(-83 as $T));
95-
assert_eq!(i32::parse_bytes("-123".as_bytes(), 16u), Some(-291 as i32));
96-
assert_eq!(i32::parse_bytes("-ffff".as_bytes(), 16u), Some(-65535 as i32));
97-
assert_eq!(i32::parse_bytes("-FFFF".as_bytes(), 16u), Some(-65535 as i32));
98-
assert_eq!(parse_bytes("-z".as_bytes(), 36u), Some(-35 as $T));
99-
assert_eq!(parse_bytes("-Z".as_bytes(), 36u), Some(-35 as $T));
100-
101-
assert!(parse_bytes("Z".as_bytes(), 35u).is_none());
102-
assert!(parse_bytes("-9".as_bytes(), 2u).is_none());
58+
fn test_from_str_radix() {
59+
assert_eq!(FromStrRadix::from_str_radix("123", 10), Some(123 as $T));
60+
assert_eq!(FromStrRadix::from_str_radix("1001", 2), Some(9 as $T));
61+
assert_eq!(FromStrRadix::from_str_radix("123", 8), Some(83 as $T));
62+
assert_eq!(FromStrRadix::from_str_radix("123", 16), Some(291 as i32));
63+
assert_eq!(FromStrRadix::from_str_radix("ffff", 16), Some(65535 as i32));
64+
assert_eq!(FromStrRadix::from_str_radix("FFFF", 16), Some(65535 as i32));
65+
assert_eq!(FromStrRadix::from_str_radix("z", 36), Some(35 as $T));
66+
assert_eq!(FromStrRadix::from_str_radix("Z", 36), Some(35 as $T));
67+
68+
assert_eq!(FromStrRadix::from_str_radix("-123", 10), Some(-123 as $T));
69+
assert_eq!(FromStrRadix::from_str_radix("-1001", 2), Some(-9 as $T));
70+
assert_eq!(FromStrRadix::from_str_radix("-123", 8), Some(-83 as $T));
71+
assert_eq!(FromStrRadix::from_str_radix("-123", 16), Some(-291 as i32));
72+
assert_eq!(FromStrRadix::from_str_radix("-ffff", 16), Some(-65535 as i32));
73+
assert_eq!(FromStrRadix::from_str_radix("-FFFF", 16), Some(-65535 as i32));
74+
assert_eq!(FromStrRadix::from_str_radix("-z", 36), Some(-35 as $T));
75+
assert_eq!(FromStrRadix::from_str_radix("-Z", 36), Some(-35 as $T));
76+
77+
assert_eq!(FromStrRadix::from_str_radix("Z", 35), None::<$T>);
78+
assert_eq!(FromStrRadix::from_str_radix("-9", 2), None::<$T>);
10379
}
10480

10581
#[test]
@@ -133,35 +109,35 @@ mod tests {
133109
fn test_int_from_str_overflow() {
134110
let mut i8_val: i8 = 127_i8;
135111
assert_eq!(from_str::<i8>("127"), Some(i8_val));
136-
assert!(from_str::<i8>("128").is_none());
112+
assert_eq!(from_str::<i8>("128"), None);
137113

138114
i8_val += 1 as i8;
139115
assert_eq!(from_str::<i8>("-128"), Some(i8_val));
140-
assert!(from_str::<i8>("-129").is_none());
116+
assert_eq!(from_str::<i8>("-129"), None);
141117

142118
let mut i16_val: i16 = 32_767_i16;
143119
assert_eq!(from_str::<i16>("32767"), Some(i16_val));
144-
assert!(from_str::<i16>("32768").is_none());
120+
assert_eq!(from_str::<i16>("32768"), None);
145121

146122
i16_val += 1 as i16;
147123
assert_eq!(from_str::<i16>("-32768"), Some(i16_val));
148-
assert!(from_str::<i16>("-32769").is_none());
124+
assert_eq!(from_str::<i16>("-32769"), None);
149125

150126
let mut i32_val: i32 = 2_147_483_647_i32;
151127
assert_eq!(from_str::<i32>("2147483647"), Some(i32_val));
152-
assert!(from_str::<i32>("2147483648").is_none());
128+
assert_eq!(from_str::<i32>("2147483648"), None);
153129

154130
i32_val += 1 as i32;
155131
assert_eq!(from_str::<i32>("-2147483648"), Some(i32_val));
156-
assert!(from_str::<i32>("-2147483649").is_none());
132+
assert_eq!(from_str::<i32>("-2147483649"), None);
157133

158134
let mut i64_val: i64 = 9_223_372_036_854_775_807_i64;
159135
assert_eq!(from_str::<i64>("9223372036854775807"), Some(i64_val));
160-
assert!(from_str::<i64>("9223372036854775808").is_none());
136+
assert_eq!(from_str::<i64>("9223372036854775808"), None);
161137

162138
i64_val += 1 as i64;
163139
assert_eq!(from_str::<i64>("-9223372036854775808"), Some(i64_val));
164-
assert!(from_str::<i64>("-9223372036854775809").is_none());
140+
assert_eq!(from_str::<i64>("-9223372036854775809"), None);
165141
}
166142
}
167143

0 commit comments

Comments
 (0)