From 851617d48856a9063b42e491288df12553f63cfa Mon Sep 17 00:00:00 2001 From: Gareth Daniel Smith Date: Thu, 4 Oct 2012 20:22:53 +0100 Subject: [PATCH 1/2] Add an &str.to_managed method to allow creating non-constant @str values (for issue #3433). --- src/libcore/str.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index cf996a8b254ca..55105d8aa2812 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -1869,6 +1869,11 @@ pub pure fn escape_unicode(s: &str) -> ~str { move out } +extern mod rustrt { + #[rust_stack] + pure fn upcall_str_new_shared(cstr: *libc::c_char, len: size_t) -> @str; +} + /// Unsafe operations pub mod raw { @@ -2087,6 +2092,7 @@ pub trait StrSlice { fn escape_default() -> ~str; fn escape_unicode() -> ~str; pure fn to_unique() -> ~str; + pure fn to_managed() -> @str; pure fn char_at(i: uint) -> char; } @@ -2198,6 +2204,14 @@ impl &str: StrSlice { #[inline] pure fn to_unique() -> ~str { self.slice(0, self.len()) } + #[inline] + pure fn to_managed() -> @str { + do str::as_buf(self) |p, _len| { + rustrt::upcall_str_new_shared(p as *libc::c_char, + self.len() as size_t) + } + } + #[inline] pure fn char_at(i: uint) -> char { char_at(self, i) } } @@ -3175,4 +3189,10 @@ mod tests { assert escape_default(~"\U0001d4ea\r") == ~"\\U0001d4ea\\r"; } + #[test] + fn test_to_managed() { + assert (~"abc").to_managed() == @"abc"; + assert view("abcdef", 1, 5).to_managed() == @"bcde"; + } + } From 6f690a1c96c370d3598b9e45836f95d503087989 Mon Sep 17 00:00:00 2001 From: Gareth Daniel Smith Date: Sat, 6 Oct 2012 11:11:06 +0100 Subject: [PATCH 2/2] Implement to_managed without using an upcall function, as suggested by brson. --- src/libcore/str.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 55105d8aa2812..fb62fc65b6d1e 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -1869,11 +1869,6 @@ pub pure fn escape_unicode(s: &str) -> ~str { move out } -extern mod rustrt { - #[rust_stack] - pure fn upcall_str_new_shared(cstr: *libc::c_char, len: size_t) -> @str; -} - /// Unsafe operations pub mod raw { @@ -2206,10 +2201,10 @@ impl &str: StrSlice { #[inline] pure fn to_managed() -> @str { - do str::as_buf(self) |p, _len| { - rustrt::upcall_str_new_shared(p as *libc::c_char, - self.len() as size_t) - } + let v = at_vec::from_fn(self.len() + 1, |i| { + if i == self.len() { 0 } else { self[i] } + }); + unsafe { ::cast::transmute(v) } } #[inline]