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>where
H::Target: Hay,
impl<H: SharedHaystack> Span<H>where
H::Target: Hay,
source§impl<H: Haystack> Span<H>where
H::Target: Hay,
impl<H: Haystack> Span<H>where
H::Target: Hay,
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) };