Skip to content

Commit 6408d54

Browse files
committed
Implement core::str::from_cstr_len, close #1666
1 parent 005a3ef commit 6408d54

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/libcore/str.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export
1717
from_char,
1818
from_chars,
1919
from_cstr,
20+
from_cstr_len,
2021
concat,
2122
connect,
2223

@@ -210,6 +211,24 @@ unsafe fn from_cstr(cstr: sbuf) -> str {
210211
ret from_bytes(res);
211212
}
212213

214+
/*
215+
Function: from_cstr_len
216+
217+
Create a Rust string from a C string of the given length
218+
*/
219+
unsafe fn from_cstr_len(cstr: sbuf, len: uint) -> str {
220+
let res = [];
221+
let start = cstr;
222+
let curr = start;
223+
let i = 0u;
224+
while i < len {
225+
vec::push(res, *curr);
226+
i += 1u;
227+
curr = ptr::offset(start, i);
228+
}
229+
ret from_bytes(res);
230+
}
231+
213232
/*
214233
Function: concat
215234

0 commit comments

Comments
 (0)