Skip to content

Commit

Permalink
Implement core::str::from_cstr_len, close #1666
Browse files Browse the repository at this point in the history
  • Loading branch information
uasi committed Feb 12, 2012
1 parent 005a3ef commit 6408d54
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/libcore/str.rs
Expand Up @@ -17,6 +17,7 @@ export
from_char,
from_chars,
from_cstr,
from_cstr_len,
concat,
connect,

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

/*
Function: from_cstr_len
Create a Rust string from a C string of the given length
*/
unsafe fn from_cstr_len(cstr: sbuf, len: uint) -> str {
let res = [];
let start = cstr;
let curr = start;
let i = 0u;
while i < len {
vec::push(res, *curr);
i += 1u;
curr = ptr::offset(start, i);
}
ret from_bytes(res);
}

/*
Function: concat
Expand Down

0 comments on commit 6408d54

Please sign in to comment.