Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
3629f702
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,发现更多精彩内容 >>
提交
3629f702
编写于
8月 04, 2013
作者:
E
Erick Tryzelaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
std: merge str::raw::from_buf and str::raw::from_c_str
上级
bd908d4c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
27 addition
and
32 deletion
+27
-32
src/libstd/os.rs
src/libstd/os.rs
+1
-1
src/libstd/str.rs
src/libstd/str.rs
+12
-22
src/test/run-pass/const-str-ptr.rs
src/test/run-pass/const-str-ptr.rs
+14
-9
未找到文件。
src/libstd/os.rs
浏览文件 @
3629f702
...
@@ -93,7 +93,7 @@ pub fn fill_charp_buf(f: &fn(*mut c_char, size_t) -> bool) -> Option<~str> {
...
@@ -93,7 +93,7 @@ pub fn fill_charp_buf(f: &fn(*mut c_char, size_t) -> bool) -> Option<~str> {
do
buf
.as_mut_buf
|
b
,
sz
|
{
do
buf
.as_mut_buf
|
b
,
sz
|
{
if
f
(
b
,
sz
as
size_t
)
{
if
f
(
b
,
sz
as
size_t
)
{
unsafe
{
unsafe
{
Some
(
str
::
raw
::
from_
buf
(
b
as
*
u8
))
Some
(
str
::
raw
::
from_
c_str
(
b
as
*
c_char
))
}
}
}
else
{
}
else
{
None
None
...
...
src/libstd/str.rs
浏览文件 @
3629f702
...
@@ -763,17 +763,6 @@ pub mod raw {
...
@@ -763,17 +763,6 @@ pub mod raw {
use
vec
::
MutableVector
;
use
vec
::
MutableVector
;
use
unstable
::
raw
::{
Slice
,
String
};
use
unstable
::
raw
::{
Slice
,
String
};
/// Create a Rust string from a null-terminated *u8 buffer
pub
unsafe
fn
from_buf
(
buf
:
*
u8
)
->
~
str
{
let
mut
curr
=
buf
;
let
mut
i
=
0u
;
while
*
curr
!=
0u8
{
i
+=
1u
;
curr
=
ptr
::
offset
(
buf
,
i
as
int
);
}
return
from_buf_len
(
buf
,
i
);
}
/// Create a Rust string from a *u8 buffer of the given length
/// Create a Rust string from a *u8 buffer of the given length
pub
unsafe
fn
from_buf_len
(
buf
:
*
u8
,
len
:
uint
)
->
~
str
{
pub
unsafe
fn
from_buf_len
(
buf
:
*
u8
,
len
:
uint
)
->
~
str
{
let
mut
v
:
~
[
u8
]
=
vec
::
with_capacity
(
len
+
1
);
let
mut
v
:
~
[
u8
]
=
vec
::
with_capacity
(
len
+
1
);
...
@@ -784,17 +773,18 @@ pub unsafe fn from_buf_len(buf: *u8, len: uint) -> ~str {
...
@@ -784,17 +773,18 @@ pub unsafe fn from_buf_len(buf: *u8, len: uint) -> ~str {
v
.push
(
0u8
);
v
.push
(
0u8
);
assert
!
(
is_utf8
(
v
));
assert
!
(
is_utf8
(
v
));
return
cast
::
transmute
(
v
);
cast
::
transmute
(
v
)
}
}
/// Create a Rust string from a null-terminated C string
/// Create a Rust string from a null-terminated C string
pub
unsafe
fn
from_c_str
(
c_str
:
*
libc
::
c_char
)
->
~
str
{
pub
unsafe
fn
from_c_str
(
buf
:
*
libc
::
c_char
)
->
~
str
{
from_buf
(
c_str
as
*
u8
)
let
mut
curr
=
buf
;
}
let
mut
i
=
0
;
while
*
curr
!=
0
{
/// Create a Rust string from a `*c_char` buffer of the given length
i
+=
1
;
pub
unsafe
fn
from_c_str_len
(
c_str
:
*
libc
::
c_char
,
len
:
uint
)
->
~
str
{
curr
=
ptr
::
offset
(
buf
,
i
);
from_buf_len
(
c_str
as
*
u8
,
len
)
}
from_buf_len
(
buf
as
*
u8
,
i
as
uint
)
}
}
/// Converts a vector of bytes to a new owned string.
/// Converts a vector of bytes to a new owned string.
...
@@ -2805,11 +2795,11 @@ fn test_from_bytes_fail() {
...
@@ -2805,11 +2795,11 @@ fn test_from_bytes_fail() {
}
}
#[test]
#[test]
fn
test_
from_buf
()
{
fn
test_
raw_from_c_str
()
{
unsafe
{
unsafe
{
let
a
=
~
[
65
u8
,
65u8
,
65u8
,
65u8
,
65u8
,
65u8
,
65u8
,
0u8
];
let
a
=
~
[
65
,
65
,
65
,
65
,
65
,
65
,
65
,
0
];
let
b
=
vec
::
raw
::
to_ptr
(
a
);
let
b
=
vec
::
raw
::
to_ptr
(
a
);
let
c
=
raw
::
from_
buf
(
b
);
let
c
=
raw
::
from_
c_str
(
b
);
assert_eq!
(
c
,
~
"AAAAAAA"
);
assert_eq!
(
c
,
~
"AAAAAAA"
);
}
}
}
}
...
...
src/test/run-pass/const-str-ptr.rs
浏览文件 @
3629f702
...
@@ -10,15 +10,20 @@
...
@@ -10,15 +10,20 @@
use
std
::
str
;
use
std
::
str
;
static
a
:
[
u8
,
..
3
]
=
[
'h'
as
u8
,
'i'
as
u8
,
0
as
u8
];
static
A
:
[
u8
,
..
2
]
=
[
'h'
as
u8
,
'i'
as
u8
];
static
c
:
&
'static
[
u8
,
..
3
]
=
&
a
;
static
B
:
&
'static
[
u8
,
..
2
]
=
&
A
;
static
b
:
*
u8
=
c
as
*
u8
;
static
C
:
*
u8
=
B
as
*
u8
;
pub
fn
main
()
{
pub
fn
main
()
{
let
foo
=
&
a
as
*
u8
;
unsafe
{
assert_eq!
(
unsafe
{
str
::
raw
::
from_bytes
(
a
)
},
~
"hi
\x00
"
);
let
foo
=
&
A
as
*
u8
;
assert_eq!
(
unsafe
{
str
::
raw
::
from_buf
(
foo
)
},
~
"hi"
);
assert_eq!
(
str
::
raw
::
from_bytes
(
A
),
~
"hi"
);
assert_eq!
(
unsafe
{
str
::
raw
::
from_buf
(
b
)
},
~
"hi"
);
assert_eq!
(
str
::
raw
::
from_buf_len
(
foo
,
A
.len
()),
~
"hi"
);
assert
!
(
unsafe
{
*
b
==
a
[
0
]
});
assert_eq!
(
str
::
raw
::
from_buf_len
(
C
,
B
.len
()),
~
"hi"
);
assert
!
(
unsafe
{
*
(
&
c
[
0
]
as
*
u8
)
==
a
[
0
]
});
assert
!
(
*
C
==
A
[
0
]);
assert
!
(
*
(
&
B
[
0
]
as
*
u8
)
==
A
[
0
]);
let
bar
=
str
::
raw
::
from_bytes
(
A
)
.to_c_str
();
assert_eq!
(
bar
.with_ref
(|
buf
|
str
::
raw
::
from_c_str
(
buf
)),
~
"hi"
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录