Struct pattern_3::haystack::Span

source ·
pub struct Span<H: Haystack>where
    H::Target: Hay,
{ /* 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§

Decomposes this span into the original haystack, and the range it focuses on.

Creates a span from a haystack, and a range it should focus on.

Safety

The range must be a valid range relative to haystack.

Reinterprets the string span as a byte-array span.

The range of the span, relative to the ultimate original haystack it was sliced from.

Borrows a shared span.

Checks whether this span is empty.

Returns this span by value, and replaces the original span by an empty span.

Slices the original haystack to the focused range.

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) };

Slices this span to the given range.

Safety

subrange must be a valid range relative to self.borrow().

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Creates a span which covers the entire haystack.

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.