pub struct Span<H: Haystack>{ /* private fields */ }
Expand description
A span is a haystack coupled with the original range where the haystack is found.
It can be considered as a tuple (H, Range<H::Target::Index>)
where the range is guaranteed to be valid for the haystack.
§Examples
use pattern_3::Span;
let orig_str = "Hello世界";
let orig_span = Span::<&str>::from(orig_str);
// slice a span.
let span = unsafe { orig_span.slice_unchecked(3..8) };
// further slicing (note the range is relative to the original span)
let subspan = unsafe { span.slice_unchecked(4..8) };
// obtains the substring.
let substring = subspan.into();
assert_eq!(substring, "o世");
Visualizing the spans:
0 1 2 3 4 5 6 7 8 9 10 11
+---+---+---+---+---+---+---+---+---+---+---+
| H | e | l | l | o | U+4E16 | U+754C | orig_str
+---+---+---+---+---+---+---+---+---+---+---+
^___________________________________________^ orig_span = (orig_str, 0..11)
^___________________^ span = (orig_str, 3..8)
^_______________^ subspan = (orig_str, 4..8)
Implementations§
Source§impl<H: SharedHaystack> Span<H>
impl<H: SharedHaystack> Span<H>
Source§impl<H: Haystack> Span<H>
impl<H: Haystack> Span<H>
Sourcepub fn original_range(&self) -> Range<<H::Target as Hay>::Index>
pub fn original_range(&self) -> Range<<H::Target as Hay>::Index>
The range of the span, relative to the ultimate original haystack it was sliced from.
Sourcepub fn take(&mut self) -> Self
pub fn take(&mut self) -> Self
Returns this span by value, and replaces the original span by an empty span.
Sourcepub unsafe fn split_around(
self,
subrange: Range<<H::Target as Hay>::Index>,
) -> [Self; 3]
pub unsafe fn split_around( self, subrange: Range<<H::Target as Hay>::Index>, ) -> [Self; 3]
Splits this span into 3 spans around the given range.
§Safety
subrange
must be a valid range relative to self.borrow()
. A safe
usage is like:
let range = searcher.search(span.borrow())?;
let [left, middle, right] = unsafe { span.split_around(range) };
Trait Implementations§
Auto Trait Implementations§
impl<H> Freeze for Span<H>
impl<H> RefUnwindSafe for Span<H>
impl<H> Send for Span<H>
impl<H> Sync for Span<H>
impl<H> Unpin for Span<H>
impl<H> UnwindSafe for Span<H>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more