Skip to content
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

Add OwnedStr::into_bytes #8733

Merged
merged 1 commit into from
Aug 24, 2013
Merged
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
17 changes: 16 additions & 1 deletion src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,7 @@ pub trait OwnedStr {
fn reserve_at_least(&mut self, n: uint);
fn capacity(&self) -> uint;
fn truncate(&mut self, len: uint);
fn into_bytes(self) -> ~[u8];

/// Work with the mutable byte buffer and length of a slice.
///
Expand Down Expand Up @@ -2273,6 +2274,13 @@ impl OwnedStr for ~str {
unsafe { raw::set_len(self, len); }
}

/// Consumes the string, returning the underlying byte buffer.
///
/// The buffer does not have a null terminator.
#[inline]
fn into_bytes(self) -> ~[u8] {
unsafe { cast::transmute(self) }
}

#[inline]
fn as_mut_buf<T>(&mut self, f: &fn(*mut u8, uint) -> T) -> T {
Expand Down Expand Up @@ -2356,7 +2364,7 @@ mod tests {
use ptr;
use str::*;
use vec;
use vec::{ImmutableVector, CopyableVector};
use vec::{Vector, ImmutableVector, CopyableVector};
use cmp::{TotalOrd, Less, Equal, Greater};

#[test]
Expand Down Expand Up @@ -2524,6 +2532,13 @@ mod tests {
assert_eq!("华", data.as_slice());
}

#[test]
fn test_into_bytes() {
let data = ~"asdf";
let buf = data.into_bytes();
assert_eq!(bytes!("asdf"), buf.as_slice());
}

#[test]
fn test_find_str() {
// byte positions
Expand Down