Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
aad24616
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,发现更多精彩内容 >>
提交
aad24616
编写于
10月 31, 2014
作者:
B
Brian Anderson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
core: Convert Char::escape_default, escape_unicode to iterators
[breaking-change]
上级
ca1820b1
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
110 addition
and
43 deletion
+110
-43
src/libcollections/str.rs
src/libcollections/str.rs
+6
-2
src/libcore/char.rs
src/libcore/char.rs
+95
-34
src/libgraphviz/lib.rs
src/libgraphviz/lib.rs
+1
-1
src/librustc_trans/back/link.rs
src/librustc_trans/back/link.rs
+1
-1
src/librustdoc/clean/mod.rs
src/librustdoc/clean/mod.rs
+2
-2
src/libsyntax/parse/lexer/mod.rs
src/libsyntax/parse/lexer/mod.rs
+2
-2
src/libsyntax/print/pprust.rs
src/libsyntax/print/pprust.rs
+3
-1
未找到文件。
src/libcollections/str.rs
浏览文件 @
aad24616
...
...
@@ -630,7 +630,9 @@ fn escape_default(&self) -> String {
let
me
=
self
.as_slice
();
let
mut
out
=
String
::
with_capacity
(
me
.len
());
for
c
in
me
.chars
()
{
c
.escape_default
(|
c
|
out
.push
(
c
));
for
c
in
c
.escape_default
()
{
out
.push
(
c
);
}
}
out
}
...
...
@@ -640,7 +642,9 @@ fn escape_unicode(&self) -> String {
let
me
=
self
.as_slice
();
let
mut
out
=
String
::
with_capacity
(
me
.len
());
for
c
in
me
.chars
()
{
c
.escape_unicode
(|
c
|
out
.push
(
c
));
for
c
in
c
.escape_unicode
()
{
out
.push
(
c
);
}
}
out
}
...
...
src/libcore/char.rs
浏览文件 @
aad24616
...
...
@@ -17,7 +17,7 @@
use
mem
::
transmute
;
use
option
::{
None
,
Option
,
Some
};
use
iter
::
range_step
;
use
iter
::
{
range_step
,
Iterator
,
RangeStep
}
;
use
slice
::
SlicePrelude
;
// UTF-8 ranges and tags for encoding characters
...
...
@@ -165,7 +165,9 @@ pub fn from_digit(num: uint, radix: uint) -> Option<char> {
///
#[deprecated
=
"use the Char::escape_unicode method"
]
pub
fn
escape_unicode
(
c
:
char
,
f
:
|
char
|)
{
c
.escape_unicode
(
f
)
for
char
in
c
.escape_unicode
()
{
f
(
char
);
}
}
///
...
...
@@ -182,7 +184,9 @@ pub fn escape_unicode(c: char, f: |char|) {
///
#[deprecated
=
"use the Char::escape_default method"
]
pub
fn
escape_default
(
c
:
char
,
f
:
|
char
|)
{
c
.escape_default
(
f
)
for
c
in
c
.escape_default
()
{
f
(
c
);
}
}
/// Returns the amount of bytes this `char` would need if encoded in UTF-8
...
...
@@ -266,7 +270,7 @@ pub trait Char {
/// * Characters in [0x100,0xffff] get 4-digit escapes: `\\uNNNN`.
/// * Characters above 0x10000 get 8-digit escapes: `\\UNNNNNNNN`.
#[unstable
=
"pending error conventions, trait organization"
]
fn
escape_unicode
(
self
,
f
:
|
char
|)
;
fn
escape_unicode
(
self
)
->
UnicodeEscapedChars
;
/// Returns a 'default' ASCII and C++11-like literal escape of a
/// character.
...
...
@@ -281,7 +285,7 @@ pub trait Char {
/// * Any other chars in the range [0x20,0x7e] are not escaped.
/// * Any other chars are given hex Unicode escapes; see `escape_unicode`.
#[unstable
=
"pending error conventions, trait organization"
]
fn
escape_default
(
self
,
f
:
|
char
|)
;
fn
escape_default
(
self
)
->
DefaultEscapedChars
;
/// Returns the amount of bytes this character would need if encoded in
/// UTF-8.
...
...
@@ -351,38 +355,23 @@ fn from_digit(num: uint, radix: uint) -> Option<char> { from_digit(num, radix) }
fn
from_u32
(
i
:
u32
)
->
Option
<
char
>
{
from_u32
(
i
)
}
#[unstable
=
"pending error conventions, trait organization"
]
fn
escape_unicode
(
self
,
f
:
|
char
|)
{
// avoid calling str::to_str_radix because we don't really need to allocate
// here.
f
(
'\\'
);
let
pad
=
match
()
{
_
if
self
<=
'\xff'
=>
{
f
(
'x'
);
2
}
_
if
self
<=
'\uffff'
=>
{
f
(
'u'
);
4
}
_
=>
{
f
(
'U'
);
8
}
};
for
offset
in
range_step
::
<
i32
>
(
4
*
(
pad
-
1
),
-
1
,
-
4
)
{
let
offset
=
offset
as
uint
;
unsafe
{
match
((
self
as
i32
)
>>
offset
)
&
0xf
{
i
@
0
...
9
=>
{
f
(
transmute
(
'0'
as
i32
+
i
));
}
i
=>
{
f
(
transmute
(
'a'
as
i32
+
(
i
-
10
)));
}
}
}
}
fn
escape_unicode
(
self
)
->
UnicodeEscapedChars
{
UnicodeEscapedChars
{
c
:
self
,
state
:
UnicodeEscapedCharsState
::
Backslash
}
}
#[unstable
=
"pending error conventions, trait organization"
]
fn
escape_default
(
self
,
f
:
|
char
|)
{
match
self
{
'\t'
=>
{
f
(
'\\'
);
f
(
't'
);
}
'\r'
=>
{
f
(
'\\'
);
f
(
'r'
);
}
'\n'
=>
{
f
(
'\\'
);
f
(
'n'
);
}
'\\'
=>
{
f
(
'\\'
);
f
(
'\\'
);
}
'\''
=>
{
f
(
'\\'
);
f
(
'\''
);
}
'"'
=>
{
f
(
'\\'
);
f
(
'"'
);
}
'\x20'
...
'\x7e'
=>
{
f
(
self
);
}
_
=>
self
.escape_unicode
(
f
),
}
fn
escape_default
(
self
)
->
DefaultEscapedChars
{
let
init_state
=
match
self
{
'\t'
=>
DefaultEscapedCharsState
::
Backslash
(
't'
),
'\r'
=>
DefaultEscapedCharsState
::
Backslash
(
'r'
),
'\n'
=>
DefaultEscapedCharsState
::
Backslash
(
'n'
),
'\\'
=>
DefaultEscapedCharsState
::
Backslash
(
'\\'
),
'\''
=>
DefaultEscapedCharsState
::
Backslash
(
'\''
),
'"'
=>
DefaultEscapedCharsState
::
Backslash
(
'"'
),
'\x20'
...
'\x7e'
=>
DefaultEscapedCharsState
::
Char
(
self
),
_
=>
DefaultEscapedCharsState
::
Unicode
(
self
.escape_unicode
())
};
DefaultEscapedChars
{
state
:
init_state
}
}
#[inline]
...
...
@@ -456,3 +445,75 @@ fn encode_utf16(&self, dst: &mut [u16]) -> Option<uint> {
}
}
}
/// An iterator over the characters that represent a `char`, as escaped by
/// Rust's unicode escaping rules.
pub
struct
UnicodeEscapedChars
{
c
:
char
,
state
:
UnicodeEscapedCharsState
}
enum
UnicodeEscapedCharsState
{
Backslash
,
Type
,
Value
(
RangeStep
<
i32
>
),
}
impl
Iterator
<
char
>
for
UnicodeEscapedChars
{
fn
next
(
&
mut
self
)
->
Option
<
char
>
{
match
self
.state
{
UnicodeEscapedCharsState
::
Backslash
=>
{
self
.state
=
UnicodeEscapedCharsState
::
Type
;
Some
(
'\\'
)
}
UnicodeEscapedCharsState
::
Type
=>
{
let
(
typechar
,
pad
)
=
if
self
.c
<=
'\x7f'
{
(
'x'
,
2
)
}
else
if
self
.c
<=
'\uffff'
{
(
'u'
,
4
)
}
else
{
(
'U'
,
8
)
};
self
.state
=
UnicodeEscapedCharsState
::
Value
(
range_step
(
4
*
(
pad
-
1
),
-
1
,
-
4i32
));
Some
(
typechar
)
}
UnicodeEscapedCharsState
::
Value
(
ref
mut
range_step
)
=>
match
range_step
.next
()
{
Some
(
offset
)
=>
{
let
offset
=
offset
as
uint
;
let
v
=
match
((
self
.c
as
i32
)
>>
offset
)
&
0xf
{
i
@
0
...
9
=>
'0'
as
i32
+
i
,
i
=>
'a'
as
i32
+
(
i
-
10
)
};
Some
(
unsafe
{
transmute
(
v
)
})
}
None
=>
None
}
}
}
}
/// An iterator over the characters that represent a `char`, escaped
/// for maximum portability.
pub
struct
DefaultEscapedChars
{
state
:
DefaultEscapedCharsState
}
enum
DefaultEscapedCharsState
{
Backslash
(
char
),
Char
(
char
),
Done
,
Unicode
(
UnicodeEscapedChars
),
}
impl
Iterator
<
char
>
for
DefaultEscapedChars
{
fn
next
(
&
mut
self
)
->
Option
<
char
>
{
match
self
.state
{
DefaultEscapedCharsState
::
Backslash
(
c
)
=>
{
self
.state
=
DefaultEscapedCharsState
::
Char
(
c
);
Some
(
'\\'
)
}
DefaultEscapedCharsState
::
Char
(
c
)
=>
{
self
.state
=
DefaultEscapedCharsState
::
Done
;
Some
(
c
)
}
DefaultEscapedCharsState
::
Done
=>
None
,
DefaultEscapedCharsState
::
Unicode
(
ref
mut
iter
)
=>
iter
.next
()
}
}
}
src/libgraphviz/lib.rs
浏览文件 @
aad24616
...
...
@@ -431,7 +431,7 @@ fn escape_char(c: char, f: |char|) {
// not escaping \\, since Graphviz escString needs to
// interpret backslashes; see EscStr above.
'\\'
=>
f
(
c
),
_
=>
c
.escape_default
(
f
)
_
=>
for
c
in
c
.escape_default
()
{
f
(
c
)
}
}
}
fn
escape_str
(
s
:
&
str
)
->
String
{
...
...
src/librustc_trans/back/link.rs
浏览文件 @
aad24616
...
...
@@ -262,7 +262,7 @@ pub fn sanitize(s: &str) -> String {
_
=>
{
let
mut
tstr
=
String
::
new
();
char
::
escape_unicode
(
c
,
|
c
|
tstr
.push
(
c
));
for
c
in
c
.escape_unicode
()
{
tstr
.push
(
c
)
}
result
.push
(
'$'
);
result
.push_str
(
tstr
.as_slice
()
.slice_from
(
1
));
}
...
...
src/librustdoc/clean/mod.rs
浏览文件 @
aad24616
...
...
@@ -2033,9 +2033,9 @@ fn lit_to_string(lit: &ast::Lit) -> String {
ast
::
LitBinary
(
ref
data
)
=>
format!
(
"{}"
,
data
),
ast
::
LitByte
(
b
)
=>
{
let
mut
res
=
String
::
from_str
(
"b'"
);
(
b
as
char
)
.escape_default
(|
c
|
{
for
c
in
(
b
as
char
)
.escape_default
()
{
res
.push
(
c
);
}
);
}
res
.push
(
'\''
);
res
},
...
...
src/libsyntax/parse/lexer/mod.rs
浏览文件 @
aad24616
...
...
@@ -193,7 +193,7 @@ fn err_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) {
fn
fatal_span_char
(
&
self
,
from_pos
:
BytePos
,
to_pos
:
BytePos
,
m
:
&
str
,
c
:
char
)
->
!
{
let
mut
m
=
m
.to_string
();
m
.push_str
(
": "
);
char
::
escape_default
(
c
,
|
c
|
m
.push
(
c
));
for
c
in
c
.escape_default
()
{
m
.push
(
c
)
}
self
.fatal_span_
(
from_pos
,
to_pos
,
m
.as_slice
());
}
...
...
@@ -202,7 +202,7 @@ fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char)
fn
err_span_char
(
&
self
,
from_pos
:
BytePos
,
to_pos
:
BytePos
,
m
:
&
str
,
c
:
char
)
{
let
mut
m
=
m
.to_string
();
m
.push_str
(
": "
);
char
::
escape_default
(
c
,
|
c
|
m
.push
(
c
));
for
c
in
c
.escape_default
()
{
m
.push
(
c
)
}
self
.err_span_
(
from_pos
,
to_pos
,
m
.as_slice
());
}
...
...
src/libsyntax/print/pprust.rs
浏览文件 @
aad24616
...
...
@@ -2756,7 +2756,9 @@ pub fn print_literal(&mut self, lit: &ast::Lit) -> IoResult<()> {
}
ast
::
LitChar
(
ch
)
=>
{
let
mut
res
=
String
::
from_str
(
"'"
);
ch
.escape_default
(|
c
|
res
.push
(
c
));
for
c
in
ch
.escape_default
()
{
res
.push
(
c
);
}
res
.push
(
'\''
);
word
(
&
mut
self
.s
,
res
.as_slice
())
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录