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>
impl WString<LittleEndian>
Sourcepub fn from_utf16le(buf: Vec<u8>) -> Result<Self, Utf16Error>
pub fn from_utf16le(buf: Vec<u8>) -> Result<Self, Utf16Error>
Creates a new WString
from raw bytes in little-endian byte order.
Source§impl WString<BigEndian>
impl WString<BigEndian>
Sourcepub fn from_utf16be(buf: Vec<u8>) -> Result<Self, Utf16Error>
pub fn from_utf16be(buf: Vec<u8>) -> Result<Self, Utf16Error>
Creates a new WString
from raw bytes in big-endian byte-order.
Source§impl<E> WString<E>where
E: ByteOrder,
impl<E> WString<E>where
E: ByteOrder,
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new empty WString
with a capacity.
Sourcepub fn from_utf16(buf: Vec<u8>) -> Result<Self, Utf16Error>
pub fn from_utf16(buf: Vec<u8>) -> Result<Self, Utf16Error>
Converts a vector of bytes to a WString
.
Sourcepub unsafe fn from_utf16_unchecked(buf: Vec<u8>) -> Self
pub unsafe fn from_utf16_unchecked(buf: Vec<u8>) -> Self
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Converts this string into a byte vector.
Sourcepub fn as_mut_wstr(&mut self) -> &mut WStr<E>
pub fn as_mut_wstr(&mut self) -> &mut WStr<E>
Returns a &mut WStr
slice containing the entire string.
Sourcepub fn push_wstr(&mut self, string: &WStr<E>)
pub fn push_wstr(&mut self, string: &WStr<E>)
Appends a string slice onto the end of this string.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Ensure that this string has spare capacity of at least additional
bytes.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of this string to match its length.
Sourcepub fn truncate(&mut self, new_len: usize)
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.
Sourcepub fn pop(&mut self) -> Option<char>
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.
Sourcepub fn insert_wstr(&mut self, idx: usize, string: &WStr<E>)
pub fn insert_wstr(&mut self, idx: usize, string: &WStr<E>)
Sourcepub unsafe fn as_mut_vec(&mut self) -> &mut Vec<u8> ⓘ
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.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the string has a WString::len
of zero, false
otherwise.
Methods from Deref<Target = WStr<E>>§
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.