Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
138b76b8
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
138b76b8
编写于
11月 02, 2014
作者:
B
Brendan Zabarauskas
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Separate string->integer implementation in strconv
上级
3327ecca
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
138 addition
and
189 deletion
+138
-189
src/librustc/metadata/decoder.rs
src/librustc/metadata/decoder.rs
+4
-2
src/librustc/metadata/tydecode.rs
src/librustc/metadata/tydecode.rs
+2
-3
src/libstd/num/f32.rs
src/libstd/num/f32.rs
+3
-3
src/libstd/num/f64.rs
src/libstd/num/f64.rs
+3
-3
src/libstd/num/int_macros.rs
src/libstd/num/int_macros.rs
+35
-59
src/libstd/num/strconv.rs
src/libstd/num/strconv.rs
+67
-68
src/libstd/num/uint_macros.rs
src/libstd/num/uint_macros.rs
+23
-48
src/test/bench/shootout-pfib.rs
src/test/bench/shootout-pfib.rs
+1
-3
未找到文件。
src/librustc/metadata/decoder.rs
浏览文件 @
138b76b8
...
...
@@ -36,7 +36,7 @@
use
std
::
io
;
use
std
::
collections
::
hashmap
::
HashMap
;
use
std
::
rc
::
Rc
;
use
std
::
u64
;
use
std
::
str
;
use
rbml
::
reader
;
use
rbml
;
use
serialize
::
Decodable
;
...
...
@@ -215,7 +215,9 @@ fn each_reexport(d: rbml::Doc, f: |rbml::Doc| -> bool) -> bool {
fn
variant_disr_val
(
d
:
rbml
::
Doc
)
->
Option
<
ty
::
Disr
>
{
reader
::
maybe_get_doc
(
d
,
tag_disr_val
)
.and_then
(|
val_doc
|
{
reader
::
with_doc_data
(
val_doc
,
|
data
|
u64
::
parse_bytes
(
data
,
10u
))
reader
::
with_doc_data
(
val_doc
,
|
data
|
{
str
::
from_utf8
(
data
)
.and_then
(
from_str
)
})
})
}
...
...
src/librustc/metadata/tydecode.rs
浏览文件 @
138b76b8
...
...
@@ -23,7 +23,6 @@
use
std
::
rc
::
Rc
;
use
std
::
str
;
use
std
::
string
::
String
;
use
std
::
uint
;
use
syntax
::
abi
;
use
syntax
::
ast
;
use
syntax
::
ast
::
*
;
...
...
@@ -615,12 +614,12 @@ pub fn parse_def_id(buf: &[u8]) -> ast::DefId {
let
crate_part
=
buf
[
0u
..
colon_idx
];
let
def_part
=
buf
[
colon_idx
+
1u
..
len
];
let
crate_num
=
match
uint
::
parse_bytes
(
crate_part
,
10u
)
{
let
crate_num
=
match
str
::
from_utf8
(
crate_part
)
.and_then
(
from_str
::
<
uint
>
)
{
Some
(
cn
)
=>
cn
as
ast
::
CrateNum
,
None
=>
panic!
(
"internal error: parse_def_id: crate number expected, found {}"
,
crate_part
)
};
let
def_num
=
match
uint
::
parse_bytes
(
def_part
,
10u
)
{
let
def_num
=
match
str
::
from_utf8
(
def_part
)
.and_then
(
from_str
::
<
uint
>
)
{
Some
(
dn
)
=>
dn
as
ast
::
NodeId
,
None
=>
panic!
(
"internal error: parse_def_id: id expected, found {}"
,
def_part
)
...
...
src/libstd/num/f32.rs
浏览文件 @
138b76b8
...
...
@@ -361,7 +361,7 @@ pub fn to_str_exp_digits(num: f32, dig: uint, upper: bool) -> String {
#[inline]
pub
fn
from_str_hex
(
num
:
&
str
)
->
Option
<
f32
>
{
strconv
::
from_str_common
(
num
,
16u
,
true
,
true
,
true
,
strconv
::
ExpBin
,
false
,
false
)
strconv
::
ExpBin
,
false
)
}
impl
FromStr
for
f32
{
...
...
@@ -393,7 +393,7 @@ impl FromStr for f32 {
#[inline]
fn
from_str
(
val
:
&
str
)
->
Option
<
f32
>
{
strconv
::
from_str_common
(
val
,
10u
,
true
,
true
,
true
,
strconv
::
ExpDec
,
false
,
false
)
strconv
::
ExpDec
,
false
)
}
}
...
...
@@ -418,7 +418,7 @@ impl num::FromStrRadix for f32 {
#[inline]
fn
from_str_radix
(
val
:
&
str
,
rdx
:
uint
)
->
Option
<
f32
>
{
strconv
::
from_str_common
(
val
,
rdx
,
true
,
true
,
false
,
strconv
::
ExpNone
,
false
,
false
)
strconv
::
ExpNone
,
false
)
}
}
...
...
src/libstd/num/f64.rs
浏览文件 @
138b76b8
...
...
@@ -369,7 +369,7 @@ pub fn to_str_exp_digits(num: f64, dig: uint, upper: bool) -> String {
#[inline]
pub
fn
from_str_hex
(
num
:
&
str
)
->
Option
<
f64
>
{
strconv
::
from_str_common
(
num
,
16u
,
true
,
true
,
true
,
strconv
::
ExpBin
,
false
,
false
)
strconv
::
ExpBin
,
false
)
}
impl
FromStr
for
f64
{
...
...
@@ -401,7 +401,7 @@ impl FromStr for f64 {
#[inline]
fn
from_str
(
val
:
&
str
)
->
Option
<
f64
>
{
strconv
::
from_str_common
(
val
,
10u
,
true
,
true
,
true
,
strconv
::
ExpDec
,
false
,
false
)
strconv
::
ExpDec
,
false
)
}
}
...
...
@@ -426,7 +426,7 @@ impl num::FromStrRadix for f64 {
#[inline]
fn
from_str_radix
(
val
:
&
str
,
rdx
:
uint
)
->
Option
<
f64
>
{
strconv
::
from_str_common
(
val
,
rdx
,
true
,
true
,
false
,
strconv
::
ExpNone
,
false
,
false
)
strconv
::
ExpNone
,
false
)
}
}
...
...
src/libstd/num/int_macros.rs
浏览文件 @
138b76b8
...
...
@@ -14,31 +14,11 @@
macro_rules!
int_module
((
$T:ty
)
=>
(
// String conversion functions and impl str -> num
/// Parse a byte slice as a number in the given base
///
/// Yields an `Option` because `buf` may or may not actually be parseable.
///
/// # Examples
///
/// ```
/// let num = std::i64::parse_bytes([49,50,51,52,53,54,55,56,57], 10);
/// assert!(num == Some(123456789));
/// ```
#[inline]
#[experimental
=
"might need to return Result"
]
pub
fn
parse_bytes
(
buf
:
&
[
u8
],
radix
:
uint
)
->
Option
<
$T
>
{
strconv
::
from_str_bytes_common
(
buf
,
radix
,
true
,
false
,
false
,
strconv
::
ExpNone
,
false
,
false
)
}
#[experimental
=
"might need to return Result"
]
impl
FromStr
for
$T
{
#[inline]
fn
from_str
(
s
:
&
str
)
->
Option
<
$T
>
{
strconv
::
from_str_common
(
s
,
10u
,
true
,
false
,
false
,
strconv
::
ExpNone
,
false
,
false
)
strconv
::
from_str_radix_int
(
s
,
10
)
}
}
...
...
@@ -46,18 +26,14 @@ fn from_str(s: &str) -> Option<$T> {
impl
FromStrRadix
for
$T
{
#[inline]
fn
from_str_radix
(
s
:
&
str
,
radix
:
uint
)
->
Option
<
$T
>
{
strconv
::
from_str_common
(
s
,
radix
,
true
,
false
,
false
,
strconv
::
ExpNone
,
false
,
false
)
strconv
::
from_str_radix_int
(
s
,
radix
)
}
}
#[cfg(test)]
mod
tests
{
use
prelude
::
*
;
use
super
::
*
;
use
i32
;
use
str
::
StrSlice
;
use
num
::
FromStrRadix
;
#[test]
fn
test_from_str
()
{
...
...
@@ -73,33 +49,33 @@ fn test_from_str() {
assert_eq!
(
from_str
::
<
i32
>
(
"-123456789"
),
Some
(
-
123456789
as
i32
));
assert_eq!
(
from_str
::
<
$T
>
(
"-00100"
),
Some
(
-
100
as
$T
));
assert!
(
from_str
::
<
$T
>
(
" "
)
.is_none
());
assert!
(
from_str
::
<
$T
>
(
"x"
)
.is_none
());
assert_eq!
(
from_str
::
<
$T
>
(
""
),
None
);
assert_eq!
(
from_str
::
<
$T
>
(
" "
),
None
);
assert_eq!
(
from_str
::
<
$T
>
(
"x"
),
None
);
}
#[test]
fn
test_parse_bytes
()
{
use
str
::
StrSlice
;
assert_eq!
(
parse_bytes
(
"123"
.as_bytes
(),
10u
),
Some
(
123
as
$T
));
assert_eq!
(
parse_bytes
(
"1001"
.as_bytes
(),
2u
),
Some
(
9
as
$T
));
assert_eq!
(
parse_bytes
(
"123"
.as_bytes
(),
8u
),
Some
(
83
as
$T
));
assert_eq!
(
i32
::
parse_bytes
(
"123"
.as_bytes
(),
16u
),
Some
(
291
as
i32
));
assert_eq!
(
i32
::
parse_bytes
(
"ffff"
.as_bytes
(),
16u
),
Some
(
65535
as
i32
));
assert_eq!
(
i32
::
parse_bytes
(
"FFFF"
.as_bytes
(),
16u
),
Some
(
65535
as
i32
));
assert_eq!
(
parse_bytes
(
"z"
.as_bytes
(),
36u
),
Some
(
35
as
$T
));
assert_eq!
(
parse_bytes
(
"Z"
.as_bytes
(),
36u
),
Some
(
35
as
$T
));
assert_eq!
(
parse_bytes
(
"-123"
.as_bytes
(),
10u
),
Some
(
-
123
as
$T
));
assert_eq!
(
parse_bytes
(
"-1001"
.as_bytes
(),
2u
),
Some
(
-
9
as
$T
));
assert_eq!
(
parse_bytes
(
"-123"
.as_bytes
(),
8u
),
Some
(
-
83
as
$T
));
assert_eq!
(
i32
::
parse_bytes
(
"-123"
.as_bytes
(),
16u
),
Some
(
-
291
as
i32
));
assert_eq!
(
i32
::
parse_bytes
(
"-ffff"
.as_bytes
(),
16u
),
Some
(
-
65535
as
i32
));
assert_eq!
(
i32
::
parse_bytes
(
"-FFFF"
.as_bytes
(),
16u
),
Some
(
-
65535
as
i32
));
assert_eq!
(
parse_bytes
(
"-z"
.as_bytes
(),
36u
),
Some
(
-
35
as
$T
));
assert_eq!
(
parse_bytes
(
"-Z"
.as_bytes
(),
36u
),
Some
(
-
35
as
$T
));
assert!
(
parse_bytes
(
"Z"
.as_bytes
(),
35u
)
.is_none
());
assert!
(
parse_bytes
(
"-9"
.as_bytes
(),
2u
)
.is_none
());
fn
test_from_str_radix
()
{
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"123"
,
10
),
Some
(
123
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"1001"
,
2
),
Some
(
9
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"123"
,
8
),
Some
(
83
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"123"
,
16
),
Some
(
291
as
i32
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"ffff"
,
16
),
Some
(
65535
as
i32
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"FFFF"
,
16
),
Some
(
65535
as
i32
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"z"
,
36
),
Some
(
35
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"Z"
,
36
),
Some
(
35
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"-123"
,
10
),
Some
(
-
123
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"-1001"
,
2
),
Some
(
-
9
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"-123"
,
8
),
Some
(
-
83
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"-123"
,
16
),
Some
(
-
291
as
i32
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"-ffff"
,
16
),
Some
(
-
65535
as
i32
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"-FFFF"
,
16
),
Some
(
-
65535
as
i32
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"-z"
,
36
),
Some
(
-
35
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"-Z"
,
36
),
Some
(
-
35
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"Z"
,
35
),
None
::
<
$T
>
);
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"-9"
,
2
),
None
::
<
$T
>
);
}
#[test]
...
...
@@ -133,35 +109,35 @@ fn test_int_to_str_overflow() {
fn
test_int_from_str_overflow
()
{
let
mut
i8_val
:
i8
=
127_i8
;
assert_eq!
(
from_str
::
<
i8
>
(
"127"
),
Some
(
i8_val
));
assert
!
(
from_str
::
<
i8
>
(
"128"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
i8
>
(
"128"
),
None
);
i8_val
+=
1
as
i8
;
assert_eq!
(
from_str
::
<
i8
>
(
"-128"
),
Some
(
i8_val
));
assert
!
(
from_str
::
<
i8
>
(
"-129"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
i8
>
(
"-129"
),
None
);
let
mut
i16_val
:
i16
=
32_767_i16
;
assert_eq!
(
from_str
::
<
i16
>
(
"32767"
),
Some
(
i16_val
));
assert
!
(
from_str
::
<
i16
>
(
"32768"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
i16
>
(
"32768"
),
None
);
i16_val
+=
1
as
i16
;
assert_eq!
(
from_str
::
<
i16
>
(
"-32768"
),
Some
(
i16_val
));
assert
!
(
from_str
::
<
i16
>
(
"-32769"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
i16
>
(
"-32769"
),
None
);
let
mut
i32_val
:
i32
=
2_147_483_647_i32
;
assert_eq!
(
from_str
::
<
i32
>
(
"2147483647"
),
Some
(
i32_val
));
assert
!
(
from_str
::
<
i32
>
(
"2147483648"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
i32
>
(
"2147483648"
),
None
);
i32_val
+=
1
as
i32
;
assert_eq!
(
from_str
::
<
i32
>
(
"-2147483648"
),
Some
(
i32_val
));
assert
!
(
from_str
::
<
i32
>
(
"-2147483649"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
i32
>
(
"-2147483649"
),
None
);
let
mut
i64_val
:
i64
=
9_223_372_036_854_775_807_i64
;
assert_eq!
(
from_str
::
<
i64
>
(
"9223372036854775807"
),
Some
(
i64_val
));
assert
!
(
from_str
::
<
i64
>
(
"9223372036854775808"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
i64
>
(
"9223372036854775808"
),
None
);
i64_val
+=
1
as
i64
;
assert_eq!
(
from_str
::
<
i64
>
(
"-9223372036854775808"
),
Some
(
i64_val
));
assert
!
(
from_str
::
<
i64
>
(
"-9223372036854775809"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
i64
>
(
"-9223372036854775809"
),
None
);
}
}
...
...
src/libstd/num/strconv.rs
浏览文件 @
138b76b8
...
...
@@ -13,15 +13,18 @@
#![allow(missing_docs)]
use
char
;
use
char
::
Char
;
use
clone
::
Clone
;
use
num
::{
NumCast
,
Zero
,
One
,
cast
,
Int
};
use
from_str
::
from_str
;
use
iter
::
Iterator
;
use
num
::{
NumCast
,
Zero
,
One
,
cast
,
Int
,
Bounded
};
use
num
::{
Float
,
FPNaN
,
FPInfinite
,
ToPrimitive
};
use
num
;
use
ops
::{
Add
,
Sub
,
Mul
,
Div
,
Rem
,
Neg
};
use
option
::{
None
,
Option
,
Some
};
use
slice
::{
ImmutableSlice
,
MutableSlice
,
CloneableVector
};
use
std
::
cmp
::{
PartialOrd
,
PartialEq
};
use
str
::
StrSlice
;
use
str
::
{
Str
,
StrSlice
}
;
use
string
::
String
;
use
vec
::
Vec
;
...
...
@@ -106,35 +109,11 @@ fn fractional_part(&self) -> $t { self.fract() }
}
))
macro_rules!
impl_NumStrConv_Integer
((
$t:ty
)
=>
(
impl
NumStrConv
for
$t
{
#[inline]
fn
nan
()
->
Option
<
$t
>
{
None
}
#[inline]
fn
inf
()
->
Option
<
$t
>
{
None
}
#[inline]
fn
neg_inf
()
->
Option
<
$t
>
{
None
}
#[inline]
fn
neg_zero
()
->
Option
<
$t
>
{
None
}
#[inline]
fn
round_to_zero
(
&
self
)
->
$t
{
*
self
}
#[inline]
fn
fractional_part
(
&
self
)
->
$t
{
0
}
}
))
// FIXME: #4955
// Replace by two generic impls for traits 'Integral' and 'Floating'
impl_NumStrConv_Floating!
(
f32
)
impl_NumStrConv_Floating!
(
f64
)
impl_NumStrConv_Integer!
(
int
)
impl_NumStrConv_Integer!
(
i8
)
impl_NumStrConv_Integer!
(
i16
)
impl_NumStrConv_Integer!
(
i32
)
impl_NumStrConv_Integer!
(
i64
)
impl_NumStrConv_Integer!
(
uint
)
impl_NumStrConv_Integer!
(
u8
)
impl_NumStrConv_Integer!
(
u16
)
impl_NumStrConv_Integer!
(
u32
)
impl_NumStrConv_Integer!
(
u64
)
// Special value strings as [u8] consts.
static
INF_BUF
:
[
u8
,
..
3
]
=
[
b
'i'
,
b
'n'
,
b
'f'
];
...
...
@@ -526,8 +505,6 @@ pub fn float_to_str_common<T:NumCast+Zero+One+PartialEq+PartialOrd+NumStrConv+Fl
* `FFp128`. The exponent string itself is always base 10.
* Can conflict with `radix`, see Failure.
* - `empty_zero` - Whether to accept an empty `buf` as a 0 or not.
* - `ignore_underscores` - Whether all underscores within the string should
* be ignored.
*
* # Return value
* Returns `Some(n)` if `buf` parses to a number n without overflowing, and
...
...
@@ -548,7 +525,6 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+
NumStrConv
+
Clone
>
(
buf
:
&
[
u8
],
radix
:
uint
,
negative
:
bool
,
fractional
:
bool
,
special
:
bool
,
exponent
:
ExponentFormat
,
empty_zero
:
bool
,
ignore_underscores
:
bool
)
->
Option
<
T
>
{
match
exponent
{
ExpDec
if
radix
>=
DIGIT_E_RADIX
// decimal exponent 'e'
...
...
@@ -646,7 +622,6 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+
last_accum
=
accum
.clone
();
}
None
=>
match
c
{
'_'
if
ignore_underscores
=>
{}
'e'
|
'E'
|
'p'
|
'P'
=>
{
exp_found
=
true
;
break
;
// start of exponent
...
...
@@ -690,7 +665,6 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+
last_accum
=
accum
.clone
();
}
None
=>
match
c
{
'_'
if
ignore_underscores
=>
{}
'e'
|
'E'
|
'p'
|
'P'
=>
{
exp_found
=
true
;
break
;
// start of exponent
...
...
@@ -726,9 +700,7 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+
// parse remaining bytes as decimal integer,
// skipping the exponent char
let
exp
:
Option
<
int
>
=
from_str_bytes_common
(
buf
[
i
+
1
..
len
],
10
,
true
,
false
,
false
,
ExpNone
,
false
,
ignore_underscores
);
let
exp
=
from_str
::
<
int
>
(
String
::
from_utf8_lossy
(
buf
[
i
+
1
..
len
])
.as_slice
());
match
exp
{
Some
(
exp_pow
)
=>
{
...
...
@@ -754,11 +726,65 @@ pub fn from_str_common<T:NumCast+Zero+One+PartialEq+PartialOrd+Div<T,T>+Mul<T,T>
Sub
<
T
,
T
>+
Neg
<
T
>+
Add
<
T
,
T
>+
NumStrConv
+
Clone
>
(
buf
:
&
str
,
radix
:
uint
,
negative
:
bool
,
fractional
:
bool
,
special
:
bool
,
exponent
:
ExponentFormat
,
empty_zero
:
bool
,
ignore_underscores
:
bool
)
->
Option
<
T
>
{
from_str_bytes_common
(
buf
.as_bytes
(),
radix
,
negative
,
fractional
,
special
,
exponent
,
empty_zero
,
ignore_underscores
)
fractional
,
special
,
exponent
,
empty_zero
)
}
pub
fn
from_str_radix_int
<
T
:
Int
>
(
src
:
&
str
,
radix
:
uint
)
->
Option
<
T
>
{
fn
cast
<
T
:
Int
>
(
x
:
uint
)
->
T
{
num
::
cast
(
x
)
.unwrap
()
}
let
_0
:
T
=
num
::
zero
();
let
_1
:
T
=
num
::
one
();
let
is_signed
=
_0
>
Bounded
::
min_value
();
let
(
is_negative
,
src
)
=
match
src
.slice_shift_char
()
{
(
Some
(
'-'
),
src
)
if
is_signed
=>
(
true
,
src
),
(
Some
(
_
),
_
)
=>
(
false
,
src
),
(
None
,
_
)
=>
return
None
,
};
let
mut
xs
=
src
.chars
()
.map
(|
c
|
{
c
.to_digit
(
radix
)
.map
(
cast
)
});
let
radix
=
cast
(
radix
);
let
mut
result
=
_0
;
if
is_negative
{
for
x
in
xs
{
let
x
=
match
x
{
Some
(
x
)
=>
x
,
None
=>
return
None
,
};
result
=
match
result
.checked_mul
(
&
radix
)
{
Some
(
result
)
=>
result
,
None
=>
return
None
,
};
result
=
match
result
.checked_sub
(
&
x
)
{
Some
(
result
)
=>
result
,
None
=>
return
None
,
};
}
}
else
{
for
x
in
xs
{
let
x
=
match
x
{
Some
(
x
)
=>
x
,
None
=>
return
None
,
};
result
=
match
result
.checked_mul
(
&
radix
)
{
Some
(
result
)
=>
result
,
None
=>
return
None
,
};
result
=
match
result
.checked_add
(
&
x
)
{
Some
(
result
)
=>
result
,
None
=>
return
None
,
};
}
}
Some
(
result
)
}
#[cfg(test)]
...
...
@@ -766,45 +792,18 @@ mod test {
use
super
::
*
;
use
option
::
*
;
#[test]
fn
from_str_ignore_underscores
()
{
let
s
:
Option
<
u8
>
=
from_str_common
(
"__1__"
,
2
,
false
,
false
,
false
,
ExpNone
,
false
,
true
);
assert_eq!
(
s
,
Some
(
1u8
));
let
n
:
Option
<
u8
>
=
from_str_common
(
"__1__"
,
2
,
false
,
false
,
false
,
ExpNone
,
false
,
false
);
assert_eq!
(
n
,
None
);
let
f
:
Option
<
f32
>
=
from_str_common
(
"_1_._5_e_1_"
,
10
,
false
,
true
,
false
,
ExpDec
,
false
,
true
);
assert_eq!
(
f
,
Some
(
1.5e1f32
));
}
#[test]
fn
from_str_issue5770
()
{
// try to parse 0b1_1111_1111 = 511 as a u8. Caused problems
// since 255*2+1 == 255 (mod 256) so the overflow wasn't
// detected.
let
n
:
Option
<
u8
>
=
from_str_common
(
"111111111"
,
2
,
false
,
false
,
false
,
ExpNone
,
false
,
false
);
assert_eq!
(
n
,
None
);
}
#[test]
fn
from_str_issue7588
()
{
let
u
:
Option
<
u8
>
=
from_str_common
(
"1000"
,
10
,
false
,
false
,
false
,
ExpNone
,
false
,
false
);
let
u
:
Option
<
u8
>
=
from_str_radix_int
(
"1000"
,
10
);
assert_eq!
(
u
,
None
);
let
s
:
Option
<
i16
>
=
from_str_common
(
"80000"
,
10
,
false
,
false
,
false
,
ExpNone
,
false
,
false
);
let
s
:
Option
<
i16
>
=
from_str_radix_int
(
"80000"
,
10
);
assert_eq!
(
s
,
None
);
let
f
:
Option
<
f32
>
=
from_str_common
(
"10000000000000000000000000000000000000000"
,
10
,
false
,
false
,
false
,
ExpNone
,
false
,
false
);
ExpNone
,
false
);
assert_eq!
(
f
,
NumStrConv
::
inf
())
let
fe
:
Option
<
f32
>
=
from_str_common
(
"1e40"
,
10
,
false
,
false
,
false
,
ExpDec
,
false
,
false
);
ExpDec
,
false
);
assert_eq!
(
fe
,
NumStrConv
::
inf
())
}
}
...
...
src/libstd/num/uint_macros.rs
浏览文件 @
138b76b8
...
...
@@ -15,31 +15,11 @@
macro_rules!
uint_module
((
$T:ty
)
=>
(
// String conversion functions and impl str -> num
/// Parse a byte slice as a number in the given base
///
/// Yields an `Option` because `buf` may or may not actually be parseable.
///
/// # Examples
///
/// ```
/// let num = std::uint::parse_bytes([49,50,51,52,53,54,55,56,57], 10);
/// assert!(num == Some(123456789));
/// ```
#[inline]
#[experimental
=
"might need to return Result"
]
pub
fn
parse_bytes
(
buf
:
&
[
u8
],
radix
:
uint
)
->
Option
<
$T
>
{
strconv
::
from_str_bytes_common
(
buf
,
radix
,
false
,
false
,
false
,
strconv
::
ExpNone
,
false
,
false
)
}
#[experimental
=
"might need to return Result"
]
impl
FromStr
for
$T
{
#[inline]
fn
from_str
(
s
:
&
str
)
->
Option
<
$T
>
{
strconv
::
from_str_common
(
s
,
10u
,
false
,
false
,
false
,
strconv
::
ExpNone
,
false
,
false
)
strconv
::
from_str_radix_int
(
s
,
10
)
}
}
...
...
@@ -47,8 +27,7 @@ fn from_str(s: &str) -> Option<$T> {
impl
FromStrRadix
for
$T
{
#[inline]
fn
from_str_radix
(
s
:
&
str
,
radix
:
uint
)
->
Option
<
$T
>
{
strconv
::
from_str_common
(
s
,
radix
,
false
,
false
,
false
,
strconv
::
ExpNone
,
false
,
false
)
strconv
::
from_str_radix_int
(
s
,
radix
)
}
}
...
...
@@ -85,10 +64,7 @@ pub fn to_str_bytes<U>(n: $T, radix: uint, f: |v: &[u8]| -> U) -> U {
#[cfg(test)]
mod
tests
{
use
prelude
::
*
;
use
super
::
*
;
use
str
::
StrSlice
;
use
u16
;
use
num
::
FromStrRadix
;
#[test]
pub
fn
test_from_str
()
{
...
...
@@ -98,23 +74,22 @@ pub fn test_from_str() {
assert_eq!
(
from_str
::
<
u32
>
(
"123456789"
),
Some
(
123456789
as
u32
));
assert_eq!
(
from_str
::
<
$T
>
(
"00100"
),
Some
(
100u
as
$T
));
assert
!
(
from_str
::
<
$T
>
(
""
)
.is_none
()
);
assert
!
(
from_str
::
<
$T
>
(
" "
)
.is_none
()
);
assert
!
(
from_str
::
<
$T
>
(
"x"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
$T
>
(
""
),
None
);
assert
_eq!
(
from_str
::
<
$T
>
(
" "
),
None
);
assert
_eq!
(
from_str
::
<
$T
>
(
"x"
),
None
);
}
#[test]
pub
fn
test_parse_bytes
()
{
use
str
::
StrSlice
;
assert_eq!
(
parse_bytes
(
"123"
.as_bytes
(),
10u
),
Some
(
123u
as
$T
));
assert_eq!
(
parse_bytes
(
"1001"
.as_bytes
(),
2u
),
Some
(
9u
as
$T
));
assert_eq!
(
parse_bytes
(
"123"
.as_bytes
(),
8u
),
Some
(
83u
as
$T
));
assert_eq!
(
u16
::
parse_bytes
(
"123"
.as_bytes
(),
16u
),
Some
(
291u
as
u16
));
assert_eq!
(
u16
::
parse_bytes
(
"ffff"
.as_bytes
(),
16u
),
Some
(
65535u
as
u16
));
assert_eq!
(
parse_bytes
(
"z"
.as_bytes
(),
36u
),
Some
(
35u
as
$T
));
assert!
(
parse_bytes
(
"Z"
.as_bytes
(),
10u
)
.is_none
());
assert!
(
parse_bytes
(
"_"
.as_bytes
(),
2u
)
.is_none
());
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"123"
,
10
),
Some
(
123u
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"1001"
,
2
),
Some
(
9u
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"123"
,
8
),
Some
(
83u
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"123"
,
16
),
Some
(
291u
as
u16
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"ffff"
,
16
),
Some
(
65535u
as
u16
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"z"
,
36
),
Some
(
35u
as
$T
));
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"Z"
,
10
),
None
::
<
$T
>
);
assert_eq!
(
FromStrRadix
::
from_str_radix
(
"_"
,
2
),
None
::
<
$T
>
);
}
#[test]
...
...
@@ -148,35 +123,35 @@ fn test_uint_to_str_overflow() {
fn
test_uint_from_str_overflow
()
{
let
mut
u8_val
:
u8
=
255_u8
;
assert_eq!
(
from_str
::
<
u8
>
(
"255"
),
Some
(
u8_val
));
assert
!
(
from_str
::
<
u8
>
(
"256"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
u8
>
(
"256"
),
None
);
u8_val
+=
1
as
u8
;
assert_eq!
(
from_str
::
<
u8
>
(
"0"
),
Some
(
u8_val
));
assert
!
(
from_str
::
<
u8
>
(
"-1"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
u8
>
(
"-1"
),
None
);
let
mut
u16_val
:
u16
=
65_535_u16
;
assert_eq!
(
from_str
::
<
u16
>
(
"65535"
),
Some
(
u16_val
));
assert
!
(
from_str
::
<
u16
>
(
"65536"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
u16
>
(
"65536"
),
None
);
u16_val
+=
1
as
u16
;
assert_eq!
(
from_str
::
<
u16
>
(
"0"
),
Some
(
u16_val
));
assert
!
(
from_str
::
<
u16
>
(
"-1"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
u16
>
(
"-1"
),
None
);
let
mut
u32_val
:
u32
=
4_294_967_295_u32
;
assert_eq!
(
from_str
::
<
u32
>
(
"4294967295"
),
Some
(
u32_val
));
assert
!
(
from_str
::
<
u32
>
(
"4294967296"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
u32
>
(
"4294967296"
),
None
);
u32_val
+=
1
as
u32
;
assert_eq!
(
from_str
::
<
u32
>
(
"0"
),
Some
(
u32_val
));
assert
!
(
from_str
::
<
u32
>
(
"-1"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
u32
>
(
"-1"
),
None
);
let
mut
u64_val
:
u64
=
18_446_744_073_709_551_615_u64
;
assert_eq!
(
from_str
::
<
u64
>
(
"18446744073709551615"
),
Some
(
u64_val
));
assert
!
(
from_str
::
<
u64
>
(
"18446744073709551616"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
u64
>
(
"18446744073709551616"
),
None
);
u64_val
+=
1
as
u64
;
assert_eq!
(
from_str
::
<
u64
>
(
"0"
),
Some
(
u64_val
));
assert
!
(
from_str
::
<
u64
>
(
"-1"
)
.is_none
()
);
assert
_eq!
(
from_str
::
<
u64
>
(
"-1"
),
None
);
}
}
...
...
src/test/bench/shootout-pfib.rs
浏览文件 @
138b76b8
...
...
@@ -24,7 +24,6 @@
use
std
::
os
;
use
std
::
result
::{
Ok
,
Err
};
use
std
::
task
;
use
std
::
uint
;
fn
fib
(
n
:
int
)
->
int
{
fn
pfib
(
tx
:
&
Sender
<
int
>
,
n
:
int
)
{
...
...
@@ -102,8 +101,7 @@ fn main() {
if
opts
.stress
{
stress
(
2
);
}
else
{
let
max
=
uint
::
parse_bytes
(
args
[
1
]
.as_bytes
(),
10u
)
.unwrap
()
as
int
;
let
max
=
from_str
::
<
uint
>
(
args
[
1
]
.as_slice
())
.unwrap
()
as
int
;
let
num_trials
=
10
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录