stdlib: Implement str::unsafe_from_bytes_ivec()

This commit is contained in:
Patrick Walton 2011-07-08 22:23:11 -07:00
parent aa0f6f4961
commit 91d45b91e6
3 changed files with 26 additions and 0 deletions

View file

@ -55,6 +55,7 @@ native "rust" mod rustrt {
fn str_vec(str s) -> vec[u8]; fn str_vec(str s) -> vec[u8];
fn str_byte_len(str s) -> uint; fn str_byte_len(str s) -> uint;
fn str_alloc(uint n_bytes) -> str; fn str_alloc(uint n_bytes) -> str;
fn str_from_ivec(&u8[mutable?] b) -> str;
fn str_from_vec(vec[mutable? u8] b) -> str; fn str_from_vec(vec[mutable? u8] b) -> str;
fn str_from_cstr(sbuf cstr) -> str; fn str_from_cstr(sbuf cstr) -> str;
fn str_from_buf(sbuf buf, uint len) -> str; fn str_from_buf(sbuf buf, uint len) -> str;
@ -170,6 +171,10 @@ fn unsafe_from_bytes(vec[mutable? u8] v) -> str {
ret rustrt::str_from_vec(v); ret rustrt::str_from_vec(v);
} }
fn unsafe_from_bytes_ivec(&u8[mutable?] v) -> str {
ret rustrt::str_from_ivec(v);
}
fn unsafe_from_byte(u8 u) -> str { ret rustrt::str_from_vec([u]); } fn unsafe_from_byte(u8 u) -> str { ret rustrt::str_from_vec([u]); }
fn str_from_cstr(sbuf cstr) -> str { ret rustrt::str_from_cstr(cstr); } fn str_from_cstr(sbuf cstr) -> str { ret rustrt::str_from_cstr(cstr); }

View file

@ -313,6 +313,26 @@ str_byte_len(rust_task *task, rust_str *s)
return s->fill - 1; // -1 for the '\0' terminator. return s->fill - 1; // -1 for the '\0' terminator.
} }
extern "C" CDECL rust_str *
str_from_ivec(rust_task *task, rust_ivec *v)
{
uintptr_t fill = v->fill ? v->fill : v->payload.ptr->fill;
void *data = v->fill ? v->payload.data : v->payload.ptr->data;
rust_str *st =
vec_alloc_with_data(task,
fill + 1, // +1 to fit at least '\0'
fill,
1,
fill ? data : NULL);
if (!st) {
task->fail(2);
return NULL;
}
st->data[st->fill++] = '\0';
return st;
}
extern "C" CDECL rust_str * extern "C" CDECL rust_str *
str_from_vec(rust_task *task, rust_vec *v) str_from_vec(rust_task *task, rust_vec *v)
{ {

View file

@ -41,6 +41,7 @@ str_buf
str_byte_len str_byte_len
str_from_buf str_from_buf
str_from_cstr str_from_cstr
str_from_ivec
str_from_vec str_from_vec
str_push_byte str_push_byte
str_slice str_slice