From 6408d54c133554a938ffc304f320786cfe6a424e Mon Sep 17 00:00:00 2001 From: Tomoki Aonuma Date: Sun, 12 Feb 2012 13:21:50 +0900 Subject: [PATCH] Implement core::str::from_cstr_len, close #1666 --- src/libcore/str.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index ca7bb819443..5cedb1f0064 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -17,6 +17,7 @@ export from_char, from_chars, from_cstr, + from_cstr_len, concat, connect, @@ -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