Struct basic_text::TextSubstring
source · pub struct TextSubstring(/* private fields */);
Expand description
A substring of a Basic Text string or stream.
This does not enforce the Basic Text requirements on the beginning or end of a stream, so it can represent substrings of Basic Text.
This is an owning string similar to TextString
, but doesn’t enforce the
starting and ending requirements, so it can represent substrings. It’s
accompanied by a borrowing TextSubstr
, which plays an analogous role to
TextStr
.
§Examples
You can create a TextSubstring
from a literal text string with
TextSubstring::from
:
use basic_text::{text_substr, TextSubstring};
let hello = TextSubstring::from(text_substr!("Hello, world!"));
If you have a String
containing a Basic Text string, you can create a
TextSubstring
from it with the from_text
method:
use basic_text::{text_substr, TextSubstring};
// a `String`
let sparkle_heart = "💖".to_owned();
// We know this string is valid, so we'll use `unwrap()`.
let sparkle_heart = TextSubstring::from_text(sparkle_heart).unwrap();
assert_eq!(text_substr!("💖"), &sparkle_heart);
If you have a vector of Basic Text bytes, you can create a String
from it
with the from_text_vec
method:
use basic_text::{text_substr, TextSubstring};
// some bytes, in a vector
let sparkle_heart = vec![240, 159, 146, 150];
// We know these bytes are valid, so we'll use `unwrap()`.
let sparkle_heart = TextSubstring::from_text_vec(sparkle_heart).unwrap();
assert_eq!(text_substr!("💖"), &sparkle_heart);
Implementations§
source§impl TextSubstring
impl TextSubstring
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new empty TextSubstring
with a particular capacity.
sourcepub fn from_text_vec(vec: Vec<u8>) -> Result<Self, FromTextError>
pub fn from_text_vec(vec: Vec<u8>) -> Result<Self, FromTextError>
Converts a vector of bytes to a TextSubstring
.
sourcepub fn from_text(s: String) -> Result<Self, FromTextError>
pub fn from_text(s: String) -> Result<Self, FromTextError>
Converts a String
to a TextSubstring
.
sourcepub fn from_text_bytes_lossy(v: &[u8]) -> Cow<'_, TextSubstr>
pub fn from_text_bytes_lossy(v: &[u8]) -> Cow<'_, TextSubstr>
Converts a slice of bytes to Basic Text, including invalid characters.
sourcepub fn from_text_lossy(v: &str) -> Cow<'_, TextSubstr>
pub fn from_text_lossy(v: &str) -> Cow<'_, TextSubstr>
Converts a string to Basic Text, including invalid characters.
sourcepub unsafe fn from_text_vec_unchecked(vec: Vec<u8>) -> Self
pub unsafe fn from_text_vec_unchecked(vec: Vec<u8>) -> Self
Converts a vector of bytes to a TextSubstring
without checking that
the string contains valid Basic Text.
§Safety
This function is unsafe because it does not check that the bytes passed
to it are valid Basic Text. If this constraint is violated, undefined
behavior results, as the rest of this crate assumes that &TextSubstr
s
are valid Basic Text.
sourcepub const unsafe fn from_text_unchecked(s: String) -> Self
pub const unsafe fn from_text_unchecked(s: String) -> Self
Converts a String
to a TextSubstring
without checking that the
string contains valid Basic Text.
§Safety
This function is unsafe because it does not check that the bytes passed
to it are valid Basic Text. If this constraint is violated, undefined
behavior results, as the rest of this crate assumes that &TextSubstr
s
are valid Basic Text.
sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Converts a TextSubstring
into a String
.
sourcepub fn into_bytes(self) -> Vec<u8>
pub fn into_bytes(self) -> Vec<u8>
Converts a String into a byte vector.
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Extracts a UTF-8 string slice containing the entire TextSubstring
.
sourcepub fn as_text(&self) -> &TextSubstr
pub fn as_text(&self) -> &TextSubstr
Extracts a Basic Text string slice containing the entire
TextSubstring
.
sourcepub fn as_mut_text(&mut self) -> &mut TextSubstr
pub fn as_mut_text(&mut self) -> &mut TextSubstr
Converts a TextSubstring
into a mutable Basic Text substring slice.
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Ensures that this TextSubstring
’s capacity is at least additional
bytes larger than its length.
sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Ensures that this TextSubstring
’s capacity is additional
bytes
larger than its length.
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of this TextSubstring
to match its length.
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 TextSubstring
.
§Safety
This function is unsafe because it does not check that the bytes passed
to it are valid Basic Text. If this constraint is violated, it may
cause memory unsafety issues with future users of the String, as the
rest of this crate assumes that TextSubstring
s are valid Basic Text.
sourcepub unsafe fn as_mut_string(&mut self) -> &mut String
pub unsafe fn as_mut_string(&mut self) -> &mut String
Returns a mutable reference to the contents of this TextSubstring
.
§Safety
This function is unsafe because it does not check that the bytes passed
to it are valid Basic Text. If this constraint is violated, it may
cause memory unsafety issues with future users of the String, as the
rest of this crate assumes that TextSubstring
s are valid Basic Text.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of this TextSubstring
, in bytes, not char
s or
graphemes.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if this TextSubstring
has a length of zero, and
false
otherwise.
sourcepub fn into_boxed_str(self) -> Box<str>
pub fn into_boxed_str(self) -> Box<str>
Converts this TextSubstring
into a Box<str>
.
sourcepub fn into_boxed_text(self) -> Box<TextSubstr>
pub fn into_boxed_text(self) -> Box<TextSubstr>
Converts this TextSubstring
into a Box<TextSubstr>
.
Methods from Deref<Target = TextSubstr>§
sourcepub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] ⓘ
pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] ⓘ
Converts a mutable text string slice to a mutable byte slice.
§Safety
The caller must ensure that the content of the slice is valid
Basic Text before the borrow ends and the underlying TextSubstr
is
used.
Use of a TextSubstr
whose contents are not valid Basic Text is
undefined behavior.
sourcepub fn as_mut_ptr(&mut self) -> *mut u8
pub fn as_mut_ptr(&mut self) -> *mut u8
Converts a mutable text string slice to a raw pointer.
sourcepub fn split_at(&self, mid: usize) -> (&Self, &Self)
pub fn split_at(&self, mid: usize) -> (&Self, &Self)
Divide one text string slice into two at an index.
sourcepub fn split_at_mut(&mut self, mid: usize) -> (&mut Self, &mut Self)
pub fn split_at_mut(&mut self, mid: usize) -> (&mut Self, &mut Self)
Divide one mutable text string slice into two at an index.
sourcepub fn char_indices(&self) -> CharIndices<'_>
pub fn char_indices(&self) -> CharIndices<'_>
Returns an iterator over the char
s of a text string slice, and their
positions.
sourcepub fn lines(&self) -> Lines<'_>
pub fn lines(&self) -> Lines<'_>
An iterator over the lines of a text string, as text string slices.
TODO: There should be a TextLines
which yields &TextSubstr
s.
sourcepub fn encode_utf16(&self) -> EncodeUtf16<'_>
pub fn encode_utf16(&self) -> EncodeUtf16<'_>
Returns an iterator of u16
over the string encoded as Basic Text.
sourcepub fn contains<'a, P>(&'a self, pat: P) -> boolwhere
P: Pattern<'a>,
pub fn contains<'a, P>(&'a self, pat: P) -> boolwhere
P: Pattern<'a>,
Returns true
if the given pattern matches a sub-slice of this
text string slice.
Returns false
if it does not.
sourcepub fn starts_with<'a, P>(&'a self, pat: P) -> boolwhere
P: Pattern<'a>,
pub fn starts_with<'a, P>(&'a self, pat: P) -> boolwhere
P: Pattern<'a>,
Returns true
if the given pattern matches a prefix of this
text string slice.
Returns false
if it does not.
sourcepub fn ends_with<'a, P>(&'a self, pat: P) -> bool
pub fn ends_with<'a, P>(&'a self, pat: P) -> bool
Returns true
if the given pattern matches a suffix of this
text string slice.
Returns false
if it does not.
sourcepub fn find<'a, P>(&'a self, pat: P) -> Option<usize>where
P: Pattern<'a>,
pub fn find<'a, P>(&'a self, pat: P) -> Option<usize>where
P: Pattern<'a>,
Returns the byte index of the first character of this text string slice that matches the pattern.
Returns None
if the pattern doesn’t match.
sourcepub fn rfind<'a, P>(&'a self, pat: P) -> Option<usize>
pub fn rfind<'a, P>(&'a self, pat: P) -> Option<usize>
Returns the byte index for the first character of the rightmost match of the pattern in this text string slice.
Returns None
if the pattern doesn’t match.
sourcepub fn matches<'a, P>(&'a self, pat: P) -> Matches<'a, P>where
P: Pattern<'a>,
pub fn matches<'a, P>(&'a self, pat: P) -> Matches<'a, P>where
P: Pattern<'a>,
An iterator over the disjoint matches of a pattern within the given text string slice.
TODO: There should be a TextMatches
which yields &TextSubstr
s.
sourcepub fn rmatches<'a, P>(&'a self, pat: P) -> RMatches<'a, P>
pub fn rmatches<'a, P>(&'a self, pat: P) -> RMatches<'a, P>
An iterator over the disjoint matches of a pattern within this text string slice, yielded in reverse order.
TODO: There should be a TextRMatches
which yields &TextSubstr
s.
sourcepub fn match_indices<'a, P>(&'a self, pat: P) -> MatchIndices<'a, P>where
P: Pattern<'a>,
pub fn match_indices<'a, P>(&'a self, pat: P) -> MatchIndices<'a, P>where
P: Pattern<'a>,
An iterator over the disjoint matches of a pattern within this text string slice as well as the index that the match starts at.
TODO: There should be a TextMatchIndices
which yields &TextSubstr
s.
sourcepub fn rmatch_indices<'a, P>(&'a self, pat: P) -> RMatchIndices<'a, P>
pub fn rmatch_indices<'a, P>(&'a self, pat: P) -> RMatchIndices<'a, P>
An iterator over the disjoint matches of a pattern within self
,
yielded in reverse order along with the index of the match.
TODO: There should be a TextRMatchIndices
which yields
&TextSubstr
s.
sourcepub fn trim(&self) -> &Self
pub fn trim(&self) -> &Self
Returns a text string slice with leading and trailing whitespace removed.
sourcepub fn trim_start(&self) -> &Self
pub fn trim_start(&self) -> &Self
Returns a text string slice with leading whitespace removed.
sourcepub fn parse<F>(&self) -> Result<F, <F as FromStr>::Err>where
F: FromStr,
pub fn parse<F>(&self) -> Result<F, <F as FromStr>::Err>where
F: FromStr,
Parses this text string slice into another type.
sourcepub fn is_ascii(&self) -> bool
pub fn is_ascii(&self) -> bool
Checks if all characters in this text string are within the ASCII range.
sourcepub fn eq_ignore_ascii_case(&self, other: &Self) -> bool
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool
Checks that two text strings are an ASCII case-insensitive match.
sourcepub fn repeat(&self, n: usize) -> TextSubstring
pub fn repeat(&self, n: usize) -> TextSubstring
Creates a new TextSubstring
by repeating a string n
times.
sourcepub fn escape_debug(&self) -> EscapeDebug<'_>
pub fn escape_debug(&self) -> EscapeDebug<'_>
Return an iterator that escapes each char
in self
with
char::escape_debug
.
sourcepub fn escape_default(&self) -> EscapeDefault<'_>
pub fn escape_default(&self) -> EscapeDefault<'_>
Return an iterator that escapes each char
in self
with
char::escape_default
.
sourcepub fn escape_unicode(&self) -> EscapeUnicode<'_>
pub fn escape_unicode(&self) -> EscapeUnicode<'_>
Return an iterator that escapes each char
in self
with
char::escape_unicode
.
Trait Implementations§
source§impl AsMut<TextSubstr> for TextSubstring
impl AsMut<TextSubstr> for TextSubstring
source§fn as_mut(&mut self) -> &mut TextSubstr
fn as_mut(&mut self) -> &mut TextSubstr
source§impl AsRef<[u8]> for TextSubstring
impl AsRef<[u8]> for TextSubstring
source§impl AsRef<OsStr> for TextSubstring
impl AsRef<OsStr> for TextSubstring
source§impl AsRef<Path> for TextSubstring
impl AsRef<Path> for TextSubstring
source§impl AsRef<TextSubstr> for TextSubstring
impl AsRef<TextSubstr> for TextSubstring
source§fn as_ref(&self) -> &TextSubstr
fn as_ref(&self) -> &TextSubstr
source§impl AsRef<str> for TextSubstring
impl AsRef<str> for TextSubstring
source§impl Borrow<TextSubstr> for TextSubstring
impl Borrow<TextSubstr> for TextSubstring
source§fn borrow(&self) -> &TextSubstr
fn borrow(&self) -> &TextSubstr
source§impl BorrowMut<TextSubstr> for TextSubstring
impl BorrowMut<TextSubstr> for TextSubstring
source§fn borrow_mut(&mut self) -> &mut TextSubstr
fn borrow_mut(&mut self) -> &mut TextSubstr
source§impl Clone for TextSubstring
impl Clone for TextSubstring
source§impl Debug for TextSubstring
impl Debug for TextSubstring
source§impl Default for TextSubstring
impl Default for TextSubstring
source§fn default() -> TextSubstring
fn default() -> TextSubstring
source§impl Deref for TextSubstring
impl Deref for TextSubstring
source§impl DerefMut for TextSubstring
impl DerefMut for TextSubstring
source§impl From<&TextSubstr> for TextSubstring
impl From<&TextSubstr> for TextSubstring
source§fn from(s: &TextSubstr) -> Self
fn from(s: &TextSubstr) -> Self
source§impl From<&TextSubstring> for TextSubstring
impl From<&TextSubstring> for TextSubstring
source§impl From<&mut TextSubstr> for TextSubstring
impl From<&mut TextSubstr> for TextSubstring
source§fn from(s: &mut TextSubstr) -> Self
fn from(s: &mut TextSubstr) -> Self
source§impl From<Box<TextSubstr>> for TextSubstring
impl From<Box<TextSubstr>> for TextSubstring
source§fn from(s: Box<TextSubstr>) -> Self
fn from(s: Box<TextSubstr>) -> Self
source§impl From<TextSubstring> for Box<TextSubstr>
impl From<TextSubstring> for Box<TextSubstr>
source§fn from(s: TextSubstring) -> Self
fn from(s: TextSubstring) -> Self
source§impl Hash for TextSubstring
impl Hash for TextSubstring
source§impl<'a> PartialEq<&'a TextStr> for TextSubstring
impl<'a> PartialEq<&'a TextStr> for TextSubstring
source§impl<'a> PartialEq<&'a TextSubstr> for TextSubstring
impl<'a> PartialEq<&'a TextSubstr> for TextSubstring
source§fn eq(&self, other: &&'a TextSubstr) -> bool
fn eq(&self, other: &&'a TextSubstr) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'a> PartialEq<&'a str> for TextSubstring
impl<'a> PartialEq<&'a str> for TextSubstring
source§impl<'a> PartialEq<Cow<'a, TextStr>> for TextSubstring
impl<'a> PartialEq<Cow<'a, TextStr>> for TextSubstring
source§impl<'a> PartialEq<Cow<'a, TextSubstr>> for TextSubstring
impl<'a> PartialEq<Cow<'a, TextSubstr>> for TextSubstring
source§impl<'a> PartialEq<Cow<'a, str>> for TextSubstring
impl<'a> PartialEq<Cow<'a, str>> for TextSubstring
source§impl PartialEq<String> for TextSubstring
impl PartialEq<String> for TextSubstring
source§impl PartialEq<TextStr> for TextSubstring
impl PartialEq<TextStr> for TextSubstring
source§impl PartialEq<TextSubstr> for TextSubstring
impl PartialEq<TextSubstr> for TextSubstring
source§fn eq(&self, other: &TextSubstr) -> bool
fn eq(&self, other: &TextSubstr) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'a> PartialEq<TextSubstring> for &'a TextStr
impl<'a> PartialEq<TextSubstring> for &'a TextStr
source§fn eq(&self, other: &TextSubstring) -> bool
fn eq(&self, other: &TextSubstring) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'a> PartialEq<TextSubstring> for &'a TextSubstr
impl<'a> PartialEq<TextSubstring> for &'a TextSubstr
source§fn eq(&self, other: &TextSubstring) -> bool
fn eq(&self, other: &TextSubstring) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'a> PartialEq<TextSubstring> for &'a str
impl<'a> PartialEq<TextSubstring> for &'a str
source§fn eq(&self, other: &TextSubstring) -> bool
fn eq(&self, other: &TextSubstring) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'a> PartialEq<TextSubstring> for Cow<'a, TextStr>
impl<'a> PartialEq<TextSubstring> for Cow<'a, TextStr>
source§fn eq(&self, other: &TextSubstring) -> bool
fn eq(&self, other: &TextSubstring) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'a> PartialEq<TextSubstring> for Cow<'a, TextSubstr>
impl<'a> PartialEq<TextSubstring> for Cow<'a, TextSubstr>
source§fn eq(&self, other: &TextSubstring) -> bool
fn eq(&self, other: &TextSubstring) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<'a> PartialEq<TextSubstring> for Cow<'a, str>
impl<'a> PartialEq<TextSubstring> for Cow<'a, str>
source§fn eq(&self, other: &TextSubstring) -> bool
fn eq(&self, other: &TextSubstring) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<TextSubstring> for String
impl PartialEq<TextSubstring> for String
source§fn eq(&self, other: &TextSubstring) -> bool
fn eq(&self, other: &TextSubstring) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<TextSubstring> for TextStr
impl PartialEq<TextSubstring> for TextStr
source§fn eq(&self, other: &TextSubstring) -> bool
fn eq(&self, other: &TextSubstring) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<TextSubstring> for TextSubstr
impl PartialEq<TextSubstring> for TextSubstr
source§fn eq(&self, other: &TextSubstring) -> bool
fn eq(&self, other: &TextSubstring) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<TextSubstring> for str
impl PartialEq<TextSubstring> for str
source§fn eq(&self, other: &TextSubstring) -> bool
fn eq(&self, other: &TextSubstring) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<str> for TextSubstring
impl PartialEq<str> for TextSubstring
source§impl PartialEq for TextSubstring
impl PartialEq for TextSubstring
source§fn eq(&self, other: &TextSubstring) -> bool
fn eq(&self, other: &TextSubstring) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for TextSubstring
impl PartialOrd for TextSubstring
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more