We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4eeb706 commit e5cc919Copy full SHA for e5cc919
src/libcore/str.rs
@@ -213,16 +213,16 @@ Function: from_cstr_len
213
Create a Rust string from a C string of the given length
214
*/
215
unsafe fn from_cstr_len(cstr: sbuf, len: uint) -> str {
216
- let res = [];
217
- let start = cstr;
218
- let curr = start;
219
- let i = 0u;
220
- while i < len {
221
- vec::push(res, *curr);
222
- i += 1u;
223
- curr = ptr::offset(start, i);
224
- }
225
- ret from_bytes(res);
+ let buf: [u8] = [];
+ vec::reserve(buf, len + 1u);
+ vec::as_buf(buf) {|b| ptr::memcpy(b, cstr, len); }
+ vec::unsafe::set_len(buf, len);
+ buf += [0u8];
+
+ assert is_utf8(buf);
+ let s: str = ::unsafe::reinterpret_cast(buf);
+ ::unsafe::leak(buf);
+ ret s;
226
}
227
228
/*
0 commit comments