Remove trailing null from strings
This commit is contained in:
parent
17e0089856
commit
5865a7597b
12 changed files with 544 additions and 43 deletions
|
@ -15,7 +15,9 @@ use str;
|
|||
use str::StrSlice;
|
||||
use cast;
|
||||
use iterator::{Iterator, IteratorUtil};
|
||||
use vec::{CopyableVector, ImmutableVector, OwnedVector};
|
||||
use vec::{CopyableVector, ImmutableVector};
|
||||
#[cfg(stage0)]
|
||||
use vec::OwnedVector;
|
||||
use to_bytes::IterBytes;
|
||||
use option::{Some, None};
|
||||
|
||||
|
@ -101,19 +103,26 @@ impl<'self> AsciiCast<&'self[Ascii]> for &'self [u8] {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'self> AsciiCast<&'self[Ascii]> for &'self str {
|
||||
impl<'self> AsciiCast<&'self [Ascii]> for &'self str {
|
||||
#[inline]
|
||||
fn to_ascii(&self) -> &'self[Ascii] {
|
||||
fn to_ascii(&self) -> &'self [Ascii] {
|
||||
assert!(self.is_ascii());
|
||||
unsafe {self.to_ascii_nocheck()}
|
||||
unsafe { self.to_ascii_nocheck() }
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
#[inline]
|
||||
unsafe fn to_ascii_nocheck(&self) -> &'self[Ascii] {
|
||||
unsafe fn to_ascii_nocheck(&self) -> &'self [Ascii] {
|
||||
let (p,len): (*u8, uint) = cast::transmute(*self);
|
||||
cast::transmute((p, len - 1))
|
||||
}
|
||||
|
||||
#[cfg(not(stage0))]
|
||||
#[inline]
|
||||
unsafe fn to_ascii_nocheck(&self) -> &'self [Ascii] {
|
||||
cast::transmute(*self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_ascii(&self) -> bool {
|
||||
self.byte_iter().all(|b| b.is_ascii())
|
||||
|
@ -186,12 +195,19 @@ impl OwnedAsciiCast for ~str {
|
|||
unsafe {self.into_ascii_nocheck()}
|
||||
}
|
||||
|
||||
#[cfg(stage0)]
|
||||
#[inline]
|
||||
unsafe fn into_ascii_nocheck(self) -> ~[Ascii] {
|
||||
let mut r: ~[Ascii] = cast::transmute(self);
|
||||
r.pop();
|
||||
r
|
||||
}
|
||||
|
||||
#[cfg(not(stage0))]
|
||||
#[inline]
|
||||
unsafe fn into_ascii_nocheck(self) -> ~[Ascii] {
|
||||
cast::transmute(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for converting an ascii type to a string. Needed to convert `&[Ascii]` to `~str`
|
||||
|
@ -210,11 +226,19 @@ pub trait AsciiStr {
|
|||
}
|
||||
|
||||
impl<'self> AsciiStr for &'self [Ascii] {
|
||||
#[cfg(stage0)]
|
||||
#[inline]
|
||||
fn to_str_ascii(&self) -> ~str {
|
||||
let mut cpy = self.to_owned();
|
||||
cpy.push(0u8.to_ascii());
|
||||
unsafe {cast::transmute(cpy)}
|
||||
unsafe { cast::transmute(cpy) }
|
||||
}
|
||||
|
||||
#[cfg(not(stage0))]
|
||||
#[inline]
|
||||
fn to_str_ascii(&self) -> ~str {
|
||||
let cpy = self.to_owned();
|
||||
unsafe { cast::transmute(cpy) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -234,11 +258,18 @@ impl<'self> AsciiStr for &'self [Ascii] {
|
|||
}
|
||||
|
||||
impl ToStrConsume for ~[Ascii] {
|
||||
#[cfg(stage0)]
|
||||
#[inline]
|
||||
fn into_str(self) -> ~str {
|
||||
let mut cpy = self;
|
||||
cpy.push(0u8.to_ascii());
|
||||
unsafe {cast::transmute(cpy)}
|
||||
unsafe { cast::transmute(cpy) }
|
||||
}
|
||||
|
||||
#[cfg(not(stage0))]
|
||||
#[inline]
|
||||
fn into_str(self) -> ~str {
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -257,7 +288,7 @@ pub trait ToBytesConsume {
|
|||
|
||||
impl ToBytesConsume for ~[Ascii] {
|
||||
fn into_bytes(self) -> ~[u8] {
|
||||
unsafe {cast::transmute(self)}
|
||||
unsafe { cast::transmute(self) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue