diff --git a/src/libcore/str.rs b/src/libcore/str.rs index cf996a8b254ca..fb62fc65b6d1e 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -2087,6 +2087,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 +2199,14 @@ impl &str: StrSlice { #[inline] pure fn to_unique() -> ~str { self.slice(0, self.len()) } + #[inline] + pure fn to_managed() -> @str { + let v = at_vec::from_fn(self.len() + 1, |i| { + if i == self.len() { 0 } else { self[i] } + }); + unsafe { ::cast::transmute(v) } + } + #[inline] pure fn char_at(i: uint) -> char { char_at(self, i) } } @@ -3175,4 +3184,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"; + } + }