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>
impl WStr<LittleEndian>
Sourcepub fn from_utf16le(raw: &[u8]) -> Result<&Self, Utf16Error>
pub fn from_utf16le(raw: &[u8]) -> Result<&Self, Utf16Error>
Creates a new &WStr<LE>
.
Sourcepub fn from_utf16le_mut(raw: &mut [u8]) -> Result<&mut Self, Utf16Error>
pub fn from_utf16le_mut(raw: &mut [u8]) -> Result<&mut Self, Utf16Error>
Creates a new &mut WStr<LE>
.
Sourcepub unsafe fn from_utf16le_unchecked(raw: &[u8]) -> &Self
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.
Sourcepub unsafe fn from_utf16le_unchecked_mut(raw: &mut [u8]) -> &mut Self
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>
impl WStr<BigEndian>
Sourcepub fn from_utf16be(raw: &[u8]) -> Result<&Self, Utf16Error>
pub fn from_utf16be(raw: &[u8]) -> Result<&Self, Utf16Error>
Creates a new &WStr<BE>
.
Sourcepub fn from_utf16be_mut(raw: &mut [u8]) -> Result<&mut Self, Utf16Error>
pub fn from_utf16be_mut(raw: &mut [u8]) -> Result<&mut Self, Utf16Error>
Creates a new &mut WStr<BE>
.
Sourcepub unsafe fn from_utf16be_unchecked(raw: &[u8]) -> &Self
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.
Sourcepub unsafe fn from_utf16be_unchecked_mut(raw: &mut [u8]) -> &mut Self
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,
impl<E> WStr<E>where
E: ByteOrder,
Sourcepub fn from_utf16(raw: &[u8]) -> Result<&Self, Utf16Error>
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.
Sourcepub fn from_utf16_mut(raw: &mut [u8]) -> Result<&mut Self, Utf16Error>
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.
Sourcepub unsafe fn from_utf16_unchecked(raw: &[u8]) -> &Self
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.
Sourcepub unsafe fn from_utf16_unchecked_mut(raw: &mut [u8]) -> &mut Self
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.
Sourcepub fn is_char_boundary(&self, index: usize) -> bool
pub fn is_char_boundary(&self, index: usize) -> bool
Returns true
if the index into the bytes is on a char boundary.
Sourcepub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] ⓘ
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.
Sourcepub fn as_ptr(&self) -> *const u8
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.
Sourcepub fn as_mut_ptr(&mut self) -> *mut u8
pub fn as_mut_ptr(&mut self) -> *mut u8
Converts to a mutable raw pointer to the byte slice.
Sourcepub fn get<I>(&self, index: I) -> Option<&<I as SliceIndex<WStr<E>>>::Output>where
I: SliceIndex<WStr<E>>,
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
.
Sourcepub fn get_mut<I>(
&mut self,
index: I,
) -> Option<&mut <I as SliceIndex<WStr<E>>>::Output>where
I: SliceIndex<WStr<E>>,
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
.
Sourcepub unsafe fn get_unchecked<I>(
&self,
index: I,
) -> &<I as SliceIndex<WStr<E>>>::Outputwhere
I: SliceIndex<WStr<E>>,
pub unsafe fn get_unchecked<I>(
&self,
index: I,
) -> &<I as SliceIndex<WStr<E>>>::Outputwhere
I: SliceIndex<WStr<E>>,
Sourcepub unsafe fn get_unchecked_mut<I>(
&mut self,
index: I,
) -> &mut <I as SliceIndex<WStr<E>>>::Outputwhere
I: SliceIndex<WStr<E>>,
pub unsafe fn get_unchecked_mut<I>(
&mut self,
index: I,
) -> &mut <I as SliceIndex<WStr<E>>>::Outputwhere
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.
Sourcepub fn char_indices(&self) -> WStrCharIndices<'_, E> ⓘ
pub fn char_indices(&self) -> WStrCharIndices<'_, E> ⓘ
Returns and iterator over the char
s of a string slice and their positions.
Trait Implementations§
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]
.
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>
type Output = WStr<E>
Source§fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
Source§fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
Source§unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
SliceIndex::get
but without bounds checking. Read moreSource§unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
SliceIndex::get_mut
but without bounds checking. Read moreSource§impl<E> SliceIndex<WStr<E>> for RangeFrom<usize>where
E: ByteOrder,
Implements substring slicing with syntax &self[begin ..]
or &mut self[begin ..]
.
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>
type Output = WStr<E>
Source§fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
Source§fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
Source§unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
SliceIndex::get
but without bounds checking. Read moreSource§unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
SliceIndex::get_mut
but without bounds checking. Read moreSource§impl<E> SliceIndex<WStr<E>> for RangeFullwhere
E: ByteOrder,
Implments substring slicing with syntax &self[..]
or &mut self[..]
.
impl<E> SliceIndex<WStr<E>> for RangeFullwhere
E: ByteOrder,
Implments substring slicing with syntax &self[..]
or &mut self[..]
.
Unlike other implementations this can never panic.
Source§type Output = WStr<E>
type Output = WStr<E>
Source§fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
Source§fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
Source§unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
SliceIndex::get
but without bounds checking. Read moreSource§unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
SliceIndex::get_mut
but without bounds checking. Read moreSource§impl<E> SliceIndex<WStr<E>> for RangeInclusive<usize>where
E: ByteOrder,
Implements substring slicing with syntax &self[begin ..= end]
or &mut self[begin ..= end]
.
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>
type Output = WStr<E>
Source§fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
Source§fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
Source§unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
SliceIndex::get
but without bounds checking. Read moreSource§unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
SliceIndex::get_mut
but without bounds checking. Read moreSource§impl<E> SliceIndex<WStr<E>> for RangeTo<usize>where
E: ByteOrder,
Implements substring slicing with syntax &self[.. end]
or &mut self[.. end]
.
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>
type Output = WStr<E>
Source§fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
Source§fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
Source§unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
SliceIndex::get
but without bounds checking. Read moreSource§unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
SliceIndex::get_mut
but without bounds checking. Read moreSource§impl<E> SliceIndex<WStr<E>> for RangeToInclusive<usize>where
E: ByteOrder,
Implements substring slicing with syntax &self[..= end]
or &mut self[..= end]
.
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>
type Output = WStr<E>
Source§fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
fn get(self, slice: &WStr<E>) -> Option<&Self::Output>
Source§fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
fn get_mut(self, slice: &mut WStr<E>) -> Option<&mut Self::Output>
Source§unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
unsafe fn get_unchecked(self, slice: &WStr<E>) -> &Self::Output
SliceIndex::get
but without bounds checking. Read moreSource§unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
unsafe fn get_unchecked_mut(self, slice: &mut WStr<E>) -> &mut Self::Output
SliceIndex::get_mut
but without bounds checking. Read more