std: remove str::NullTerminatedStr
This commit is contained in:
parent
fd293dfb0f
commit
dca9ff9a13
3 changed files with 2 additions and 76 deletions
|
|
@ -63,7 +63,7 @@ pub use path::PosixPath;
|
|||
pub use path::WindowsPath;
|
||||
pub use ptr::RawPtr;
|
||||
pub use ascii::{Ascii, AsciiCast, OwnedAsciiCast, AsciiStr, ToBytesConsume};
|
||||
pub use str::{Str, StrVector, StrSlice, OwnedStr, NullTerminatedStr};
|
||||
pub use str::{Str, StrVector, StrSlice, OwnedStr};
|
||||
pub use from_str::{FromStr};
|
||||
pub use to_bytes::IterBytes;
|
||||
pub use to_str::{ToStr, ToStrConsume};
|
||||
|
|
|
|||
|
|
@ -1981,35 +1981,6 @@ impl<'self> StrSlice<'self> for &'self str {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(missing_doc)]
|
||||
pub trait NullTerminatedStr {
|
||||
fn as_bytes_with_null<'a>(&'a self) -> &'a [u8];
|
||||
}
|
||||
|
||||
impl NullTerminatedStr for ~str {
|
||||
/// Work with the byte buffer of a string as a byte slice.
|
||||
///
|
||||
/// The byte slice does include the null terminator.
|
||||
#[inline]
|
||||
fn as_bytes_with_null<'a>(&'a self) -> &'a [u8] {
|
||||
let ptr: &'a ~[u8] = unsafe { cast::transmute(self) };
|
||||
let slice: &'a [u8] = *ptr;
|
||||
slice
|
||||
}
|
||||
}
|
||||
|
||||
impl NullTerminatedStr for @str {
|
||||
/// Work with the byte buffer of a string as a byte slice.
|
||||
///
|
||||
/// The byte slice does include the null terminator.
|
||||
#[inline]
|
||||
fn as_bytes_with_null<'a>(&'a self) -> &'a [u8] {
|
||||
let ptr: &'a @[u8] = unsafe { cast::transmute(self) };
|
||||
let slice: &'a [u8] = *ptr;
|
||||
slice
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(missing_doc)]
|
||||
pub trait OwnedStr {
|
||||
fn push_str_no_overallocate(&mut self, rhs: &str);
|
||||
|
|
@ -2979,30 +2950,6 @@ mod tests {
|
|||
assert_eq!("ศไทย中华Việt Nam".as_bytes(), v);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_as_bytes_with_null() {
|
||||
// has null
|
||||
let v = [
|
||||
224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228,
|
||||
184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
|
||||
109, 0
|
||||
];
|
||||
|
||||
let s1 = @"";
|
||||
let s2 = @"abc";
|
||||
let s3 = @"ศไทย中华Việt Nam";
|
||||
assert_eq!(s1.as_bytes_with_null(), &[0]);
|
||||
assert_eq!(s2.as_bytes_with_null(), &['a' as u8, 'b' as u8, 'c' as u8, 0]);
|
||||
assert_eq!(s3.as_bytes_with_null(), v);
|
||||
|
||||
let s1 = ~"";
|
||||
let s2 = ~"abc";
|
||||
let s3 = ~"ศไทย中华Việt Nam";
|
||||
assert_eq!(s1.as_bytes_with_null(), &[0]);
|
||||
assert_eq!(s2.as_bytes_with_null(), &['a' as u8, 'b' as u8, 'c' as u8, 0]);
|
||||
assert_eq!(s3.as_bytes_with_null(), v);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_bytes_with_null() {
|
||||
let s = ~"ศไทย中华Việt Nam";
|
||||
|
|
@ -3024,7 +2971,7 @@ mod tests {
|
|||
// Don't double free. (I'm not sure if this exercises the
|
||||
// original problem code path anymore.)
|
||||
let s = ~"";
|
||||
let _bytes = s.as_bytes_with_null();
|
||||
let _bytes = s.as_bytes();
|
||||
fail!();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,21 +0,0 @@
|
|||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
fn main() {
|
||||
let _ = (~"foo").as_bytes_with_null();
|
||||
let _ = (@"foo").as_bytes_with_null();
|
||||
|
||||
// a plain static slice is null terminated, but such a slice can
|
||||
// be sliced shorter (i.e. become non-null terminated) and still
|
||||
// have the static lifetime
|
||||
let foo: &'static str = "foo";
|
||||
let _ = foo.as_bytes_with_null();
|
||||
//~^ ERROR does not implement any method in scope named `as_bytes_with_null`
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue