rustc: Add str_from_cstr() and str_from_buf() functions to the standard library, as well as a test case

This commit is contained in:
Patrick Walton 2011-03-16 18:40:51 -07:00
parent 320ac6b8eb
commit ea7197e2cf
4 changed files with 48 additions and 0 deletions

View file

@ -498,6 +498,7 @@ TEST_XFAILS_RUSTC := $(addprefix test/run-pass/, \
lib-sha1.rs \
lib-sort.rs \
lib-str.rs \
lib-str-buf.rs \
lib-task.rs \
lib-uint.rs \
lib-vec-str-conversions.rs \

View file

@ -8,6 +8,8 @@
fn str_byte_len(str s) -> uint;
fn str_alloc(uint n_bytes) -> str;
fn str_from_vec(vec[u8] b) -> str;
fn str_from_cstr(sbuf cstr) -> str;
fn str_from_buf(sbuf buf, uint len) -> str;
fn refcount[T](str s) -> uint;
}
@ -115,6 +117,14 @@ fn unsafe_from_byte(u8 u) -> str {
ret rustrt.str_from_vec(vec(u));
}
unsafe fn str_from_cstr(sbuf cstr) -> str {
ret rustrt.str_from_cstr(cstr);
}
unsafe fn str_from_buf(sbuf buf, uint len) -> str {
ret rustrt.str_from_buf(buf, len);
}
fn refcount(str s) -> uint {
auto r = rustrt.refcount[u8](s);

View file

@ -207,6 +207,29 @@ str_from_vec(rust_task *task, rust_vec *v)
return st;
}
extern "C" CDECL rust_str *
str_from_cstr(rust_task *task, char *sbuf)
{
size_t len = strlen(sbuf) + 1;
rust_str *st = vec_alloc_with_data(task, len, len, 1, sbuf);
if (!st) {
task->fail(2);
return NULL;
}
return st;
}
extern "C" CDECL rust_str *
str_from_buf(rust_task *task, char *buf, unsigned int len) {
rust_str *st = vec_alloc_with_data(task, len + 1, len, 1, buf);
if (!st) {
task->fail(2);
return NULL;
}
st->data[st->fill++] = '\0';
return st;
}
extern "C" CDECL void *
rand_new(rust_task *task)
{

View file

@ -0,0 +1,14 @@
// -*- rust -*-
use std;
import std._str;
fn main() {
auto s = "hello";
auto sb = _str.rustrt.str_buf(s);
auto s_cstr = _str.rustrt.str_from_cstr(sb);
check (_str.eq(s_cstr, s));
auto s_buf = _str.rustrt.str_from_buf(sb, 5u);
check (_str.eq(s_buf, s));
}