Struct WStr

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

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§

Source§

impl WStr<LittleEndian>

Source

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

Creates a new &WStr<LE>.

Source

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

Creates a new &mut WStr<LE>.

Source

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

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.

Source

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

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.

Source§

impl WStr<BigEndian>

Source

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

Creates a new &WStr<BE>.

Source

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

Creates a new &mut WStr<BE>.

Source

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

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.

Source

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

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.

Source§

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

Source

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

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

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

Source

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

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

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

Source

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

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.

Source

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

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.

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> AsRef<[u8]> for WStr<E>
where E: ByteOrder,

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

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

Source§

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

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

impl<E> Display for WStr<E>
where E: ByteOrder,

Source§

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

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

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

Source§

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

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

impl<I, E> Index<I> for WStr<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 WStr<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 WStr<E>

Source§

fn eq(&self, other: &WStr<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> SliceIndex<WStr<E>> for Range<usize>
where E: ByteOrder,

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

Source§

type Output = WStr<E>

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

fn get(self, slice: &WStr<E>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output

Like SliceIndex::get but without bounds checking. Read more
Source§

unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Like SliceIndex::get_mut but without bounds checking. Read more
Source§

fn index(self, slice: &WStr<E>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<E> SliceIndex<WStr<E>> for RangeFrom<usize>
where E: ByteOrder,

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

Source§

type Output = WStr<E>

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

fn get(self, slice: &WStr<E>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output

Like SliceIndex::get but without bounds checking. Read more
Source§

unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Like SliceIndex::get_mut but without bounds checking. Read more
Source§

fn index(self, slice: &WStr<E>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<E> SliceIndex<WStr<E>> for RangeFull
where E: ByteOrder,

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

Unlike other implementations this can never panic.

Source§

type Output = WStr<E>

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

fn get(self, slice: &WStr<E>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output

Like SliceIndex::get but without bounds checking. Read more
Source§

unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Like SliceIndex::get_mut but without bounds checking. Read more
Source§

fn index(self, slice: &WStr<E>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<E> SliceIndex<WStr<E>> for RangeInclusive<usize>
where E: ByteOrder,

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

Source§

type Output = WStr<E>

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

fn get(self, slice: &WStr<E>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output

Like SliceIndex::get but without bounds checking. Read more
Source§

unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Like SliceIndex::get_mut but without bounds checking. Read more
Source§

fn index(self, slice: &WStr<E>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<E> SliceIndex<WStr<E>> for RangeTo<usize>
where E: ByteOrder,

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

Source§

type Output = WStr<E>

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

fn get(self, slice: &WStr<E>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output

Like SliceIndex::get but without bounds checking. Read more
Source§

unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Like SliceIndex::get_mut but without bounds checking. Read more
Source§

fn index(self, slice: &WStr<E>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

impl<E> SliceIndex<WStr<E>> for RangeToInclusive<usize>
where E: ByteOrder,

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

Source§

type Output = WStr<E>

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

fn get(self, slice: &WStr<E>) -> Option<&Self::Output>

Returns a shared reference to the output at this location, if in bounds.
Source§

fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>

Returns a mutable reference to the output at this location, if in bounds.
Source§

unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output

Like SliceIndex::get but without bounds checking. Read more
Source§

unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Like SliceIndex::get_mut but without bounds checking. Read more
Source§

fn index(self, slice: &WStr<E>) -> &Self::Output

Returns a shared reference to the output at this location, panicking if out of bounds.
Source§

fn index_mut(self, slice: &mut WStr<E>) -> &mut Self::Output

Returns a mutable reference to the output at this location, panicking if out of bounds.
Source§

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

Source§

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

Auto Trait Implementations§

§

impl<E> Freeze for WStr<E>

§

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

§

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

§

impl<E> !Sized for WStr<E>

§

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§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more