Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qq_38905368
tensorflow
提交
1d004f96
T
tensorflow
项目概览
qq_38905368
/
tensorflow
与 Fork 源项目一致
从无法访问的项目Fork
通知
5
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
tensorflow
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1d004f96
编写于
4月 05, 2016
作者:
A
A. Unique TensorFlower
提交者:
TensorFlower Gardener
4月 05, 2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add safe_strtou32, safe_strtou64, and safe_strtod to numbers.h.
Change: 119099796
上级
a8f0dd87
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
169 addition
and
8 deletion
+169
-8
tensorflow/core/lib/strings/numbers.cc
tensorflow/core/lib/strings/numbers.cc
+58
-8
tensorflow/core/lib/strings/numbers.h
tensorflow/core/lib/strings/numbers.h
+15
-0
tensorflow/core/lib/strings/numbers_test.cc
tensorflow/core/lib/strings/numbers_test.cc
+96
-0
未找到文件。
tensorflow/core/lib/strings/numbers.cc
浏览文件 @
1d004f96
...
...
@@ -108,11 +108,13 @@ char SafeFirstChar(StringPiece str) {
if
(
str
.
empty
())
return
'\0'
;
return
str
[
0
];
}
void
SkipSpaces
(
StringPiece
*
str
)
{
while
(
isspace
(
SafeFirstChar
(
*
str
)))
str
->
remove_prefix
(
1
);
}
}
// namespace
bool
safe_strto64
(
StringPiece
str
,
int64
*
value
)
{
// Skip leading space.
while
(
isspace
(
SafeFirstChar
(
str
)))
str
.
remove_prefix
(
1
);
SkipSpaces
(
&
str
);
int64
vlimit
=
kint64max
;
int
sign
=
1
;
...
...
@@ -145,9 +147,28 @@ bool safe_strto64(StringPiece str, int64* value) {
}
while
(
isdigit
(
SafeFirstChar
(
str
)));
}
// Skip trailing space.
while
(
isspace
(
SafeFirstChar
(
str
)))
str
.
remove_prefix
(
1
);
SkipSpaces
(
&
str
);
if
(
!
str
.
empty
())
return
false
;
*
value
=
result
;
return
true
;
}
bool
safe_strtou64
(
StringPiece
str
,
uint64
*
value
)
{
SkipSpaces
(
&
str
);
if
(
!
isdigit
(
SafeFirstChar
(
str
)))
return
false
;
int64
result
=
0
;
do
{
int
digit
=
SafeFirstChar
(
str
)
-
'0'
;
if
((
kuint64max
-
digit
)
/
10
<
result
)
{
return
false
;
}
result
=
result
*
10
+
digit
;
str
.
remove_prefix
(
1
);
}
while
(
isdigit
(
SafeFirstChar
(
str
)));
SkipSpaces
(
&
str
);
if
(
!
str
.
empty
())
return
false
;
*
value
=
result
;
...
...
@@ -155,8 +176,7 @@ bool safe_strto64(StringPiece str, int64* value) {
}
bool
safe_strto32
(
StringPiece
str
,
int32
*
value
)
{
// Skip leading space.
while
(
isspace
(
SafeFirstChar
(
str
)))
str
.
remove_prefix
(
1
);
SkipSpaces
(
&
str
);
int64
vmax
=
kint32max
;
int
sign
=
1
;
...
...
@@ -177,8 +197,7 @@ bool safe_strto32(StringPiece str, int32* value) {
str
.
remove_prefix
(
1
);
}
while
(
isdigit
(
SafeFirstChar
(
str
)));
// Skip trailing space.
while
(
isspace
(
SafeFirstChar
(
str
)))
str
.
remove_prefix
(
1
);
SkipSpaces
(
&
str
);
if
(
!
str
.
empty
())
return
false
;
...
...
@@ -186,6 +205,26 @@ bool safe_strto32(StringPiece str, int32* value) {
return
true
;
}
bool
safe_strtou32
(
StringPiece
str
,
uint32
*
value
)
{
SkipSpaces
(
&
str
);
if
(
!
isdigit
(
SafeFirstChar
(
str
)))
return
false
;
int64
result
=
0
;
do
{
result
=
result
*
10
+
SafeFirstChar
(
str
)
-
'0'
;
if
(
result
>
kuint32max
)
{
return
false
;
}
str
.
remove_prefix
(
1
);
}
while
(
isdigit
(
SafeFirstChar
(
str
)));
SkipSpaces
(
&
str
);
if
(
!
str
.
empty
())
return
false
;
*
value
=
result
;
return
true
;
}
bool
safe_strtof
(
const
char
*
str
,
float
*
value
)
{
char
*
endptr
;
*
value
=
strtof
(
str
,
&
endptr
);
...
...
@@ -197,6 +236,17 @@ bool safe_strtof(const char* str, float* value) {
return
*
str
!=
'\0'
&&
*
endptr
==
'\0'
;
}
bool
safe_strtod
(
const
char
*
str
,
double
*
value
)
{
char
*
endptr
;
*
value
=
strtod
(
str
,
&
endptr
);
while
(
isspace
(
*
endptr
))
++
endptr
;
// Ignore range errors from strtod/strtof.
// The values it returns on underflow and
// overflow are the right fallback in a
// robust setting.
return
*
str
!=
'\0'
&&
*
endptr
==
'\0'
;
}
char
*
FloatToBuffer
(
float
value
,
char
*
buffer
)
{
// FLT_DIG is 6 for IEEE-754 floats, which are used on almost all
// platforms these days. Just in case some system exists where FLT_DIG
...
...
tensorflow/core/lib/strings/numbers.h
浏览文件 @
1d004f96
...
...
@@ -97,16 +97,31 @@ bool HexStringToUint64(const StringPiece& s, uint64* v);
// Return false with overflow or invalid input.
bool
safe_strto32
(
StringPiece
str
,
int32
*
value
);
// Convert strings to unsigned 32bit integer values.
// Leading and trailing spaces are allowed.
// Return false with overflow or invalid input.
bool
safe_strtou32
(
StringPiece
str
,
uint32
*
value
);
// Convert strings to 64bit integer values.
// Leading and trailing spaces are allowed.
// Return false with overflow or invalid input.
bool
safe_strto64
(
StringPiece
str
,
int64
*
value
);
// Convert strings to unsigned 64bit integer values.
// Leading and trailing spaces are allowed.
// Return false with overflow or invalid input.
bool
safe_strtou64
(
StringPiece
str
,
uint64
*
value
);
// Convert strings to floating point values.
// Leading and trailing spaces are allowed.
// Values may be rounded on over- and underflow.
bool
safe_strtof
(
const
char
*
str
,
float
*
value
);
// Convert strings to double precision floating point values.
// Leading and trailing spaces are allowed.
// Values may be rounded on over- and underflow.
bool
safe_strtod
(
const
char
*
str
,
double
*
value
);
// Converts from an int64 representing a number of bytes to a
// human readable string representing the same number.
// e.g. 12345678 -> "11.77MiB".
...
...
tensorflow/core/lib/strings/numbers_test.cc
浏览文件 @
1d004f96
...
...
@@ -119,6 +119,39 @@ TEST(safe_strto32, Int32s) {
EXPECT_EQ
(
false
,
safe_strto32
(
StringPiece
(
nullptr
,
0
),
&
result
));
}
TEST
(
safe_strtou32
,
UInt32s
)
{
uint32
result
;
EXPECT_TRUE
(
safe_strtou32
(
"0"
,
&
result
));
EXPECT_EQ
(
0
,
result
);
EXPECT_TRUE
(
safe_strtou32
(
"1"
,
&
result
));
EXPECT_EQ
(
1
,
result
);
EXPECT_TRUE
(
safe_strtou32
(
"123"
,
&
result
));
EXPECT_EQ
(
123
,
result
);
EXPECT_TRUE
(
safe_strtou32
(
"4294967295"
,
&
result
));
EXPECT_EQ
(
4294967295
,
result
);
// Invalid argument
EXPECT_FALSE
(
safe_strtou32
(
" 132as "
,
&
result
));
EXPECT_FALSE
(
safe_strtou32
(
" 132.2 "
,
&
result
));
EXPECT_FALSE
(
safe_strtou32
(
" -"
,
&
result
));
EXPECT_FALSE
(
safe_strtou32
(
""
,
&
result
));
EXPECT_FALSE
(
safe_strtou32
(
" "
,
&
result
));
EXPECT_FALSE
(
safe_strtou32
(
"123 a"
,
&
result
));
EXPECT_FALSE
(
safe_strtou32
(
"123 456"
,
&
result
));
// Overflow
EXPECT_FALSE
(
safe_strtou32
(
"4294967296"
,
&
result
));
EXPECT_FALSE
(
safe_strtou32
(
"-1"
,
&
result
));
// Check that the StringPiece's length is respected.
EXPECT_TRUE
(
safe_strtou32
(
StringPiece
(
"123"
,
1
),
&
result
));
EXPECT_EQ
(
1
,
result
);
EXPECT_TRUE
(
safe_strtou32
(
StringPiece
(
" 123"
,
3
),
&
result
));
EXPECT_EQ
(
12
,
result
);
EXPECT_FALSE
(
safe_strtou32
(
StringPiece
(
nullptr
,
0
),
&
result
));
}
TEST
(
safe_strto64
,
Int64s
)
{
int64
result
;
...
...
@@ -155,5 +188,68 @@ TEST(safe_strto64, Int64s) {
EXPECT_EQ
(
false
,
safe_strto64
(
StringPiece
(
nullptr
,
0
),
&
result
));
}
TEST
(
safe_strtou64
,
UInt64s
)
{
uint64
result
;
EXPECT_TRUE
(
safe_strtou64
(
"0"
,
&
result
));
EXPECT_EQ
(
0
,
result
);
EXPECT_TRUE
(
safe_strtou64
(
"1"
,
&
result
));
EXPECT_EQ
(
1
,
result
);
EXPECT_TRUE
(
safe_strtou64
(
"123"
,
&
result
));
EXPECT_EQ
(
123
,
result
);
EXPECT_TRUE
(
safe_strtou64
(
" 345 "
,
&
result
));
EXPECT_EQ
(
345
,
result
);
EXPECT_TRUE
(
safe_strtou64
(
"18446744073709551615"
,
&
result
));
EXPECT_EQ
(
18446744073709551615UL
,
result
);
// Invalid argument
EXPECT_FALSE
(
safe_strtou64
(
" 132.2 "
,
&
result
));
EXPECT_FALSE
(
safe_strtou64
(
" 132.2 "
,
&
result
));
EXPECT_FALSE
(
safe_strtou64
(
" -"
,
&
result
));
EXPECT_FALSE
(
safe_strtou64
(
""
,
&
result
));
EXPECT_FALSE
(
safe_strtou64
(
" "
,
&
result
));
EXPECT_FALSE
(
safe_strtou64
(
"123 a"
,
&
result
));
EXPECT_FALSE
(
safe_strtou64
(
"123 456"
,
&
result
));
// Overflow
EXPECT_FALSE
(
safe_strtou64
(
"18446744073709551616"
,
&
result
));
EXPECT_FALSE
(
safe_strtou64
(
"-1"
,
&
result
));
// Check that the StringPiece's length is respected.
EXPECT_TRUE
(
safe_strtou64
(
StringPiece
(
"123"
,
1
),
&
result
));
EXPECT_EQ
(
1
,
result
);
EXPECT_TRUE
(
safe_strtou64
(
StringPiece
(
" 123"
,
3
),
&
result
));
EXPECT_EQ
(
12
,
result
);
EXPECT_FALSE
(
safe_strtou64
(
StringPiece
(
nullptr
,
0
),
&
result
));
}
TEST
(
safe_strtof
,
Float
)
{
float
result
=
0
;
EXPECT_TRUE
(
safe_strtof
(
"0.123456"
,
&
result
));
EXPECT_EQ
(
0.123456
f
,
result
);
EXPECT_FALSE
(
safe_strtof
(
"0.12345abc"
,
&
result
));
// Overflow to infinity, underflow to 0.
EXPECT_TRUE
(
safe_strtof
(
"1e39"
,
&
result
));
EXPECT_EQ
(
std
::
numeric_limits
<
float
>::
infinity
(),
result
);
EXPECT_TRUE
(
safe_strtof
(
"1e-50"
,
&
result
));
EXPECT_EQ
(
0
,
result
);
}
TEST
(
safe_strtod
,
Double
)
{
double
result
=
0
;
EXPECT_TRUE
(
safe_strtod
(
"0.1234567890123"
,
&
result
));
EXPECT_EQ
(
0.1234567890123
,
result
);
EXPECT_FALSE
(
safe_strtod
(
"0.1234567890123abc"
,
&
result
));
// Overflow to infinity, underflow to 0.
EXPECT_TRUE
(
safe_strtod
(
"1e310"
,
&
result
));
EXPECT_EQ
(
std
::
numeric_limits
<
double
>::
infinity
(),
result
);
EXPECT_TRUE
(
safe_strtod
(
"1e-325"
,
&
result
));
EXPECT_EQ
(
0
,
result
);
}
}
// namespace strings
}
// namespace tensorflow
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录