use vec::{ImmutableVector, MutableVector};
use vec;
use rt::global_heap::malloc_raw;
+use unstable::raw::Slice;
/// The representation of a C String.
///
}
/// Converts the CString into a `&[u8]` without copying.
+ /// Includes the terminating NUL byte.
///
/// # Failure
///
pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
if self.buf.is_null() { fail!("CString is null!"); }
unsafe {
- cast::transmute((self.buf, self.len() + 1))
+ cast::transmute(Slice { data: self.buf, len: self.len() + 1 })
+ }
+ }
+
+ /// Converts the CString into a `&[u8]` without copying.
+ /// Does not include the terminating NUL byte.
+ ///
+ /// # Failure
+ ///
+ /// Fails if the CString is null.
+ #[inline]
+ pub fn as_bytes_no_nul<'a>(&'a self) -> &'a [u8] {
+ if self.buf.is_null() { fail!("CString is null!"); }
+ unsafe {
+ cast::transmute(Slice { data: self.buf, len: self.len() })
}
}
/// Fails if the CString is null.
#[inline]
pub fn as_str<'a>(&'a self) -> Option<&'a str> {
- let buf = self.as_bytes();
- let buf = buf.slice_to(buf.len()-1); // chop off the trailing NUL
+ let buf = self.as_bytes_no_nul();
str::from_utf8(buf)
}
let expected = ["zero", "one"];
let mut it = expected.iter();
let result = from_c_multistring(ptr as *libc::c_char, None, |c| {
- let cbytes = c.as_bytes().slice_to(c.len());
+ let cbytes = c.as_bytes_no_nul();
assert_eq!(cbytes, it.next().unwrap().as_bytes());
});
assert_eq!(result, 2);
assert_eq!(c_str.as_bytes(), bytes!("foo", 0xff, 0));
}
+ #[test]
+ fn test_as_bytes_no_nul() {
+ let c_str = "hello".to_c_str();
+ assert_eq!(c_str.as_bytes_no_nul(), bytes!("hello"));
+ let c_str = "".to_c_str();
+ let exp: &[u8] = [];
+ assert_eq!(c_str.as_bytes_no_nul(), exp);
+ let c_str = bytes!("foo", 0xff).to_c_str();
+ assert_eq!(c_str.as_bytes_no_nul(), bytes!("foo", 0xff));
+ }
+
#[test]
#[should_fail]
fn test_as_bytes_fail() {
c_str.as_bytes();
}
+ #[test]
+ #[should_fail]
+ fn test_as_bytes_no_nul_fail() {
+ let c_str = unsafe { CString::new(ptr::null(), false) };
+ c_str.as_bytes_no_nul();
+ }
+
#[test]
fn test_as_str() {
let c_str = "hello".to_c_str();