Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Third Party CJSON
提交
1ea72f82
T
Third Party CJSON
项目概览
OpenHarmony
/
Third Party CJSON
10 个月 前同步成功
通知
4
Star
22
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
Third Party CJSON
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1ea72f82
编写于
3月 01, 2017
作者:
M
Max Bruckner
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
print_number: Remove trailing zeroes (for doubles)
上级
e78bc423
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
47 addition
and
6 deletion
+47
-6
cJSON.c
cJSON.c
+41
-0
tests/inputs/test7.expected
tests/inputs/test7.expected
+3
-3
tests/print_number.c
tests/print_number.c
+2
-2
tests/print_value.c
tests/print_value.c
+1
-1
未找到文件。
cJSON.c
浏览文件 @
1ea72f82
...
...
@@ -324,12 +324,44 @@ static void update_offset(printbuffer * const buffer)
buffer
->
offset
+=
strlen
((
const
char
*
)
buffer_pointer
);
}
/* Removes trailing zeroes from the end of a printed number */
static
unsigned
char
*
trim_trailing_zeroes
(
printbuffer
*
const
buffer
)
{
size_t
offset
=
0
;
unsigned
char
*
content
=
NULL
;
if
((
buffer
==
NULL
)
||
(
buffer
->
buffer
==
NULL
)
||
(
buffer
->
offset
<
1
))
{
return
NULL
;
}
offset
=
buffer
->
offset
-
1
;
content
=
buffer
->
buffer
;
while
((
offset
>
0
)
&&
(
content
[
offset
]
==
'0'
))
{
offset
--
;
}
if
((
offset
>
0
)
&&
(
content
[
offset
]
==
'.'
))
{
offset
--
;
}
offset
++
;
content
[
offset
]
=
'\0'
;
buffer
->
offset
=
offset
;
return
content
+
offset
;
}
/* Render the number nicely from the given item into a string. */
static
unsigned
char
*
print_number
(
const
cJSON
*
const
item
,
printbuffer
*
const
output_buffer
,
const
internal_hooks
*
const
hooks
)
{
unsigned
char
*
output_pointer
=
NULL
;
double
d
=
item
->
valuedouble
;
int
length
=
0
;
cjbool
trim_zeroes
=
true
;
/* should at the end be removed? */
if
(
output_buffer
==
NULL
)
{
...
...
@@ -339,6 +371,8 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const
/* value is an int */
if
((
fabs
(((
double
)
item
->
valueint
)
-
d
)
<=
DBL_EPSILON
)
&&
(
d
<=
INT_MAX
)
&&
(
d
>=
INT_MIN
))
{
trim_zeroes
=
false
;
/* don't remove zeroes for integers */
/* 2^64+1 can be represented in 21 chars. */
output_pointer
=
ensure
(
output_buffer
,
21
,
hooks
);
if
(
output_pointer
!=
NULL
)
...
...
@@ -361,10 +395,12 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const
else
if
((
fabs
(
floor
(
d
)
-
d
)
<=
DBL_EPSILON
)
&&
(
fabs
(
d
)
<
1.0e60
))
{
length
=
sprintf
((
char
*
)
output_pointer
,
"%.0f"
,
d
);
trim_zeroes
=
false
;
/* don't remove zeroes for "big integers" */
}
else
if
((
fabs
(
d
)
<
1.0e-6
)
||
(
fabs
(
d
)
>
1.0e9
))
{
length
=
sprintf
((
char
*
)
output_pointer
,
"%e"
,
d
);
trim_zeroes
=
false
;
/* don't remove zeroes in engineering notation */
}
else
{
...
...
@@ -381,6 +417,11 @@ static unsigned char *print_number(const cJSON * const item, printbuffer * const
output_buffer
->
offset
+=
(
size_t
)
length
;
if
(
trim_zeroes
)
{
return
trim_trailing_zeroes
(
output_buffer
);
}
return
output_buffer
->
buffer
+
output_buffer
->
offset
;
}
...
...
tests/inputs/test7.expected
浏览文件 @
1ea72f82
[{
"precision": "zip",
"Latitude": 37.7668
00
,
"Longitude": -122.3959
00
,
"Latitude": 37.7668,
"Longitude": -122.3959,
"Address": "",
"City": "SAN FRANCISCO",
"State": "CA",
...
...
@@ -10,7 +10,7 @@
}, {
"precision": "zip",
"Latitude": 37.371991,
"Longitude": -122.02602
0
,
"Longitude": -122.02602,
"Address": "",
"City": "SUNNYVALE",
"State": "CA",
...
...
tests/print_number.c
浏览文件 @
1ea72f82
...
...
@@ -62,7 +62,7 @@ static void print_number_should_print_positive_integers(void)
static
void
print_number_should_print_positive_reals
(
void
)
{
assert_print_number
(
"0.123
000
"
,
0
.
123
);
assert_print_number
(
"0.123"
,
0
.
123
);
assert_print_number
(
"1.000000e-09"
,
10e-10
);
assert_print_number
(
"1000000000000"
,
10e11
);
assert_print_number
(
"1.230000e+129"
,
123e+127
);
...
...
@@ -71,7 +71,7 @@ static void print_number_should_print_positive_reals(void)
static
void
print_number_should_print_negative_reals
(
void
)
{
assert_print_number
(
"-0.0123
00
"
,
-
0
.
0123
);
assert_print_number
(
"-0.0123"
,
-
0
.
0123
);
assert_print_number
(
"-1.000000e-09"
,
-
10e-10
);
assert_print_number
(
"-1000000000000000000000"
,
-
10e20
);
assert_print_number
(
"-1.230000e+129"
,
-
123e+127
);
...
...
tests/print_value.c
浏览文件 @
1ea72f82
...
...
@@ -66,7 +66,7 @@ static void print_value_should_print_false(void)
static
void
print_value_should_print_number
(
void
)
{
assert_print_value
(
"1.5
00000
"
);
assert_print_value
(
"1.5"
);
}
static
void
print_value_should_print_string
(
void
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录