Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
4c0aea69
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,发现更多精彩内容 >>
提交
4c0aea69
编写于
4月 17, 2011
作者:
B
Brian Anderson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support #fmt precision for unsigned types
上级
c7edcb3a
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
65 addition
and
27 deletion
+65
-27
src/lib/ExtFmt.rs
src/lib/ExtFmt.rs
+49
-11
src/test/run-pass/syntax-extension-fmt.rs
src/test/run-pass/syntax-extension-fmt.rs
+16
-16
未找到文件。
src/lib/ExtFmt.rs
浏览文件 @
4c0aea69
...
...
@@ -336,19 +336,55 @@ fn conv_int(&conv cv, int i) -> str {
}
fn
conv_uint
(
&
conv
cv
,
uint
u
)
->
str
{
// Convert a uint to string with a minimum number of digits. If
// precision is 0 and num is 0 then the result is the empty
// string. Could move this to _str, but it doesn't seem all that
// useful.
fn
uint_to_str_prec
(
uint
num
,
uint
radix
,
uint
prec
)
->
str
{
auto
s
;
if
(
prec
==
0u
&&
num
==
0u
)
{
s
=
""
;
}
else
{
s
=
_u
int
.to_str
(
num
,
radix
);
auto
len
=
_
str
.char_len
(
s
);
if
(
len
<
prec
)
{
auto
diff
=
prec
-
len
;
auto
pad
=
str_init_elt
(
'0'
,
diff
);
s
=
pad
+
s
;
}
}
ret
s
;
}
fn
get_precision
(
&
conv
cv
)
->
uint
{
alt
(
cv
.precision
)
{
case
(
count_is
(
?
c
))
{
ret
c
as
uint
;
}
case
(
count_implied
)
{
ret
1u
;
}
}
}
auto
prec
=
get_precision
(
cv
);
auto
res
;
alt
(
cv
.ty
)
{
case
(
ty_default
)
{
res
=
_u
int
.to_str
(
u
,
10u
);
res
=
uint_to_str_prec
(
u
,
10u
,
prec
);
}
case
(
ty_hex_lower
)
{
res
=
_u
int
.to_str
(
u
,
16u
);
res
=
uint_to_str_prec
(
u
,
16u
,
prec
);
}
case
(
ty_hex_upper
)
{
res
=
_
str
.to_upper
(
_u
int
.to_str
(
u
,
16u
));
res
=
_
str
.to_upper
(
uint_to_str_prec
(
u
,
16u
,
prec
));
}
case
(
ty_bits
)
{
res
=
_u
int
.to_str
(
u
,
2u
);
res
=
uint_to_str_prec
(
u
,
2u
,
prec
);
}
}
ret
pad
(
cv
,
res
);
...
...
@@ -382,6 +418,14 @@ fn conv_str(&conv cv, str s) -> str {
ret
pad
(
cv
,
unpadded
);
}
// FIXME: This might be useful in _str, but needs to be utf8 safe first
fn
str_init_elt
(
char
c
,
uint
n_elts
)
->
str
{
auto
svec
=
_
vec
.init_elt
[
u8
](
c
as
u8
,
n_elts
);
// FIXME: Using unsafe_from_bytes because rustboot
// can't figure out the is_utf8 predicate on from_bytes?
ret
_
str
.unsafe_from_bytes
(
svec
);
}
fn
pad
(
&
conv
cv
,
str
s
)
->
str
{
alt
(
cv
.width
)
{
case
(
count_implied
)
{
...
...
@@ -393,13 +437,7 @@ fn pad(&conv cv, str s) -> str {
auto
strlen
=
_
str
.char_len
(
s
);
if
(
strlen
<
uwidth
)
{
auto
diff
=
uwidth
-
strlen
;
// FIXME: Probably should be a _str fn for
// initializing from n chars
auto
padvec
=
_
vec
.init_elt
[
u8
](
' '
as
u8
,
diff
);
// FIXME: Using unsafe_from_bytes because rustboot
// can't figure out the is_utf8 predicate on from_bytes?
auto
padstr
=
_
str
.unsafe_from_bytes
(
padvec
);
auto
padstr
=
str_init_elt
(
' '
,
diff
);
if
(
have_flag
(
cv
.flags
,
flag_left_justify
))
{
ret
s
+
padstr
;
}
else
{
...
...
src/test/run-pass/syntax-extension-fmt.rs
浏览文件 @
4c0aea69
...
...
@@ -56,38 +56,38 @@ fn main() {
// Precision
// test(#fmt("%.d", 0), "");
//
test(#fmt("%.u", 0u), "");
//
test(#fmt("%.x", 0u), "");
test
(
#
fmt
(
"%.u"
,
0u
),
""
);
test
(
#
fmt
(
"%.x"
,
0u
),
""
);
// test(#fmt("%.d", 10), "10");
// test(#fmt("%.d", -10), "-10");
//
test(#fmt("%.u", 10u), "10");
test
(
#
fmt
(
"%.u"
,
10u
),
"10"
);
test
(
#
fmt
(
"%.s"
,
"test"
),
""
);
//
test(#fmt("%.x", 127u), "7f");
test
(
#
fmt
(
"%.x"
,
127u
),
"7f"
);
// test(#fmt("%.0d", 0), "");
//
test(#fmt("%.0u", 0u), "");
//
test(#fmt("%.0x", 0u), "");
test
(
#
fmt
(
"%.0u"
,
0u
),
""
);
test
(
#
fmt
(
"%.0x"
,
0u
),
""
);
// test(#fmt("%.0d", 10), "10");
// test(#fmt("%.0d", -10), "-10");
//
test(#fmt("%.0u", 10u), "10");
test
(
#
fmt
(
"%.0u"
,
10u
),
"10"
);
test
(
#
fmt
(
"%.0s"
,
"test"
),
""
);
//
test(#fmt("%.0x", 127u), "7f");
test
(
#
fmt
(
"%.0x"
,
127u
),
"7f"
);
// test(#fmt("%.1d", 0), "0");
//
test(#fmt("%.1u", 0u), "0");
//
test(#fmt("%.1x", 0u), "0");
test
(
#
fmt
(
"%.1u"
,
0u
),
"0"
);
test
(
#
fmt
(
"%.1x"
,
0u
),
"0"
);
// test(#fmt("%.1d", 10), "10");
// test(#fmt("%.1d", -10), "-10");
//
test(#fmt("%.1u", 10u), "10");
test
(
#
fmt
(
"%.1u"
,
10u
),
"10"
);
test
(
#
fmt
(
"%.1s"
,
"test"
),
"t"
);
//
test(#fmt("%.1x", 127u), "7f");
test
(
#
fmt
(
"%.1x"
,
127u
),
"7f"
);
// test(#fmt("%.5d", 0), "00000");
//
test(#fmt("%.5u", 0u), "00000");
//
test(#fmt("%.5x", 0u), "00000");
test
(
#
fmt
(
"%.5u"
,
0u
),
"00000"
);
test
(
#
fmt
(
"%.5x"
,
0u
),
"00000"
);
// test(#fmt("%.5d", 10), "00010");
// test(#fmt("%.5d", -10), "-00010");
//
test(#fmt("%.5u", 10u), "00010");
test
(
#
fmt
(
"%.5u"
,
10u
),
"00010"
);
test
(
#
fmt
(
"%.5s"
,
"test"
),
"test"
);
//
test(#fmt("%.5x", 127u), "0007f");
test
(
#
fmt
(
"%.5x"
,
127u
),
"0007f"
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录