Skip to content

Commit 03b8c8d

Browse files
killerswanbrson
authored andcommitted
Rename str::from_byte(s) to str::unsafe::from_byte(s),
mark them as unsafe, make comp/driver/driver.rs use str::from_bytes...
1 parent 24668d7 commit 03b8c8d

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

src/comp/driver/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn parse_input(sess: session, cfg: ast::crate_cfg, input: str)
8080
if !input_is_stdin(input) {
8181
parser::parse_crate_from_file(input, cfg, sess.parse_sess)
8282
} else {
83-
let src = @str::unsafe_from_bytes(io::stdin().read_whole_stream());
83+
let src = @str::from_bytes(io::stdin().read_whole_stream());
8484
parser::parse_crate_from_source_str(input, src, cfg, sess.parse_sess)
8585
}
8686
}

src/libcore/str.rs

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ For some heavy-duty uses, we recommend trying std::rope.
1212
export
1313
// Creating a string
1414
from_bytes,
15-
unsafe_from_bytes,
1615
from_byte,
17-
unsafe_from_byte,
1816
//push_utf8_bytes,
1917
from_char,
2018
from_chars,
@@ -120,37 +118,11 @@ Function: from_bytes
120118
121119
Convert a vector of bytes to a UTF-8 string. Fails if invalid UTF-8.
122120
*/
123-
fn from_bytes(vv: [u8]) -> str {
121+
fn from_bytes(vv: [u8]) -> str unsafe {
124122
assert is_utf8(vv);
125-
ret unsafe_from_bytes(vv);
123+
ret unsafe::from_bytes(vv);
126124
}
127125

128-
/*
129-
Function: unsafe_from_bytes
130-
131-
Converts a vector of bytes to a string. Does not verify that the
132-
vector contains valid UTF-8.
133-
134-
FIXME: stop exporting
135-
*/
136-
fn unsafe_from_bytes(v: [const u8]) -> str unsafe {
137-
let vcopy: [u8] = v + [0u8];
138-
let scopy: str = unsafe::reinterpret_cast(vcopy);
139-
unsafe::leak(vcopy);
140-
ret scopy;
141-
}
142-
143-
/*
144-
Function: unsafe_from_byte
145-
146-
Converts a byte to a string. Does not verify that the byte is
147-
valid UTF-8.
148-
149-
FIXME: stop exporting
150-
*/
151-
fn unsafe_from_byte(u: u8) -> str { unsafe_from_bytes([u]) }
152-
153-
154126
/*
155127
Function: from_byte
156128
@@ -211,6 +183,7 @@ fn from_chars(chs: [char]) -> str {
211183
ret buf;
212184
}
213185

186+
// FIXME: not unsafe now
214187
/*
215188
Function: from_cstr
216189
@@ -413,9 +386,9 @@ Converts a string to a vector of bytes. The result vector is not
413386
null-terminated.
414387
*/
415388
fn bytes(s: str) -> [u8] unsafe {
416-
let v = unsafe::reinterpret_cast(s);
389+
let v = ::unsafe::reinterpret_cast(s);
417390
let vcopy = vec::slice(v, 0u, vec::len(v) - 1u);
418-
unsafe::leak(v);
391+
::unsafe::leak(v);
419392
ret vcopy;
420393
}
421394

@@ -492,12 +465,12 @@ fn slice(s: str, begin: uint, end: uint) -> str unsafe {
492465
assert (begin <= end);
493466
assert (end <= byte_len(s));
494467

495-
let v: [u8] = unsafe::reinterpret_cast(s);
468+
let v: [u8] = ::unsafe::reinterpret_cast(s);
496469
let v2 = vec::slice(v, begin, end);
497-
unsafe::leak(v);
470+
::unsafe::leak(v);
498471
v2 += [0u8];
499-
let s2: str = unsafe::reinterpret_cast(v2);
500-
unsafe::leak(v2);
472+
let s2: str = ::unsafe::reinterpret_cast(v2);
473+
::unsafe::leak(v2);
501474
ret s2;
502475
}
503476

@@ -1093,9 +1066,9 @@ Returns the length in bytes of a string
10931066
FIXME: rename to 'len_bytes'?
10941067
*/
10951068
pure fn byte_len(s: str) -> uint unsafe {
1096-
let v: [u8] = unsafe::reinterpret_cast(s);
1069+
let v: [u8] = ::unsafe::reinterpret_cast(s);
10971070
let vlen = vec::len(v);
1098-
unsafe::leak(v);
1071+
::unsafe::leak(v);
10991072
// There should always be a null terminator
11001073
assert (vlen > 0u);
11011074
ret vlen - 1u;
@@ -1371,7 +1344,7 @@ const tag_six_b: uint = 252u;
13711344
// no guarantee that the string is rooted). Instead, use as_buf below.
13721345
unsafe fn buf(s: str) -> sbuf {
13731346
let saddr = ptr::addr_of(s);
1374-
let vaddr: *[u8] = unsafe::reinterpret_cast(saddr);
1347+
let vaddr: *[u8] = ::unsafe::reinterpret_cast(saddr);
13751348
let buf = vec::to_ptr(*vaddr);
13761349
ret buf;
13771350
}
@@ -1398,6 +1371,38 @@ An unsafe buffer of bytes. Corresponds to a C char pointer.
13981371
*/
13991372
type sbuf = *u8;
14001373

1374+
mod unsafe {
1375+
export
1376+
// UNSAFE
1377+
from_bytes,
1378+
from_byte;
1379+
1380+
/*
1381+
Function: unsafe::from_bytes
1382+
1383+
Converts a vector of bytes to a string. Does not verify that the
1384+
vector contains valid UTF-8.
1385+
1386+
FIXME: stop exporting
1387+
*/
1388+
unsafe fn from_bytes(v: [const u8]) -> str unsafe {
1389+
let vcopy: [u8] = v + [0u8];
1390+
let scopy: str = ::unsafe::reinterpret_cast(vcopy);
1391+
::unsafe::leak(vcopy);
1392+
ret scopy;
1393+
}
1394+
1395+
/*
1396+
Function: unsafe::from_byte
1397+
1398+
Converts a byte to a string. Does not verify that the byte is
1399+
valid UTF-8.
1400+
1401+
FIXME: stop exporting
1402+
*/
1403+
unsafe fn from_byte(u: u8) -> str { unsafe::from_bytes([u]) }
1404+
}
1405+
14011406

14021407
#[cfg(test)]
14031408
mod tests {
@@ -1771,9 +1776,9 @@ mod tests {
17711776
}
17721777

17731778
#[test]
1774-
fn test_unsafe_from_bytes() {
1779+
fn test_unsafe_from_bytes() unsafe {
17751780
let a = [65u8, 65u8, 65u8, 65u8, 65u8, 65u8, 65u8];
1776-
let b = unsafe_from_bytes(a);
1781+
let b = unsafe::from_bytes(a);
17771782
assert (b == "AAAAAAA");
17781783
}
17791784

0 commit comments

Comments
 (0)