[−][src]Struct utf16string::WString
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_str("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]
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]
impl<E> WString<E> where
E: ByteOrder,
[src]
E: ByteOrder,
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 from_str(s: &str) -> Self
[src]
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]
F: FnMut(char) -> bool,
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]
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
.
pub fn get_mut<I>(
&mut self,
index: I
) -> Option<&mut <I as SliceIndex<WStr<E>>>::Output> where
I: SliceIndex<WStr<E>>,
[src]
&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
.
pub unsafe fn get_unchecked<I>(
&self,
index: I
) -> &<I as SliceIndex<WStr<E>>>::Output where
I: SliceIndex<WStr<E>>,
[src]
&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.
pub unsafe fn get_unchecked_mut<I>(
&mut self,
index: I
) -> &mut <I as SliceIndex<WStr<E>>>::Output where
I: SliceIndex<WStr<E>>,
[src]
&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.
pub fn chars(&self) -> WStrChars<'_, E>ⓘ
[src]
Returns an iterator of the char
s 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]
Notable traits for WStrCharIndices<'a, E>
impl<'a, E> Iterator for WStrCharIndices<'a, E> where
E: ByteOrder, type Item = (usize, char);
Returns and iterator over the char
s of a string slice and their positions.
pub fn to_utf8(&self) -> String
[src]
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]
E: ByteOrder,
impl<E> Deref for WString<E> where
E: ByteOrder,
[src]
E: ByteOrder,
impl<E> DerefMut for WString<E> where
E: ByteOrder,
[src]
E: ByteOrder,
impl<E: Eq + 'static + ByteOrder> Eq for WString<E>
[src]
impl<E, '_> From<&'_ String> for WString<E> where
E: ByteOrder,
[src]
E: ByteOrder,
impl<E, '_> From<&'_ mut str> for WString<E> where
E: ByteOrder,
[src]
E: ByteOrder,
impl<E, '_> From<&'_ str> for WString<E> where
E: ByteOrder,
[src]
E: ByteOrder,
impl<E: Hash + 'static + ByteOrder> Hash for WString<E>
[src]
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<I, E> Index<I> for WString<E> where
I: SliceIndex<WStr<E>>,
E: ByteOrder,
[src]
I: SliceIndex<WStr<E>>,
E: ByteOrder,
type Output = I::Output
The returned type after indexing.
fn index(&self, index: I) -> &I::Output
[src]
impl<I, E> IndexMut<I> for WString<E> where
I: SliceIndex<WStr<E>>,
E: ByteOrder,
[src]
I: SliceIndex<WStr<E>>,
E: ByteOrder,
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,
E: RefUnwindSafe,
impl<E> Send for WString<E> where
E: Sync,
E: Sync,
impl<E> Sync for WString<E> where
E: Sync,
E: Sync,
impl<E> Unpin for WString<E>
impl<E> UnwindSafe for WString<E> where
E: RefUnwindSafe,
E: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,