Skip to content

Add an &str.to_managed method to allow creating non-constant @str values... #3657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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";
}

}