Struct WString

Source
pub struct WString<E: 'static + ByteOrder> { /* private fields */ }
Expand description

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§

Source§

impl WString<LittleEndian>

Source

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

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

Source

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

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.

Source§

impl WString<BigEndian>

Source

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

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

Source

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

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.

Source§

impl<E> WString<E>
where E: ByteOrder,

Source

pub fn new() -> Self

Creates a new empty WString.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new empty WString with a capacity.

Source

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

Converts a vector of bytes to a WString.

Source

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

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.

Source

pub fn into_bytes(self) -> Vec<u8>

Converts this string into a byte vector.

Source

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

Returns a &WStr slice containing the entire string.

Source

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

Returns a &mut WStr slice containing the entire string.

Source

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

Appends a string slice onto the end of this string.

Source

pub fn capacity(&self) -> usize

Returns the capacity in bytes.

Source

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

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

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of this string to match its length.

Source

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

Appends the given char to the end of this string.

Source

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

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.

Source

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

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

Returns None if this string is empty.

Source

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

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.

Source

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

Retains only the characters specified by the predicate.

Source

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

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.

Source

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

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.

Source

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

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.

Source

pub fn len(&self) -> usize

Returns the length in bytes, not chars or graphemes.

Source

pub fn is_empty(&self) -> bool

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

Source

pub fn split_off(&mut self, at: usize) -> WString<E>

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.

Source

pub fn clear(&mut self)

Truncates this string, removing all contents.

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

Methods from Deref<Target = WStr<E>>§

Source

pub fn len(&self) -> usize

The length in bytes, not chars or graphemes.

Source

pub fn is_empty(&self) -> bool

Returns true if the WStr::len is zero.

Source

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

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

Source

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

Converts to a byte slice.

Source

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

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.

Source

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

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.

Source

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

Converts to a mutable raw pointer to the byte slice.

Source

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

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.

Source

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

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.

Source

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

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.

Source

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

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.

Source

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

Returns an iterator of the chars of a string slice.

Source

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

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

Source

pub fn to_utf8(&self) -> String

Returns this WStr as a new owned String.

Source

pub fn is_ascii(&self) -> bool

Returns true if all characters in the string are ASCII.

Trait Implementations§

Source§

impl<E: Debug + 'static + ByteOrder> Debug for WString<E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<E> Default for WString<E>
where E: ByteOrder,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<E> Deref for WString<E>
where E: ByteOrder,

Source§

type Target = WStr<E>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<E> DerefMut for WString<E>
where E: ByteOrder,

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<E> From<&String> for WString<E>
where E: ByteOrder,

Source§

fn from(source: &String) -> Self

Converts to this type from the input type.
Source§

impl<E> From<&mut str> for WString<E>
where E: ByteOrder,

Source§

fn from(source: &mut str) -> Self

Converts to this type from the input type.
Source§

impl<E> From<&str> for WString<E>
where E: ByteOrder,

Source§

fn from(source: &str) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

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

Source§

type Output = <I as SliceIndex<WStr<E>>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &I::Output

Performs the indexing (container[index]) operation. Read more
Source§

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

Source§

fn index_mut(&mut self, index: I) -> &mut I::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

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

Source§

fn eq(&self, other: &WString<E>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

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

Auto Trait Implementations§

§

impl<E> Freeze for WString<E>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.