Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
db71ff3e
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,发现更多精彩内容 >>
提交
db71ff3e
编写于
8月 25, 2012
作者:
P
Patrick Walton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
libcore: Add some methods to make working with string slices easier
上级
80429dd7
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
56 addition
and
0 deletion
+56
-0
src/libcore/str.rs
src/libcore/str.rs
+56
-0
未找到文件。
src/libcore/str.rs
浏览文件 @
db71ff3e
...
...
@@ -32,6 +32,7 @@
push_char
,
pop_char
,
shift_char
,
view_shift_char
,
unshift_char
,
trim_left
,
trim_right
,
...
...
@@ -43,6 +44,7 @@
chars
,
substr
,
slice
,
view
,
split
,
splitn
,
split_nonempty
,
split_char
,
splitn_char
,
split_char_nonempty
,
split_str
,
split_str_nonempty
,
...
...
@@ -338,6 +340,22 @@ fn shift_char(&s: ~str) -> char {
return
ch
;
}
/**
* Removes the first character from a string slice and returns it. This does
* not allocate a new string; instead, it mutates a slice to point one
* character beyond the character that was shifted.
*
* # Failure
*
* If the string does not contain any characters
*/
#[inline]
fn
view_shift_char
(
s
:
&
a
/
str
)
->
(
char
,
&
a
/
str
)
{
let
{
ch
,
next
}
=
char_range_at
(
s
,
0u
);
let
next_s
=
unsafe
{
unsafe
::
view_bytes
(
s
,
next
,
len
(
s
))
};
return
(
ch
,
next_s
);
}
/// Prepend a char to a string
fn
unshift_char
(
&
s
:
~
str
,
ch
:
char
)
{
s
=
from_char
(
ch
)
+
s
;
}
...
...
@@ -423,6 +441,18 @@ fn shift_char(&s: ~str) -> char {
unsafe
{
unsafe
::
slice_bytes
(
s
,
begin
,
end
)
}
}
/**
* Returns a view of the given string from the byte range [`begin`..`end`)
*
* Fails when `begin` and `end` do not point to valid characters or beyond
* the last character of the string
*/
pure
fn
view
(
s
:
&
a
/
str
,
begin
:
uint
,
end
:
uint
)
->
&
a
/
str
{
assert
is_char_boundary
(
s
,
begin
);
assert
is_char_boundary
(
s
,
end
);
unsafe
{
unsafe
::
view_bytes
(
s
,
begin
,
end
)
}
}
/// Splits a string into substrings at each occurrence of a given character
pure
fn
split_char
(
s
:
&
str
,
sep
:
char
)
->
~
[
~
str
]
{
split_char_inner
(
s
,
sep
,
len
(
s
),
true
)
...
...
@@ -1773,6 +1803,7 @@ mod unsafe {
from_c_str_len
,
from_bytes
,
slice_bytes
,
view_bytes
,
push_byte
,
pop_byte
,
shift_byte
,
...
...
@@ -1857,6 +1888,27 @@ unsafe fn slice_bytes(s: &str, begin: uint, end: uint) -> ~str {
}
}
/**
* Takes a bytewise (not UTF-8) view from a string.
*
* Returns the substring from [`begin`..`end`).
*
* # Failure
*
* If begin is greater than end.
* If end is greater than the length of the string.
*/
#[inline]
unsafe
fn
view_bytes
(
s
:
&
str
,
begin
:
uint
,
end
:
uint
)
->
&
str
{
do
as_buf
(
s
)
|
sbuf
,
n
|
{
assert
(
begin
<=
end
);
assert
(
end
<=
n
);
let
tuple
=
(
ptr
::
offset
(
sbuf
,
begin
),
end
-
begin
+
1
);
::
unsafe
::
reinterpret_cast
(
tuple
)
}
}
/// Appends a byte to a string. (Not UTF-8 safe).
unsafe
fn
push_byte
(
&
s
:
~
str
,
b
:
u8
)
{
rustrt
::
rust_str_push
(
s
,
b
);
...
...
@@ -1958,6 +2010,7 @@ trait StrSlice {
fn
escape_default
()
->
~
str
;
fn
escape_unicode
()
->
~
str
;
pure
fn
to_unique
()
->
~
str
;
pure
fn
char_at
(
i
:
uint
)
->
char
;
}
/// Extension methods for strings
...
...
@@ -2067,6 +2120,9 @@ fn escape_unicode() -> ~str { escape_unicode(self) }
#[inline]
pure
fn
to_unique
()
->
~
str
{
self
.slice
(
0
,
self
.len
())
}
#[inline]
pure
fn
char_at
(
i
:
uint
)
->
char
{
char_at
(
self
,
i
)
}
}
#[cfg(test)]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录