std: rename str.as_bytes_with_null_consume to str.to_bytes_with_null

This commit is contained in:
Erick Tryzelaar 2013-07-23 16:56:17 -07:00
parent 7434080dd7
commit 9ad815e063
3 changed files with 8 additions and 9 deletions

View File

@ -545,7 +545,7 @@ priv fn format(val: Param, op: FormatOp, flags: Flags) -> Result<~[u8],~str> {
String(s) => { String(s) => {
match op { match op {
FormatString => { FormatString => {
let mut s = s.as_bytes_with_null_consume(); let mut s = s.to_bytes_with_null();
s.pop(); // remove the null s.pop(); // remove the null
if flags.precision > 0 && flags.precision < s.len() { if flags.precision > 0 && flags.precision < s.len() {
s.truncate(flags.precision); s.truncate(flags.precision);

View File

@ -2073,8 +2073,7 @@ pub trait OwnedStr {
fn reserve(&mut self, n: uint); fn reserve(&mut self, n: uint);
fn reserve_at_least(&mut self, n: uint); fn reserve_at_least(&mut self, n: uint);
fn capacity(&self) -> uint; fn capacity(&self) -> uint;
fn to_bytes_with_null(self) -> ~[u8];
fn as_bytes_with_null_consume(self) -> ~[u8];
} }
impl OwnedStr for ~str { impl OwnedStr for ~str {
@ -2263,7 +2262,7 @@ impl OwnedStr for ~str {
/// Convert to a vector of bytes. This does not allocate a new /// Convert to a vector of bytes. This does not allocate a new
/// string, and includes the null terminator. /// string, and includes the null terminator.
#[inline] #[inline]
fn as_bytes_with_null_consume(self) -> ~[u8] { fn to_bytes_with_null(self) -> ~[u8] {
unsafe { ::cast::transmute(self) } unsafe { ::cast::transmute(self) }
} }
} }
@ -3065,17 +3064,17 @@ mod tests {
} }
#[test] #[test]
fn test_as_bytes_with_null_consume() { fn test_to_bytes_with_null() {
let s = ~"ศไทย中华Việt Nam"; let s = ~"ศไทย中华Việt Nam";
let v = ~[ let v = ~[
224, 184, 168, 224, 185, 132, 224, 184, 151, 224, 184, 162, 228, 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, 184, 173, 229, 141, 142, 86, 105, 225, 187, 135, 116, 32, 78, 97,
109, 0 109, 0
]; ];
assert_eq!((~"").as_bytes_with_null_consume(), ~[0]); assert_eq!((~"").to_bytes_with_null(), ~[0]);
assert_eq!((~"abc").as_bytes_with_null_consume(), assert_eq!((~"abc").to_bytes_with_null(),
~['a' as u8, 'b' as u8, 'c' as u8, 0]); ~['a' as u8, 'b' as u8, 'c' as u8, 0]);
assert_eq!(s.as_bytes_with_null_consume(), v); assert_eq!(s.to_bytes_with_null(), v);
} }
#[test] #[test]

View File

@ -26,7 +26,7 @@ mod libc {
fn strlen(str: ~str) -> uint { fn strlen(str: ~str) -> uint {
unsafe { unsafe {
// C string is terminated with a zero // C string is terminated with a zero
let bytes = str.as_bytes_with_null_consume(); let bytes = str.to_bytes_with_null();
return libc::my_strlen(vec::raw::to_ptr(bytes)); return libc::my_strlen(vec::raw::to_ptr(bytes));
} }
} }