11//! The required interface for an underlying regex engine
2- use std:: { fmt, io} ;
2+ use std:: { fmt, io, ops :: Range } ;
33
4- /// An [Re ] is an underlying regular expression engine that can be used to match and extract text
5- /// as part of a structural regular expression.
4+ /// A [RegexEngine ] is an underlying regular expression engine that can be used to match and
5+ /// extract text as part of a structural regular expression.
66///
77/// The interface provided by this trait is intended for use by [Structex][crate::Structex] as an
88/// internal implementation detail and is likely not particularly useful outside of this crate.
99/// Implementors of this trait should pay particular attention to the requirements around
1010/// compilation in order for the resulting regex expressions to be compatible with this crate.
11- pub trait Re : Sized {
12- /// Error type that is returned by the [Re::compile] method when compilation of a given regular
13- /// expression fails. This will be wrapped into an [Error][crate::Error] when returned from
14- /// [Structex::compile][crate::Structex::new] or [StructexBuilder::build][crate::StructexBuilder::build].
11+ pub trait RegexEngine : Sized {
12+ /// Error type that is returned by the [RegexEngine::compile] method when compilation of a
13+ /// given regular expression fails. This will be wrapped into an [Error][crate::Error] when
14+ /// returned from [Structex::compile][crate::Structex::new] or
15+ /// [StructexBuilder::build][crate::StructexBuilder::build].
1516 type CompileError : std:: error:: Error + ' static ;
1617
1718 /// Attempt to compile the given regular expression for use inside of a [Structex][crate::Structex].
@@ -30,13 +31,13 @@ pub trait Re: Sized {
3031 fn compile ( re : & str ) -> Result < Self , Self :: CompileError > ;
3132}
3233
33- /// A haystack that can be searched by an [Re] regular expression engine .
34+ /// A haystack that can be searched by a [RegexEngine] .
3435///
3536/// Typically this is a [&str] but some engines may support richer types in order to provide
3637/// searching of streams or discontiguous inputs.
3738pub trait Haystack < R > : Sliceable
3839where
39- R : Re ,
40+ R : RegexEngine ,
4041{
4142 /// Returns true if there is a match for the regex between the given byte offsets in the haystack.
4243 ///
@@ -60,16 +61,14 @@ pub trait Sliceable: Writable + fmt::Debug + PartialEq + Eq + Copy {
6061 where
6162 Self : ' h ;
6263
63- /// A contiguous sub-section between the given bytes offsets.
64- ///
65- /// The given byte offsets from a half-open interval, inclusive of `from` but omitting `to`.
66- /// This is the same semantics as a normal Rust range `from..to`.
67- fn slice < ' h > ( & ' h self , from : usize , to : usize ) -> Self :: Slice < ' h > ;
64+ /// The contiguous sub-section of self that is denoted by the given byte [Range].
65+ fn slice < ' h > ( & ' h self , range : Range < usize > ) -> Self :: Slice < ' h > ;
6866
6967 /// The maximum length in bytes.
7068 ///
7169 /// This value will be used as the upper bound to extract slices when searching for matches. As
72- /// such, this value must be a valid `to` argument to the [slice][Sliceable::slice] method.
70+ /// such, this value must be a valid end to the `range` argument to the
71+ /// [slice][Sliceable::slice] method.
7372 fn max_len ( & self ) -> usize ;
7473}
7574
@@ -87,8 +86,8 @@ impl Sliceable for &str {
8786 where
8887 Self : ' h ;
8988
90- fn slice ( & self , from : usize , to : usize ) -> & str {
91- & self [ from..to ]
89+ fn slice ( & self , range : Range < usize > ) -> & str {
90+ & self [ range ]
9291 }
9392
9493 fn max_len ( & self ) -> usize {
@@ -111,8 +110,8 @@ impl Sliceable for &[u8] {
111110 where
112111 Self : ' h ;
113112
114- fn slice ( & self , from : usize , to : usize ) -> & [ u8 ] {
115- & self [ from..to ]
113+ fn slice ( & self , range : Range < usize > ) -> & [ u8 ] {
114+ & self [ range ]
116115 }
117116
118117 fn max_len ( & self ) -> usize {
@@ -129,8 +128,8 @@ impl Writable for &[u8] {
129128 }
130129}
131130
132- /// Represents the capture group positions for a single [Re ] match only in terms of byte offsets
133- /// into the original haystack.
131+ /// Represents the capture group positions for a single [RegexEngine ] match only in terms of byte
132+ /// offsets into the original haystack.
134133///
135134/// This is converted into a [Captures] by [TaggedCapturesIter][crate::TaggedCapturesIter] as
136135/// matches are returned during iteration.
@@ -165,8 +164,8 @@ impl RawCaptures {
165164 }
166165}
167166
168- /// Represents the capture group positions for a single [Re ] match in terms of byte offsets into
169- /// the original haystack that the match was run against.
167+ /// Represents the capture group positions for a single [RegexEngine ] match in terms of byte
168+ /// offsets into the original haystack that the match was run against.
170169#[ derive( Debug , PartialEq , Eq ) ]
171170pub struct Captures < H >
172171where
@@ -224,21 +223,21 @@ where
224223 pub fn match_text ( & self ) -> H :: Slice < ' _ > {
225224 let ( from, to) = self . get_match ( ) ;
226225
227- self . haystack . slice ( from, to)
226+ self . haystack . slice ( from.. to)
228227 }
229228
230229 /// The full text of the submatch, if present, in the original haystack.
231230 pub fn submatch_text ( & self , n : usize ) -> Option < H :: Slice < ' _ > > {
232231 let ( from, to) = self . get ( n) ?;
233232
234- Some ( self . haystack . slice ( from, to) )
233+ Some ( self . haystack . slice ( from.. to) )
235234 }
236235
237236 /// Iterate over all submatches starting with the full match.
238237 pub fn iter_submatches ( & self ) -> impl Iterator < Item = Option < H :: Slice < ' _ > > > {
239238 self . caps
240239 . iter ( )
241- . map ( |cap| cap. map ( |( from, to) | self . haystack . slice ( from, to) ) )
240+ . map ( |cap| cap. map ( |( from, to) | self . haystack . slice ( from.. to) ) )
242241 }
243242}
244243
@@ -247,7 +246,7 @@ mod impl_for_regex {
247246 use super :: * ;
248247 use regex:: { Error , Regex , RegexBuilder } ;
249248
250- impl Re for Regex {
249+ impl RegexEngine for Regex {
251250 type CompileError = Error ;
252251
253252 fn compile ( re : & str ) -> Result < Self , Self :: CompileError > {
0 commit comments