Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
e74b55b9
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,发现更多精彩内容 >>
提交
e74b55b9
编写于
1月 27, 2017
作者:
M
Michał Krasnoborski
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use `String::with_capacity` in `format!`
上级
8430042a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
39 addition
and
1 deletion
+39
-1
src/libcollections/fmt.rs
src/libcollections/fmt.rs
+2
-1
src/libcore/fmt/mod.rs
src/libcore/fmt/mod.rs
+28
-0
src/libcoretest/fmt/mod.rs
src/libcoretest/fmt/mod.rs
+8
-0
src/libcoretest/lib.rs
src/libcoretest/lib.rs
+1
-0
未找到文件。
src/libcollections/fmt.rs
浏览文件 @
e74b55b9
...
...
@@ -539,7 +539,8 @@
/// [format!]: ../macro.format.html
#[stable(feature
=
"rust1"
,
since
=
"1.0.0"
)]
pub
fn
format
(
args
:
Arguments
)
->
string
::
String
{
let
mut
output
=
string
::
String
::
new
();
let
capacity
=
args
.estimated_capacity
();
let
mut
output
=
string
::
String
::
with_capacity
(
capacity
);
let
_
=
output
.write_fmt
(
args
);
output
}
src/libcore/fmt/mod.rs
浏览文件 @
e74b55b9
...
...
@@ -265,6 +265,34 @@ pub fn new_v1_formatted(pieces: &'a [&'a str],
args
:
args
}
}
/// Estimates the length of the formatted text.
///
/// This is intended to be used for setting initial `String` capacity
/// when using `format!`. Note: this is neither the lower nor upper bound.
#[doc(hidden)]
#[inline]
#[unstable(feature
=
"fmt_internals"
,
reason
=
"internal to format_args!"
,
issue
=
"0"
)]
pub
fn
estimated_capacity
(
&
self
)
->
usize
{
// Using wrapping arithmetics in this function, because
// wrong result is highly unlikely and doesn't cause unsafety.
use
::
num
::
Wrapping
as
W
;
let
pieces_length
:
W
<
usize
>
=
self
.pieces
.iter
()
.map
(|
x
|
W
(
x
.len
()))
.sum
();
// If they are any arguments to format, the string will most likely
// double in size. So we're pre-doubling it here.
let
multiplier
=
if
self
.args
.is_empty
()
{
W
(
1
)
}
else
{
W
(
2
)
};
let
capacity
=
multiplier
*
pieces_length
;
if
multiplier
==
W
(
2
)
&&
(
W
(
1
)
..
W
(
8
))
.contains
(
capacity
)
{
// Allocations smaller than 8 don't really make sense for String.
8
}
else
{
capacity
.0
}
}
}
/// This structure represents a safely precompiled version of a format string
...
...
src/libcoretest/fmt/mod.rs
浏览文件 @
e74b55b9
...
...
@@ -28,3 +28,11 @@ fn test_pointer_formats_data_pointer() {
assert_eq!
(
format!
(
"{:p}"
,
s
),
format!
(
"{:p}"
,
s
.as_ptr
()));
assert_eq!
(
format!
(
"{:p}"
,
b
),
format!
(
"{:p}"
,
b
.as_ptr
()));
}
#[test]
fn
test_estimated_capacity
()
{
assert_eq!
(
format_args!
(
"{}"
,
""
)
.estimated_capacity
(),
0
);
assert_eq!
(
format_args!
(
"Hello"
)
.estimated_capacity
(),
5
);
assert_eq!
(
format_args!
(
"Hello, {}!"
,
""
)
.estimated_capacity
(),
16
);
assert_eq!
(
format_args!
(
"{}, hello!"
,
"World"
)
.estimated_capacity
(),
16
);
}
src/libcoretest/lib.rs
浏览文件 @
e74b55b9
...
...
@@ -34,6 +34,7 @@
#![feature(ordering_chaining)]
#![feature(result_unwrap_or_default)]
#![feature(ptr_unaligned)]
#![feature(fmt_internals)]
extern
crate
core
;
extern
crate
test
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录