diff options
Diffstat (limited to 'src/libstd/sys/common/wtf8.rs')
| -rw-r--r-- | src/libstd/sys/common/wtf8.rs | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/src/libstd/sys/common/wtf8.rs b/src/libstd/sys/common/wtf8.rs index 6c17f9910ac..dfc88571a82 100644 --- a/src/libstd/sys/common/wtf8.rs +++ b/src/libstd/sys/common/wtf8.rs @@ -29,7 +29,6 @@ use core::prelude::*; use core::char::{encode_utf8_raw, encode_utf16_raw}; use core::str::{char_range_at_raw, next_code_point}; -use core::raw::Slice as RawSlice; use ascii::*; use borrow::Cow; @@ -173,6 +172,7 @@ impl Wtf8Buf { Wtf8Buf { bytes: string.into_bytes() } } + #[cfg(stage0)] /// Create a WTF-8 string from an UTF-8 `&str` slice. /// /// This copies the content of the slice. @@ -183,6 +183,17 @@ impl Wtf8Buf { Wtf8Buf { bytes: slice::SliceExt::to_vec(str.as_bytes()) } } + #[cfg(not(stage0))] + /// Create a WTF-8 string from an UTF-8 `&str` slice. + /// + /// This copies the content of the slice. + /// + /// Since WTF-8 is a superset of UTF-8, this always succeeds. + #[inline] + pub fn from_str(str: &str) -> Wtf8Buf { + Wtf8Buf { bytes: <[_]>::to_vec(str.as_bytes()) } + } + /// Create a WTF-8 string from a potentially ill-formed UTF-16 slice of 16-bit code units. /// /// This is lossless: calling `.encode_wide()` on the resulting string @@ -214,10 +225,10 @@ impl Wtf8Buf { unsafe { // Attempt to not use an intermediate buffer by just pushing bytes // directly onto this string. - let slice = RawSlice { - data: self.bytes.as_ptr().offset(cur_len as int), - len: 4, - }; + let slice = slice::from_raw_parts_mut( + self.bytes.as_mut_ptr().offset(cur_len as int), + 4 + ); let used = encode_utf8_raw(code_point.value, mem::transmute(slice)) .unwrap_or(0); self.bytes.set_len(cur_len + used); @@ -725,10 +736,11 @@ pub fn is_code_point_boundary(slice: &Wtf8, index: uint) -> bool { /// Copied from core::str::raw::slice_unchecked #[inline] pub unsafe fn slice_unchecked(s: &Wtf8, begin: uint, end: uint) -> &Wtf8 { - mem::transmute(RawSlice { - data: s.bytes.as_ptr().offset(begin as int), - len: end - begin, - }) + // memory layout of an &[u8] and &Wtf8 are the same + mem::transmute(slice::from_raw_parts( + s.bytes.as_ptr().offset(begin as int), + end - begin + )) } /// Copied from core::str::raw::slice_error_fail |
