about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGareth Daniel Smith <garethdanielsmith@gmail.com>2012-10-04 20:22:53 +0100
committerNiko Matsakis <niko@alum.mit.edu>2012-10-13 05:57:13 -0700
commite9caa3fe2e291b7f2cea624bfd0dc879e95fe382 (patch)
tree3676bc6a1bc93db442abf7ecd3f247be1318c67f /src
parent9406f8101dd32b4458b8c042a1bfb1bb150a2276 (diff)
downloadrust-e9caa3fe2e291b7f2cea624bfd0dc879e95fe382.tar.gz
rust-e9caa3fe2e291b7f2cea624bfd0dc879e95fe382.zip
Add an &str.to_managed method to allow creating non-constant @str values (for issue #3433).
Diffstat (limited to 'src')
-rw-r--r--src/libcore/str.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs
index 677a33d56c0..e2e7b15b1d7 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 {
 
@@ -2090,6 +2095,7 @@ pub trait StrSlice {
     pure fn trim_left() -> ~str;
     pure fn trim_right() -> ~str;
     pure fn to_unique() -> ~str;
+    pure fn to_managed() -> @str;
     pure fn char_at(i: uint) -> char;
 }
 
@@ -2214,6 +2220,14 @@ impl &str: StrSlice {
     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) }
 }
 
@@ -3190,4 +3204,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";
+    }
+
 }