Struct wtf8::Wtf8Buf
[−]
[src]
pub struct Wtf8Buf { /* fields omitted */ }
An owned, growable string of well-formed WTF-8 data.
Similar to String
, but can additionally contain surrogate code points
if they’re not in a surrogate pair.
Methods
impl Wtf8Buf
[src]
fn new() -> Wtf8Buf
Create an new, empty WTF-8 string.
fn with_capacity(n: usize) -> Wtf8Buf
Create an new, empty WTF-8 string with pre-allocated capacity for n
bytes.
fn from_string(string: String) -> Wtf8Buf
Create a WTF-8 string from an UTF-8 String
.
This takes ownership of the String
and does not copy.
Since WTF-8 is a superset of UTF-8, this always succeeds.
fn from_str(str: &str) -> Wtf8Buf
Create a WTF-8 string from an UTF-8 &str
slice.
This copies the content of the slice.
Since WTF-8 is a superset of UTF-8, this always succeeds.
fn from_ill_formed_utf16(v: &[u16]) -> Wtf8Buf
Create a WTF-8 string from a potentially ill-formed UTF-16 slice of 16-bit code units.
This is lossless: calling .to_ill_formed_utf16()
on the resulting string
will always return the original code units.
fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more bytes to be inserted
in the given Wtf8Buf
.
The collection may reserve more space to avoid frequent reallocations.
Panics
Panics if the new capacity overflows usize
.
Example
let mut s = Wtf8Buf::new(); s.reserve(10); assert!(s.capacity() >= 10);
fn capacity(&self) -> usize
Returns the number of bytes that this string buffer can hold without reallocating.
Example
let s = Wtf8Buf::with_capacity(10); assert!(s.capacity() >= 10);
fn push_str(&mut self, other: &str)
Append an UTF-8 slice at the end of the string.
fn push_wtf8(&mut self, other: &Wtf8)
Append a WTF-8 slice at the end of the string.
This replaces newly paired surrogates at the boundary with a supplementary code point, like concatenating ill-formed UTF-16 strings effectively would.
fn push_char(&mut self, c: char)
Append a Unicode scalar value at the end of the string.
fn push(&mut self, code_point: CodePoint)
Append a code point at the end of the string.
This replaces newly paired surrogates at the boundary with a supplementary code point, like concatenating ill-formed UTF-16 strings effectively would.
fn truncate(&mut self, new_len: usize)
Shortens a string to the specified length.
Failure
Fails if new_len
> current length,
or if new_len
is not a code point boundary.
fn into_string(self) -> Result<String, Wtf8Buf>
Consume the WTF-8 string and try to convert it to UTF-8.
This does not copy the data.
If the contents are not well-formed UTF-8 (that is, if the string contains surrogates), the original WTF-8 string is returned instead.
fn into_string_lossy(self) -> String
Consume the WTF-8 string and convert it lossily to UTF-8.
This does not copy the data (but may overwrite parts of it in place).
Surrogates are replaced with "\u{FFFD}"
(the replacement character “�”)
Methods from Deref<Target = Wtf8>
fn len(&self) -> usize
Return the length, in WTF-8 bytes.
fn slice(&self, begin: usize, end: usize) -> &Wtf8
Return a slice of the given string for the byte range [begin
..end
).
Failure
Fails when begin
and end
do not point to code point boundaries,
or point beyond the end of the string.
fn slice_from(&self, begin: usize) -> &Wtf8
Return a slice of the given string from byte begin
to its end.
Failure
Fails when begin
is not at a code point boundary,
or is beyond the end of the string.
fn slice_to(&self, end: usize) -> &Wtf8
Return a slice of the given string from its beginning to byte end
.
Failure
Fails when end
is not at a code point boundary,
or is beyond the end of the string.
fn ascii_byte_at(&self, position: usize) -> u8
Return the code point at position
if it is in the ASCII range,
or `b'\xFF' otherwise.
Failure
Fails if position
is beyond the end of the string.
fn code_points(&self) -> Wtf8CodePoints
Return an iterator for the string’s code points.
fn as_str(&self) -> Option<&str>
Try to convert the string to UTF-8 and return a &str
slice.
Return None
if the string contains surrogates.
This does not copy the data.
fn to_string_lossy(&self) -> Cow<str>
Lossily convert the string to UTF-8.
Return an UTF-8 &str
slice if the contents are well-formed in UTF-8.
Surrogates are replaced with "\u{FFFD}"
(the replacement character “�”).
This only copies the data if necessary (if it contains any surrogate).
fn to_ill_formed_utf16(&self) -> IllFormedUtf16CodeUnits
Convert the WTF-8 string to potentially ill-formed UTF-16 and return an iterator of 16-bit code units.
This is lossless:
calling Wtf8Buf::from_ill_formed_utf16
on the resulting code units
would always return the original WTF-8 string.
Trait Implementations
impl Eq for Wtf8Buf
[src]
impl PartialEq for Wtf8Buf
[src]
fn eq(&self, __arg_0: &Wtf8Buf) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Wtf8Buf) -> bool
This method tests for !=
.
impl Ord for Wtf8Buf
[src]
fn cmp(&self, __arg_0: &Wtf8Buf) -> Ordering
This method returns an Ordering
between self
and other
. Read more
impl PartialOrd for Wtf8Buf
[src]
fn partial_cmp(&self, __arg_0: &Wtf8Buf) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, __arg_0: &Wtf8Buf) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
fn le(&self, __arg_0: &Wtf8Buf) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
fn gt(&self, __arg_0: &Wtf8Buf) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
fn ge(&self, __arg_0: &Wtf8Buf) -> bool
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
impl Clone for Wtf8Buf
[src]
fn clone(&self) -> Wtf8Buf
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl Deref for Wtf8Buf
[src]
type Target = Wtf8
The resulting type after dereferencing
fn deref(&self) -> &Wtf8
The method called to dereference a value
impl Debug for Wtf8Buf
[src]
Format the string with double quotes,
and surrogates as \u
followed by four hexadecimal digits.
Example: "a\u{D800}"
for a string with code points [U+0061, U+D800]
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error>
Formats the value using the given formatter.
impl FromIterator<CodePoint> for Wtf8Buf
[src]
Create a new WTF-8 string from an iterator of code points.
This replaces surrogate code point pairs with supplementary code points, like concatenating ill-formed UTF-16 strings effectively would.
fn from_iter<T: IntoIterator<Item = CodePoint>>(iterable: T) -> Wtf8Buf
Creates a value from an iterator. Read more
impl Extend<CodePoint> for Wtf8Buf
[src]
Append code points from an iterator to the string.
This replaces surrogate code point pairs with supplementary code points, like concatenating ill-formed UTF-16 strings effectively would.
fn extend<T: IntoIterator<Item = CodePoint>>(&mut self, iterable: T)
Extends a collection with the contents of an iterator. Read more
impl<'a> PartialEq<&'a Wtf8> for Wtf8Buf
[src]
fn eq(&self, other: &&Wtf8) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0
This method tests for !=
.