Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
83f9f07e
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,发现更多精彩内容 >>
提交
83f9f07e
编写于
7月 04, 2014
作者:
E
Erick Tryzelaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
serialize: Remove allocations from escaping strs and indenting spaces
上级
04ac2b08
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
63 addition
and
38 deletion
+63
-38
src/libserialize/json.rs
src/libserialize/json.rs
+63
-38
未找到文件。
src/libserialize/json.rs
浏览文件 @
83f9f07e
...
...
@@ -256,22 +256,38 @@ fn io_error_to_error(io: io::IoError) -> ParserError {
pub
type
EncodeResult
=
io
::
IoResult
<
()
>
;
pub
type
DecodeResult
<
T
>
=
Result
<
T
,
DecoderError
>
;
fn
escape_str
(
s
:
&
str
)
->
String
{
let
mut
escaped
=
String
::
from_str
(
"
\"
"
);
for
c
in
s
.chars
()
{
match
c
{
'"'
=>
escaped
.push_str
(
"
\\\"
"
),
'\\'
=>
escaped
.push_str
(
"
\\\\
"
),
'\x08'
=>
escaped
.push_str
(
"
\\
b"
),
'\x0c'
=>
escaped
.push_str
(
"
\\
f"
),
'\n'
=>
escaped
.push_str
(
"
\\
n"
),
'\r'
=>
escaped
.push_str
(
"
\\
r"
),
'\t'
=>
escaped
.push_str
(
"
\\
t"
),
_
=>
escaped
.push_char
(
c
),
}
};
escaped
.push_char
(
'"'
);
escaped
fn
escape_bytes
(
writer
:
&
mut
io
::
Writer
,
s
:
&
[
u8
])
->
Result
<
(),
io
::
IoError
>
{
try!
(
writer
.write_str
(
"
\"
"
));
for
byte
in
s
.iter
()
{
match
*
byte
{
b
'"'
=>
try!
(
writer
.write_str
(
"
\\\"
"
)),
b
'\\'
=>
try!
(
writer
.write_str
(
"
\\\\
"
)),
b
'\x08'
=>
try!
(
writer
.write_str
(
"
\\
b"
)),
b
'\x0c'
=>
try!
(
writer
.write_str
(
"
\\
f"
)),
b
'\n'
=>
try!
(
writer
.write_str
(
"
\\
n"
)),
b
'\r'
=>
try!
(
writer
.write_str
(
"
\\
r"
)),
b
'\t'
=>
try!
(
writer
.write_str
(
"
\\
t"
)),
_
=>
try!
(
writer
.write_u8
(
*
byte
)),
}
}
writer
.write_str
(
"
\"
"
)
}
fn
escape_str
(
writer
:
&
mut
io
::
Writer
,
v
:
&
str
)
->
Result
<
(),
io
::
IoError
>
{
escape_bytes
(
writer
,
v
.as_bytes
())
}
fn
escape_char
(
writer
:
&
mut
io
::
Writer
,
v
:
char
)
->
Result
<
(),
io
::
IoError
>
{
let
mut
buf
=
[
0
,
..
4
];
v
.encode_utf8
(
buf
);
escape_bytes
(
writer
,
buf
)
}
fn
spaces
(
writer
:
&
mut
io
::
Writer
,
n
:
uint
)
->
Result
<
(),
io
::
IoError
>
{
for
_
in
range
(
0
,
n
)
{
try!
(
writer
.write_str
(
" "
));
}
Ok
(())
}
fn
fmt_number_or_null
(
v
:
f64
)
->
String
{
...
...
@@ -281,10 +297,6 @@ fn fmt_number_or_null(v: f64) -> String {
}
}
fn
spaces
(
n
:
uint
)
->
String
{
String
::
from_char
(
n
,
' '
)
}
/// A structure for implementing serialization to JSON.
pub
struct
Encoder
<
'a
>
{
writer
:
&
'a
mut
io
::
Writer
,
...
...
@@ -348,10 +360,10 @@ fn emit_f64(&mut self, v: f64) -> EncodeResult {
fn
emit_f32
(
&
mut
self
,
v
:
f32
)
->
EncodeResult
{
self
.emit_f64
(
v
as
f64
)
}
fn
emit_char
(
&
mut
self
,
v
:
char
)
->
EncodeResult
{
self
.emit_str
(
str
::
from_char
(
v
)
.as_slice
()
)
escape_char
(
self
.writer
,
v
)
}
fn
emit_str
(
&
mut
self
,
v
:
&
str
)
->
EncodeResult
{
write!
(
self
.writer
,
"{}"
,
escape_str
(
v
)
)
escape_str
(
self
.writer
,
v
)
}
fn
emit_enum
(
&
mut
self
,
_
name
:
&
str
,
f
:
|
&
mut
Encoder
<
'a
>
|
->
EncodeResult
)
->
EncodeResult
{
...
...
@@ -367,10 +379,10 @@ fn emit_enum_variant(&mut self,
// Bunny => "Bunny"
// Kangaroo(34,"William") => {"variant": "Kangaroo", "fields": [34,"William"]}
if
cnt
==
0
{
write!
(
self
.writer
,
"{}"
,
escape_str
(
name
)
)
escape_str
(
self
.writer
,
name
)
}
else
{
try!
(
write!
(
self
.writer
,
"{{
\"
variant
\"
:"
));
try!
(
write!
(
self
.writer
,
"{}"
,
escape_str
(
name
)
));
try!
(
escape_str
(
self
.writer
,
name
));
try!
(
write!
(
self
.writer
,
",
\"
fields
\"
:["
));
try!
(
f
(
self
));
write!
(
self
.writer
,
"]}}"
)
...
...
@@ -415,7 +427,8 @@ fn emit_struct_field(&mut self,
idx
:
uint
,
f
:
|
&
mut
Encoder
<
'a
>
|
->
EncodeResult
)
->
EncodeResult
{
if
idx
!=
0
{
try!
(
write!
(
self
.writer
,
","
));
}
try!
(
write!
(
self
.writer
,
"{}:"
,
escape_str
(
name
)));
try!
(
escape_str
(
self
.writer
,
name
));
try!
(
write!
(
self
.writer
,
":"
));
f
(
self
)
}
...
...
@@ -541,10 +554,10 @@ fn emit_f32(&mut self, v: f32) -> EncodeResult {
}
fn
emit_char
(
&
mut
self
,
v
:
char
)
->
EncodeResult
{
self
.emit_str
(
str
::
from_char
(
v
)
.as_slice
()
)
escape_char
(
self
.writer
,
v
)
}
fn
emit_str
(
&
mut
self
,
v
:
&
str
)
->
EncodeResult
{
write!
(
self
.writer
,
"{}"
,
escape_str
(
v
)
)
escape_str
(
self
.writer
,
v
)
}
fn
emit_enum
(
&
mut
self
,
...
...
@@ -559,14 +572,18 @@ fn emit_enum_variant(&mut self,
cnt
:
uint
,
f
:
|
&
mut
PrettyEncoder
<
'a
>
|
->
EncodeResult
)
->
EncodeResult
{
if
cnt
==
0
{
write!
(
self
.writer
,
"{}"
,
escape_str
(
name
)
)
escape_str
(
self
.writer
,
name
)
}
else
{
self
.indent
+=
2
;
try!
(
write!
(
self
.writer
,
"[
\n
{}{},
\n
"
,
spaces
(
self
.indent
),
escape_str
(
name
)));
try!
(
write!
(
self
.writer
,
"[
\n
"
));
try!
(
spaces
(
self
.writer
,
self
.indent
));
try!
(
escape_str
(
self
.writer
,
name
));
try!
(
write!
(
self
.writer
,
",
\n
"
));
try!
(
f
(
self
));
self
.indent
-=
2
;
write!
(
self
.writer
,
"
\n
{}]"
,
spaces
(
self
.indent
))
try!
(
write!
(
self
.writer
,
"
\n
"
));
try!
(
spaces
(
self
.writer
,
self
.indent
));
write!
(
self
.writer
,
"]"
)
}
}
...
...
@@ -576,7 +593,7 @@ fn emit_enum_variant_arg(&mut self,
if
idx
!=
0
{
try!
(
write!
(
self
.writer
,
",
\n
"
));
}
try!
(
write!
(
self
.writer
,
"{}"
,
spaces
(
self
.indent
)
));
try!
(
spaces
(
self
.writer
,
self
.indent
));
f
(
self
)
}
...
...
@@ -607,7 +624,9 @@ fn emit_struct(&mut self,
self
.indent
+=
2
;
try!
(
f
(
self
));
self
.indent
-=
2
;
write!
(
self
.writer
,
"
\n
{}}}"
,
spaces
(
self
.indent
))
try!
(
write!
(
self
.writer
,
"
\n
"
));
try!
(
spaces
(
self
.writer
,
self
.indent
));
write!
(
self
.writer
,
"}}"
)
}
}
...
...
@@ -620,7 +639,9 @@ fn emit_struct_field(&mut self,
}
else
{
try!
(
write!
(
self
.writer
,
",
\n
"
));
}
try!
(
write!
(
self
.writer
,
"{}{}: "
,
spaces
(
self
.indent
),
escape_str
(
name
)));
try!
(
spaces
(
self
.writer
,
self
.indent
));
try!
(
escape_str
(
self
.writer
,
name
));
try!
(
write!
(
self
.writer
,
": "
));
f
(
self
)
}
...
...
@@ -665,7 +686,9 @@ fn emit_seq(&mut self,
self
.indent
+=
2
;
try!
(
f
(
self
));
self
.indent
-=
2
;
write!
(
self
.writer
,
"
\n
{}]"
,
spaces
(
self
.indent
))
try!
(
write!
(
self
.writer
,
"
\n
"
));
try!
(
spaces
(
self
.writer
,
self
.indent
));
write!
(
self
.writer
,
"]"
)
}
}
...
...
@@ -677,7 +700,7 @@ fn emit_seq_elt(&mut self,
}
else
{
try!
(
write!
(
self
.writer
,
",
\n
"
));
}
try!
(
write!
(
self
.writer
,
"{}"
,
spaces
(
self
.indent
)
));
try!
(
spaces
(
self
.writer
,
self
.indent
));
f
(
self
)
}
...
...
@@ -691,7 +714,9 @@ fn emit_map(&mut self,
self
.indent
+=
2
;
try!
(
f
(
self
));
self
.indent
-=
2
;
write!
(
self
.writer
,
"
\n
{}}}"
,
spaces
(
self
.indent
))
try!
(
write!
(
self
.writer
,
"
\n
"
));
try!
(
spaces
(
self
.writer
,
self
.indent
));
write!
(
self
.writer
,
"}}"
)
}
}
...
...
@@ -703,7 +728,7 @@ fn emit_map_elt_key(&mut self,
}
else
{
try!
(
write!
(
self
.writer
,
",
\n
"
));
}
try!
(
write!
(
self
.writer
,
"{}"
,
spaces
(
self
.indent
)
));
try!
(
spaces
(
self
.writer
,
self
.indent
));
// ref #12967, make sure to wrap a key in double quotes,
// in the event that its of a type that omits them (eg numbers)
let
mut
buf
=
MemWriter
::
new
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录