Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/re_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::ops::Index;
use std::ops::{Index, Range};
use std::str::FromStr;
use std::sync::Arc;

Expand Down Expand Up @@ -36,10 +36,17 @@ impl<'t> Match<'t> {
self.end
}

/// Returns the range over the starting and ending byte offsets of the match
/// in the haystack.
#[inline]
pub fn range(&self) -> Range<usize> {
self.start..self.end
}

/// Returns the matched text.
#[inline]
pub fn as_bytes(&self) -> &'t [u8] {
&self.text[self.start..self.end]
&self.text[self.range()]
}

/// Creates a new match from the given haystack and byte offsets.
Expand All @@ -49,6 +56,12 @@ impl<'t> Match<'t> {
}
}

impl<'t> From<Match<'t>> for Range<usize> {
fn from(m: Match<'t>) -> Range<usize> {
m.range()
}
}

/// A compiled regular expression for matching arbitrary bytes.
///
/// It can be used to search, split or replace text. All searching is done with
Expand Down
17 changes: 15 additions & 2 deletions src/re_unicode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt;
use std::ops::Index;
use std::ops::{Index, Range};
use std::str::FromStr;
use std::sync::Arc;

Expand Down Expand Up @@ -45,10 +45,17 @@ impl<'t> Match<'t> {
self.end
}

/// Returns the range over the starting and ending byte offsets of the match
/// in the haystack.
#[inline]
pub fn range(&self) -> Range<usize> {
self.start..self.end
}

/// Returns the matched text.
#[inline]
pub fn as_str(&self) -> &'t str {
&self.text[self.start..self.end]
&self.text[self.range()]
}

/// Creates a new match from the given haystack and byte offsets.
Expand All @@ -64,6 +71,12 @@ impl<'t> From<Match<'t>> for &'t str {
}
}

impl<'t> From<Match<'t>> for Range<usize> {
fn from(m: Match<'t>) -> Range<usize> {
m.range()
}
}

/// A compiled regular expression for matching Unicode strings.
///
/// It is represented as either a sequence of bytecode instructions (dynamic)
Expand Down