Skip to content

Commit faef949

Browse files
committed
Optimize std::istr::to_estr. Issue #855
1 parent 15e3ae7 commit faef949

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/lib/istr.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ fn from_estr(s: &str) -> istr {
1616
}
1717

1818
fn to_estr(s: &istr) -> str {
19-
let s2 = "";
20-
for u in s {
21-
str::push_byte(s2, u);
22-
}
23-
ret s2;
19+
if byte_len(s) == 0u { ret "" };
20+
let v: [u8] = unsafe::reinterpret_cast(s);
21+
let vlen = vec::len(v);
22+
assert vlen > 0u;
23+
vec::unsafe::set_len(v, vlen - 1u);
24+
let res = str::unsafe_from_bytes(v);
25+
vec::unsafe::set_len(v, vlen);
26+
unsafe::leak(v);
27+
ret res;
2428
}
2529

2630
fn from_estrs(ss: &[str]) -> [istr] {

0 commit comments

Comments
 (0)