[][src]Struct utf16string::WString

pub struct WString<E: 'static + ByteOrder> { /* fields omitted */ }

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

Examples

use utf16string::{LE, WString};

let v = Vec::from(&b"h\x00e\x00l\x00l\x00o\x00"[..]);
let s = WString::from_utf16le(v)?;

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

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

Converting from valid Unicode is infallible:

use utf16string::{LE, WString};

let s0: WString<LE> = WString::from("hello");
assert_eq!(s0.len(), 10);

let s1: WString<LE> = From::from("hello");
assert_eq!(s0, s1);

Implementations

impl WString<LittleEndian>[src]

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

Creates a new WString from raw bytes in little-endian byte order.

pub unsafe fn from_utf16le_unchecked(buf: Vec<u8>) -> Self[src]

Converts a vector of bytes to a WString, not checking validity.

Safety

You must ensure the vector contains already valid UTF-16 with little-endian byte-order, otherwise you will get undefined behaviour.

impl WString<BigEndian>[src]

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

Creates a new WString from raw bytes in big-endian byte-order.

pub unsafe fn from_utf16be_unchecked(buf: Vec<u8>) -> Self[src]

Converts a vector of bytes to a WString, not checking validity.

Safety

You must ensure the vector contains already valid UTF-16 with big-endian byte-order, otherwise you will get undefined behaviour.

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

pub fn new() -> Self[src]

Creates a new empty WString.

pub fn with_capacity(capacity: usize) -> Self[src]

Creates a new empty WString with a capacity.

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

Converts a vector of bytes to a WString.

pub unsafe fn from_utf16_unchecked(buf: Vec<u8>) -> Self[src]

Converts a vector of bytes to a WString, not checking validity.

Safety

You must ensure the vector contains already valid UTF-16 in the correct byte-order, otherwise you will get undefined behaviour.

pub fn into_bytes(self) -> Vec<u8>[src]

Converts this string into a byte vector.

pub fn as_wstr(&self) -> &WStr<E>[src]

Returns a &WStr slice containing the entire string.

pub fn as_mut_wstr(&mut self) -> &mut WStr<E>[src]

Returns a &mut WStr slice containing the entire string.

pub fn push_wstr(&mut self, string: &WStr<E>)[src]

Appends a string slice onto the end of this string.

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

Returns the capacity in bytes.

pub fn reserve(&mut self, additional: usize)[src]

Ensure that this string has spare capacity of at least additional bytes.

pub fn shrink_to_fit(&mut self)[src]

Shrinks the capacity of this string to match its length.

pub fn push(&mut self, ch: char)[src]

Appends the given char to the end of this string.

pub fn truncate(&mut self, new_len: usize)[src]

Shortens this string to the specified length.

The new_len is specified in bytes and not characters, just as WString::len returns the length in bytes. If new_len is greater than the string's current length, this has no effect.

Note that this method has no effect on the allocated capacity of the string.

Panics

Panics if new_len does not lie on a char boundary.

pub fn pop(&mut self) -> Option<char>[src]

Removes the last character from the string buffer and returns it.

Returns None if this string is empty.

pub fn remove(&mut self, idx: usize) -> char[src]

Removes a char from this string at the given byte position and returns it.

This is an O(n) operation as it requires copying every element in the buffer.

Panics

Panics if idx is larger than or equal to the string's length, or if it does not lie on a char boundary.

pub fn retain<F>(&mut self, f: F) where
    F: FnMut(char) -> bool
[src]

Retains only the characters specified by the predicate.

pub fn insert(&mut self, idx: usize, ch: char)[src]

Inserts a char into this string at the given byte position.

This is an O(n) operation as it requires copying every element in the buffer.

Panics

Panics if idx is larger than the string's length or if it does not lie on a char boundary.

pub fn insert_wstr(&mut self, idx: usize, string: &WStr<E>)[src]

Inserts a string slice into this string at the given byte position.

This is an O(n) operation as it requires copying every element in the buffer. The string slice must have the same endianess.

Panics

Panics if idx is larger than the string's length or if it does not lie on a char boundary.

pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8>[src]

Returns a mutable reference to the contents of this string.

Safety

You must ensure that the bytes remain encoded in UTF-16 with the correct byte-order, otherwise you will get undefined behaviour trying to use the string.

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

Returns the length in bytes, not chars or graphemes.

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

Returns true if the string has a WString::len of zero, false otherwise.

#[must_use = "use `.truncate()` if you don't need the other half"]pub fn split_off(&mut self, at: usize) -> WString<E>[src]

Splits the string into two at the given index.

Returns a newly allocated WString. self contains bytes [0..at] and the returned WString contains bytes [at..len]].

Panics

Panics if at is not on a character boundary or is beyond the end of the string.

pub fn clear(&mut self)[src]

Truncates this string, removing all contents.

The length will be zero, but the capacity will remain unchanged.

Methods from Deref<Target = WStr<E>>

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: Debug + 'static + ByteOrder> Debug for WString<E>[src]

impl<E> Default for WString<E> where
    E: ByteOrder
[src]

impl<E> Deref for WString<E> where
    E: ByteOrder
[src]

type Target = WStr<E>

The resulting type after dereferencing.

impl<E> DerefMut for WString<E> where
    E: ByteOrder
[src]

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

impl<E, '_> From<&'_ String> for WString<E> where
    E: ByteOrder
[src]

impl<E, '_> From<&'_ mut str> for WString<E> where
    E: ByteOrder
[src]

impl<E, '_> From<&'_ str> for WString<E> where
    E: ByteOrder
[src]

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

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

type Output = I::Output

The returned type after indexing.

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

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

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

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

Auto Trait Implementations

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

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

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

impl<E> Unpin for WString<E>

impl<E> UnwindSafe for WString<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, 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.