Skip to content

Commit cf2b5e8

Browse files
committed
---
yaml --- r: 223991 b: refs/heads/beta c: 60734a6 h: refs/heads/master i: 223989: f76ab93 223987: 5b8ac35 223983: 348e378 v: v3
1 parent 7704a9a commit cf2b5e8

File tree

5 files changed

+37
-121
lines changed

5 files changed

+37
-121
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ refs/tags/0.9: 36870b185fc5f5486636d4515f0e22677493f225
2323
refs/tags/0.10: ac33f2b15782272ae348dbd7b14b8257b2148b5a
2424
refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
26-
refs/heads/beta: a41178e8582001d7075d14699542e671c78badef
26+
refs/heads/beta: 60734a65deeef396bb029b159d75a8fa29622067
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
2828
refs/heads/tmp: 938f5d7af401e2d8238522fed4a612943b6e77fd
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/beta/src/libcore/fmt/num.rs

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,18 @@ use fmt;
2020
use num::Zero;
2121
use ops::{Div, Rem, Sub};
2222
use str;
23-
use slice;
24-
use ptr;
25-
use mem;
2623

2724
#[doc(hidden)]
2825
trait Int: Zero + PartialEq + PartialOrd + Div<Output=Self> + Rem<Output=Self> +
2926
Sub<Output=Self> + Copy {
3027
fn from_u8(u: u8) -> Self;
3128
fn to_u8(&self) -> u8;
32-
fn to_u32(&self) -> u32;
33-
fn to_u64(&self) -> u64;
3429
}
3530

3631
macro_rules! doit {
3732
($($t:ident)*) => ($(impl Int for $t {
3833
fn from_u8(u: u8) -> $t { u as $t }
3934
fn to_u8(&self) -> u8 { *self as u8 }
40-
fn to_u32(&self) -> u32 { *self as u32 }
41-
fn to_u64(&self) -> u64 { *self as u64 }
4235
})*)
4336
}
4437
doit! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
@@ -195,7 +188,6 @@ macro_rules! radix_fmt {
195188
}
196189
}
197190
}
198-
199191
macro_rules! int_base {
200192
($Trait:ident for $T:ident as $U:ident -> $Radix:ident) => {
201193
#[stable(feature = "rust1", since = "1.0.0")]
@@ -217,16 +209,17 @@ macro_rules! debug {
217209
}
218210
}
219211
}
220-
221212
macro_rules! integer {
222213
($Int:ident, $Uint:ident) => {
214+
int_base! { Display for $Int as $Int -> Decimal }
223215
int_base! { Binary for $Int as $Uint -> Binary }
224216
int_base! { Octal for $Int as $Uint -> Octal }
225217
int_base! { LowerHex for $Int as $Uint -> LowerHex }
226218
int_base! { UpperHex for $Int as $Uint -> UpperHex }
227219
radix_fmt! { $Int as $Int, fmt_int }
228220
debug! { $Int }
229221

222+
int_base! { Display for $Uint as $Uint -> Decimal }
230223
int_base! { Binary for $Uint as $Uint -> Binary }
231224
int_base! { Octal for $Uint as $Uint -> Octal }
232225
int_base! { LowerHex for $Uint as $Uint -> LowerHex }
@@ -240,80 +233,3 @@ integer! { i8, u8 }
240233
integer! { i16, u16 }
241234
integer! { i32, u32 }
242235
integer! { i64, u64 }
243-
244-
const DEC_DIGITS_LUT: &'static[u8] =
245-
b"0001020304050607080910111213141516171819\
246-
2021222324252627282930313233343536373839\
247-
4041424344454647484950515253545556575859\
248-
6061626364656667686970717273747576777879\
249-
8081828384858687888990919293949596979899";
250-
251-
macro_rules! impl_Display {
252-
($($t:ident),*: $conv_fn:ident) => ($(
253-
impl fmt::Display for $t {
254-
#[allow(unused_comparisons)]
255-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
256-
let is_positive = *self >= 0;
257-
let mut n = if is_positive {
258-
self.$conv_fn()
259-
} else {
260-
// convert the negative num to positive by summing 1 to it's 2 complement
261-
(!self.$conv_fn()).wrapping_add(1)
262-
};
263-
let mut buf: [u8; 20] = unsafe { mem::uninitialized() };
264-
let mut curr = buf.len() as isize;
265-
let buf_ptr = buf.as_mut_ptr();
266-
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
267-
268-
unsafe {
269-
// eagerly decode 4 characters at a time
270-
if <$t>::max_value() as u64 >= 10000 {
271-
while n >= 10000 {
272-
let rem = (n % 10000) as isize;
273-
n /= 10000;
274-
275-
let d1 = (rem / 100) << 1;
276-
let d2 = (rem % 100) << 1;
277-
curr -= 4;
278-
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
279-
ptr::copy_nonoverlapping(lut_ptr.offset(d2), buf_ptr.offset(curr + 2), 2);
280-
}
281-
}
282-
283-
// if we reach here numbers are <= 9999, so at most 4 chars long
284-
let mut n = n as isize; // possibly reduce 64bit math
285-
286-
// decode 2 more chars, if > 2 chars
287-
if n >= 100 {
288-
let d1 = (n % 100) << 1;
289-
n /= 100;
290-
curr -= 2;
291-
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
292-
}
293-
294-
// decode last 1 or 2 chars
295-
if n < 10 {
296-
curr -= 1;
297-
*buf_ptr.offset(curr) = (n as u8) + 48;
298-
} else {
299-
let d1 = n << 1;
300-
curr -= 2;
301-
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
302-
}
303-
}
304-
305-
let buf_slice = unsafe {
306-
str::from_utf8_unchecked(
307-
slice::from_raw_parts(buf_ptr.offset(curr), buf.len() - curr as usize))
308-
};
309-
f.pad_integral(is_positive, "", buf_slice)
310-
}
311-
})*);
312-
}
313-
314-
impl_Display!(i8, u8, i16, u16, i32, u32: to_u32);
315-
impl_Display!(i64, u64: to_u64);
316-
#[cfg(target_pointer_width = "32")]
317-
impl_Display!(isize, usize: to_u32);
318-
#[cfg(target_pointer_width = "64")]
319-
impl_Display!(isize, usize: to_u64);

branches/beta/src/libcore/num/mod.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use mem::size_of;
2424
use option::Option::{self, Some, None};
2525
use result::Result::{self, Ok, Err};
2626
use str::{FromStr, StrExt};
27-
use slice::SliceExt;
2827

2928
/// Provides intentionally-wrapped arithmetic on `T`.
3029
///
@@ -1449,30 +1448,19 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32)
14491448
-> Result<T, ParseIntError> {
14501449
use self::IntErrorKind::*;
14511450
use self::ParseIntError as PIE;
1452-
14531451
assert!(radix >= 2 && radix <= 36,
14541452
"from_str_radix_int: must lie in the range `[2, 36]` - found {}",
14551453
radix);
14561454

1457-
if src.is_empty() {
1458-
return Err(PIE { kind: Empty });
1459-
}
1460-
14611455
let is_signed_ty = T::from_u32(0) > T::min_value();
14621456

1463-
// all valid digits are ascii, so we will just iterate over the utf8 bytes
1464-
// and cast them to chars. .to_digit() will safely return None for anything
1465-
// other than a valid ascii digit for a the given radix, including the first-byte
1466-
// of multi-byte sequences
1467-
let src = src.as_bytes();
1468-
1469-
match (src[0], &src[1..]) {
1470-
(b'-', digits) if digits.is_empty() => Err(PIE { kind: Empty }),
1471-
(b'-', digits) if is_signed_ty => {
1457+
match src.slice_shift_char() {
1458+
Some(('-', "")) => Err(PIE { kind: Empty }),
1459+
Some(('-', src)) if is_signed_ty => {
14721460
// The number is negative
14731461
let mut result = T::from_u32(0);
1474-
for &c in digits {
1475-
let x = match (c as char).to_digit(radix) {
1462+
for c in src.chars() {
1463+
let x = match c.to_digit(radix) {
14761464
Some(x) => x,
14771465
None => return Err(PIE { kind: InvalidDigit }),
14781466
};
@@ -1487,14 +1475,11 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32)
14871475
}
14881476
Ok(result)
14891477
},
1490-
(c, digits) => {
1478+
Some((_, _)) => {
14911479
// The number is signed
1492-
let mut result = match (c as char).to_digit(radix) {
1493-
Some(x) => T::from_u32(x),
1494-
None => return Err(PIE { kind: InvalidDigit }),
1495-
};
1496-
for &c in digits {
1497-
let x = match (c as char).to_digit(radix) {
1480+
let mut result = T::from_u32(0);
1481+
for c in src.chars() {
1482+
let x = match c.to_digit(radix) {
14981483
Some(x) => x,
14991484
None => return Err(PIE { kind: InvalidDigit }),
15001485
};
@@ -1508,7 +1493,8 @@ fn from_str_radix<T: FromStrRadixHelper>(src: &str, radix: u32)
15081493
};
15091494
}
15101495
Ok(result)
1511-
}
1496+
},
1497+
None => Err(ParseIntError { kind: Empty }),
15121498
}
15131499
}
15141500

branches/beta/src/libcoretest/num/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,7 @@ mod tests {
117117
}
118118

119119
#[test]
120-
fn test_invalid() {
121-
assert_eq!("--129".parse::<i8>().ok(), None);
122-
assert_eq!("Съешь".parse::<u8>().ok(), None);
123-
}
124-
125-
#[test]
126-
fn test_empty() {
127-
assert_eq!("-".parse::<i8>().ok(), None);
128-
assert_eq!("".parse::<u8>().ok(), None);
120+
fn test_int_from_minus_sign() {
121+
assert_eq!("-".parse::<i32>().ok(), None);
129122
}
130123
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern: too big for the current architecture
12+
13+
#[cfg(target_pointer_width = "32")]
14+
fn main() {
15+
let x = [0usize; 0xffff_ffff];
16+
}
17+
18+
#[cfg(target_pointer_width = "64")]
19+
fn main() {
20+
let x = [0usize; 0xffff_ffff_ffff_ffff];
21+
}

0 commit comments

Comments
 (0)