Struct 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§

Source§

impl<H: SharedHaystack> Span<H>
where H::Target: Hay,

Source

pub fn into_parts(self) -> (H, Range<<H::Target as Hay>::Index>)

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

Source

pub unsafe fn from_parts( haystack: H, range: Range<<H::Target as Hay>::Index>, ) -> Self

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

§Safety

The range must be a valid range relative to haystack.

Source§

impl<'h> Span<&'h str>

Source

pub fn as_bytes(self) -> Span<&'h [u8]>

Reinterprets the string span as a byte-array span.

Source§

impl<H: Haystack> Span<H>
where H::Target: Hay,

Source

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.

Source

pub fn borrow(&self) -> Span<&H::Target>

Borrows a shared span.

Source

pub fn is_empty(&self) -> bool

Checks whether this span is empty.

Source

pub fn take(&mut self) -> Self

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

Source

pub fn into(self) -> H

Slices the original haystack to the focused range.

Source

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

pub unsafe fn slice_unchecked( self, subrange: Range<<H::Target as Hay>::Index>, ) -> Self

Slices this span to the given range.

§Safety

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

Source§

impl<'h> Span<&'h Wtf8>

Source

pub fn as_bytes(self) -> Span<&'h [u8]>

Trait Implementations§

Source§

impl<H: Clone + Haystack> Clone for Span<H>
where H::Target: Hay,

Source§

fn clone(&self) -> Span<H>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<H: Debug + Haystack> Debug for Span<H>
where H::Target: Hay,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<H: Haystack> From<H> for Span<H>
where H::Target: Hay,

Creates a span which covers the entire haystack.

Source§

fn from(haystack: H) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<H> Freeze for Span<H>
where H: Freeze, <<H as Deref>::Target as Hay>::Index: Freeze,

§

impl<H> RefUnwindSafe for Span<H>
where H: RefUnwindSafe, <<H as Deref>::Target as Hay>::Index: RefUnwindSafe,

§

impl<H> Send for Span<H>
where H: Send, <<H as Deref>::Target as Hay>::Index: Send,

§

impl<H> Sync for Span<H>
where H: Sync, <<H as Deref>::Target as Hay>::Index: Sync,

§

impl<H> Unpin for Span<H>
where H: Unpin, <<H as Deref>::Target as Hay>::Index: Unpin,

§

impl<H> UnwindSafe for Span<H>
where H: UnwindSafe, <<H as Deref>::Target as Hay>::Index: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.