[][src]Struct utf16string::WStr

#[repr(transparent)]pub struct WStr<E: 'static + ByteOrder> { /* fields omitted */ }

A UTF-16 str-like type with little- or big-endian byte order.

This mostly behaves like str does for UTF-8 encoded bytes slices, but works with UTF-16 encoded byte slices. The endianess is determined by the type parameter.

Examples

use utf16string::{LE, WStr};

let b = b"h\x00e\x00l\x00l\x00o\x00";
let s: &WStr<LE> = WStr::from_utf16le(b)?;

let chars: Vec<char> = s.chars().collect();
assert_eq!(chars, vec!['h', 'e', 'l', 'l', 'o']);

assert_eq!(s.to_utf8(), "hello");

Implementations

impl WStr<LittleEndian>[src]

pub fn from_utf16le(raw: &[u8]) -> Result<&Self, Utf16Error>[src]

Creates a new &WStr<LE>.

pub fn from_utf16le_mut(raw: &mut [u8]) -> Result<&mut Self, Utf16Error>[src]

Creates a new &mut WStr<LE>.

pub unsafe fn from_utf16le_unchecked(raw: &[u8]) -> &Self[src]

Creates a new WStr with little-endian byte-ordering.

This is a shortcut to easily create WStr<LE> without having to specify the type explicitly.

Example

use utf16string::{LE, WStr};

let b = b"h\x00i\x00";
let s: &WStr<LE> = unsafe { WStr::from_utf16_unchecked(b) };
let t = unsafe { WStr::from_utf16le_unchecked(b) };
assert_eq!(s, t);

Safety

You must guarantee that the buffer passed in is encoded correctly as UTF-16 with little-endian byte-order, otherwise you will get undefined behaviour.

pub unsafe fn from_utf16le_unchecked_mut(raw: &mut [u8]) -> &mut Self[src]

Creates a new &mut WStr<LE>.

Safety

You must guarantee that the buffer passed in is encoded correctly as UTF-16 with little-endian byte-order, otherwise you will get undefined behaviour.

impl WStr<BigEndian>[src]

pub fn from_utf16be(raw: &[u8]) -> Result<&Self, Utf16Error>[src]

Creates a new &WStr<BE>.

pub fn from_utf16be_mut(raw: &mut [u8]) -> Result<&mut Self, Utf16Error>[src]

Creates a new &mut WStr<BE>.

pub unsafe fn from_utf16be_unchecked(raw: &[u8]) -> &Self[src]

Creates a new &WStr<BE> from an existing byte-slice with big-endian byte-ordering.

This is a shortcut to easily create WStr<BE> without having to specify the type explicitly.

Example

use utf16string::{BE, WStr};

let b = b"h\x00i\x00";
let s: &WStr<BE> = unsafe { WStr::from_utf16_unchecked(b) };
let t = unsafe { WStr::from_utf16be_unchecked(b) };
assert_eq!(s, t);

Safety

You must guarantee that the buffer passed in is encoded correctly as UTF-16 with big-endian byte-order, otherwise you will get undefined behaviour.

pub unsafe fn from_utf16be_unchecked_mut(raw: &mut [u8]) -> &mut Self[src]

Creates a new &mut WStr<BE>.

Safety

You must guarantee that the buffer passed in is encoded correctly as UTF-16 with big-endian byte-order, otherwise you will get undefined behaviour.

impl<E> WStr<E> where
    E: ByteOrder
[src]

pub fn from_utf16(raw: &[u8]) -> Result<&Self, Utf16Error>[src]

Creates a new &WStr<E> from an existing UTF-16 byte-slice.

If the byte-slice is not valid Utf16Error is returned.

pub fn from_utf16_mut(raw: &mut [u8]) -> Result<&mut Self, Utf16Error>[src]

Creates a new &mut WStr<E> from an existing UTF-16 byte-slice.

If the byte-slice is not valid Utf16Error is returned.

pub unsafe fn from_utf16_unchecked(raw: &[u8]) -> &Self[src]

Creates a new &WStr<E> from an existing UTF-16 byte-slice.

Safety

You must guarantee that the buffer passed in is encoded correctly otherwise you will get undefined behaviour. Be aware of the byte-level endianess.

pub unsafe fn from_utf16_unchecked_mut(raw: &mut [u8]) -> &mut Self[src]

Like WStr::from_utf16_unchecked but return a mutable reference.

Safety

You must guarantee that the buffer passed in is encoded correctly otherwise you will get undefined behaviour.

pub fn len(&self) -> usize[src]

The length in bytes, not chars or graphemes.

pub fn is_empty(&self) -> bool[src]

Returns true if the WStr::len is zero.

pub fn is_char_boundary(&self, index: usize) -> bool[src]

Returns true if the index into the bytes is on a char boundary.

pub fn as_bytes(&self) -> &[u8][src]

Converts to a byte slice.

pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8][src]

Converts to a mutable byte slice.

Safety

When mutating the bytes it must still be valid encoded UTF-16 with the correct byte-order, otherwise you will get undefined behaviour.

pub fn as_ptr(&self) -> *const u8[src]

Converts to a raw pointer to the byte slice.

This is currently not const fn because this is not yet stable with a trait bound.

pub fn as_mut_ptr(&mut self) -> *mut u8[src]

Converts to a mutable raw pointer to the byte slice.

pub fn get<I>(&self, index: I) -> Option<&<I as SliceIndex<WStr<E>>>::Output> where
    I: SliceIndex<WStr<E>>, 
[src]

Returns a subslice of self.

The slice indices are on byte offsets of the underlying UTF-16 encoded buffer, if the subslice is not on character boundaries or otherwise invalid this will return None.

pub fn get_mut<I>(
    &mut self,
    index: I
) -> Option<&mut <I as SliceIndex<WStr<E>>>::Output> where
    I: SliceIndex<WStr<E>>, 
[src]

Returns a mutable subslice of self.

The slice indices are on byte offsets of the underlying UTF-16 encoded buffer, if the subslice is not on character boundaries or otherwise invalid this will return None.

pub unsafe fn get_unchecked<I>(
    &self,
    index: I
) -> &<I as SliceIndex<WStr<E>>>::Output where
    I: SliceIndex<WStr<E>>, 
[src]

Returns a subslice of self.

Safety

Like WStr::get but this results in undefined behaviour if the sublice is not on character boundaries or otherwise invalid.

pub unsafe fn get_unchecked_mut<I>(
    &mut self,
    index: I
) -> &mut <I as SliceIndex<WStr<E>>>::Output where
    I: SliceIndex<WStr<E>>, 
[src]

Returns a mutable subslice of self.

Safety

Lice WStr::get_mut but this results in undefined behaviour if the subslice is not on character boundaries or otherwise invalid.

pub fn chars(&self) -> WStrChars<'_, E>

Notable traits for WStrChars<'a, E>

impl<'a, E> Iterator for WStrChars<'a, E> where
    E: ByteOrder
type Item = char;
[src]

Returns an iterator of the chars of a string slice.

pub fn char_indices(&self) -> WStrCharIndices<'_, E>

Notable traits for WStrCharIndices<'a, E>

impl<'a, E> Iterator for WStrCharIndices<'a, E> where
    E: ByteOrder
type Item = (usize, char);
[src]

Returns and iterator over the chars of a string slice and their positions.

pub fn to_utf8(&self) -> String[src]

Returns this WStr as a new owned String.

pub fn is_ascii(&self) -> bool[src]

Returns true if all characters in the string are ASCII.

Trait Implementations

impl<E> AsRef<[u8]> for WStr<E> where
    E: ByteOrder
[src]

impl<E: Debug + 'static + ByteOrder> Debug for WStr<E>[src]

impl<E> Display for WStr<E> where
    E: ByteOrder
[src]

impl<E: Eq + 'static + ByteOrder> Eq for WStr<E>[src]

impl<E: Hash + 'static + ByteOrder> Hash for WStr<E>[src]

impl<I, E> Index<I> for WStr<E> where
    I: SliceIndex<WStr<E>>,
    E: ByteOrder
[src]

type Output = I::Output

The returned type after indexing.

impl<I, E> IndexMut<I> for WStr<E> where
    I: SliceIndex<WStr<E>>,
    E: ByteOrder
[src]

impl<E: PartialEq + 'static + ByteOrder> PartialEq<WStr<E>> for WStr<E>[src]

impl<E> SliceIndex<WStr<E>> for RangeFull where
    E: ByteOrder
[src]

Implments substring slicing with syntax &self[..] or &mut self[..].

Unlike other implementations this can never panic.

type Output = WStr<E>

The result of slicing, another slice of the same type as you started with normally.

impl<E> SliceIndex<WStr<E>> for Range<usize> where
    E: ByteOrder
[src]

Implements substring slicing with syntax &self[begin .. end] or &mut self[begin .. end].

type Output = WStr<E>

The result of slicing, another slice of the same type as you started with normally.

impl<E> SliceIndex<WStr<E>> for RangeTo<usize> where
    E: ByteOrder
[src]

Implements substring slicing with syntax &self[.. end] or &mut self[.. end].

type Output = WStr<E>

The result of slicing, another slice of the same type as you started with normally.

impl<E> SliceIndex<WStr<E>> for RangeFrom<usize> where
    E: ByteOrder
[src]

Implements substring slicing with syntax &self[begin ..] or &mut self[begin ..].

type Output = WStr<E>

The result of slicing, another slice of the same type as you started with normally.

impl<E> SliceIndex<WStr<E>> for RangeInclusive<usize> where
    E: ByteOrder
[src]

Implements substring slicing with syntax &self[begin ..= end] or &mut self[begin ..= end].

type Output = WStr<E>

The result of slicing, another slice of the same type as you started with normally.

impl<E> SliceIndex<WStr<E>> for RangeToInclusive<usize> where
    E: ByteOrder
[src]

Implements substring slicing with syntax &self[..= end] or &mut self[..= end].

type Output = WStr<E>

The result of slicing, another slice of the same type as you started with normally.

impl<E: 'static + ByteOrder> StructuralEq for WStr<E>[src]

impl<E: 'static + ByteOrder> StructuralPartialEq for WStr<E>[src]

Auto Trait Implementations

impl<E> RefUnwindSafe for WStr<E> where
    E: RefUnwindSafe

impl<E> Send for WStr<E> where
    E: Sync

impl<E> Sync for WStr<E> where
    E: Sync

impl<E> Unpin for WStr<E>

impl<E> UnwindSafe for WStr<E> where
    E: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.