Skip to content

Commit 91d45b9

Browse files
committed
stdlib: Implement str::unsafe_from_bytes_ivec()
1 parent aa0f6f4 commit 91d45b9

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/lib/str.rs

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ native "rust" mod rustrt {
5555
fn str_vec(str s) -> vec[u8];
5656
fn str_byte_len(str s) -> uint;
5757
fn str_alloc(uint n_bytes) -> str;
58+
fn str_from_ivec(&u8[mutable?] b) -> str;
5859
fn str_from_vec(vec[mutable? u8] b) -> str;
5960
fn str_from_cstr(sbuf cstr) -> str;
6061
fn str_from_buf(sbuf buf, uint len) -> str;
@@ -170,6 +171,10 @@ fn unsafe_from_bytes(vec[mutable? u8] v) -> str {
170171
ret rustrt::str_from_vec(v);
171172
}
172173

174+
fn unsafe_from_bytes_ivec(&u8[mutable?] v) -> str {
175+
ret rustrt::str_from_ivec(v);
176+
}
177+
173178
fn unsafe_from_byte(u8 u) -> str { ret rustrt::str_from_vec([u]); }
174179

175180
fn str_from_cstr(sbuf cstr) -> str { ret rustrt::str_from_cstr(cstr); }

src/rt/rust_builtin.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,26 @@ str_byte_len(rust_task *task, rust_str *s)
313313
return s->fill - 1; // -1 for the '\0' terminator.
314314
}
315315

316+
extern "C" CDECL rust_str *
317+
str_from_ivec(rust_task *task, rust_ivec *v)
318+
{
319+
uintptr_t fill = v->fill ? v->fill : v->payload.ptr->fill;
320+
void *data = v->fill ? v->payload.data : v->payload.ptr->data;
321+
322+
rust_str *st =
323+
vec_alloc_with_data(task,
324+
fill + 1, // +1 to fit at least '\0'
325+
fill,
326+
1,
327+
fill ? data : NULL);
328+
if (!st) {
329+
task->fail(2);
330+
return NULL;
331+
}
332+
st->data[st->fill++] = '\0';
333+
return st;
334+
}
335+
316336
extern "C" CDECL rust_str *
317337
str_from_vec(rust_task *task, rust_vec *v)
318338
{

src/rt/rustrt.def.in

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ str_buf
4141
str_byte_len
4242
str_from_buf
4343
str_from_cstr
44+
str_from_ivec
4445
str_from_vec
4546
str_push_byte
4647
str_slice

0 commit comments

Comments
 (0)