Skip to content

Commit 7f9367a

Browse files
committed
Rename Re to RegexEngine
1 parent 0cc4938 commit 7f9367a

File tree

7 files changed

+54
-55
lines changed

7 files changed

+54
-55
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub enum Error {
1818
/// A syntax error in the provided structural regular expression.
1919
Syntax(ParseError),
2020
/// An error that occurred during parsing or compiling a regular expression with the given
21-
/// [Re][re::Re] implementation.
21+
/// [RegexEngine][re::RegexEngine].
2222
Regex(Box<dyn error::Error>),
2323
}
2424

src/re.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
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.
3738
pub trait Haystack<R>: Sliceable
3839
where
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)]
171170
pub struct Captures<H>
172171
where
@@ -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> {

src/se/extract.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
compile::Inst,
3-
re::{Haystack, RawCaptures, Re},
3+
re::{Haystack, RawCaptures, RegexEngine},
44
se::{Dot, Inner, MatchesInner, TaggedCaptures},
55
};
66
use std::sync::Arc;
@@ -45,7 +45,7 @@ impl Extract {
4545

4646
pub(super) struct Iter<'s, R, H>
4747
where
48-
R: Re,
48+
R: RegexEngine,
4949
H: Haystack<R>,
5050
{
5151
haystack: H,
@@ -63,7 +63,7 @@ where
6363

6464
impl<'s, R, H> Iter<'s, R, H>
6565
where
66-
R: Re,
66+
R: RegexEngine,
6767
H: Haystack<R>,
6868
{
6969
pub fn new(haystack: H, parent: Dot, ext: &'s Extract, inner: Arc<Inner<R>>) -> Self {
@@ -100,7 +100,7 @@ where
100100

101101
impl<'s, R, H> Iterator for Iter<'s, R, H>
102102
where
103-
R: Re,
103+
R: RegexEngine,
104104
H: Haystack<R>,
105105
{
106106
type Item = TaggedCaptures<H>;

src/se/guard.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
compile::Inst,
3-
re::{Haystack, Re},
3+
re::{Haystack, RegexEngine},
44
se::{Dot, Inner, MatchesInner},
55
};
66
use std::sync::Arc;
@@ -49,7 +49,7 @@ impl Guard {
4949
inner: Arc<Inner<R>>,
5050
) -> Option<MatchesInner<'s, R, H>>
5151
where
52-
R: Re,
52+
R: RegexEngine,
5353
H: Haystack<R>,
5454
{
5555
let (from, to) = dot.loc();

src/se/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
Error,
55
compile::{Compiler, Inst},
6-
re::{Captures, Haystack, RawCaptures, Re, Sliceable},
6+
re::{Captures, Haystack, RawCaptures, RegexEngine, Sliceable},
77
};
88
use std::{fmt, ops::Deref, sync::Arc};
99

@@ -25,15 +25,15 @@ pub(crate) use narrow::Narrow;
2525
#[derive(Clone)]
2626
pub struct Structex<R>
2727
where
28-
R: Re,
28+
R: RegexEngine,
2929
{
3030
raw: Arc<str>,
3131
inner: Arc<Inner<R>>,
3232
}
3333

3434
impl<R> fmt::Debug for Structex<R>
3535
where
36-
R: Re,
36+
R: RegexEngine,
3737
{
3838
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3939
f.debug_tuple("Structex").field(&self.raw).finish()
@@ -42,7 +42,7 @@ where
4242

4343
impl<R> fmt::Display for Structex<R>
4444
where
45-
R: Re,
45+
R: RegexEngine,
4646
{
4747
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4848
write!(f, "Structex({})", self.raw)
@@ -51,7 +51,7 @@ where
5151

5252
impl<R> Structex<R>
5353
where
54-
R: Re,
54+
R: RegexEngine,
5555
{
5656
/// Compiles a structural regular expression. Once compiled it may be used repeatedly and cloned
5757
/// cheaply, but note that compilation can be an expensive process so [Structex] instances
@@ -379,7 +379,7 @@ impl StructexBuilder {
379379
/// If the expression was invalid, an error is returned.
380380
pub fn build<R>(self) -> Result<Structex<R>, Error>
381381
where
382-
R: Re,
382+
R: RegexEngine,
383383
{
384384
let mut c = Compiler {
385385
require_actions: self.require_actions,
@@ -422,7 +422,7 @@ impl StructexBuilder {
422422

423423
pub(super) struct Inner<R>
424424
where
425-
R: Re,
425+
R: RegexEngine,
426426
{
427427
pub(super) inst: Inst,
428428
pub(super) re: Vec<R>,
@@ -564,15 +564,15 @@ impl Action {
564564
/// This iterator is created by [Structex::iter_tagged_captures].
565565
pub struct TaggedCapturesIter<'s, R, H>
566566
where
567-
R: Re,
567+
R: RegexEngine,
568568
H: Haystack<R>,
569569
{
570570
inner: Option<MatchesInner<'s, R, H>>,
571571
}
572572

573573
impl<'s, R, H> TaggedCapturesIter<'s, R, H>
574574
where
575-
R: Re,
575+
R: RegexEngine,
576576
H: Haystack<R>,
577577
{
578578
fn new(inst: &'s Inst, inner: Arc<Inner<R>>, haystack: H) -> Self {
@@ -592,7 +592,7 @@ where
592592

593593
impl<'s, R, H> Iterator for TaggedCapturesIter<'s, R, H>
594594
where
595-
R: Re,
595+
R: RegexEngine,
596596
H: Haystack<R>,
597597
{
598598
type Item = TaggedCaptures<H>;
@@ -604,7 +604,7 @@ where
604604

605605
enum MatchesInner<'s, R, H>
606606
where
607-
R: Re,
607+
R: RegexEngine,
608608
H: Haystack<R>,
609609
{
610610
Extract(extract::Iter<'s, R, H>),
@@ -614,7 +614,7 @@ where
614614

615615
impl<'s, R, H> MatchesInner<'s, R, H>
616616
where
617-
R: Re,
617+
R: RegexEngine,
618618
H: Haystack<R>,
619619
{
620620
fn new(inst: &'s Inst, inner: Arc<Inner<R>>, haystack: H, dot: Dot) -> Option<Self> {
@@ -646,7 +646,7 @@ where
646646

647647
impl<'s, R, H> Iterator for MatchesInner<'s, R, H>
648648
where
649-
R: Re,
649+
R: RegexEngine,
650650
H: Haystack<R>,
651651
{
652652
type Item = TaggedCaptures<H>;

src/se/narrow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
compile::Inst,
3-
re::{Haystack, Re},
3+
re::{Haystack, RegexEngine},
44
se::{Dot, Inner, MatchesInner},
55
};
66
use std::sync::Arc;
@@ -19,7 +19,7 @@ impl Narrow {
1919
inner: Arc<Inner<R>>,
2020
) -> Option<MatchesInner<'s, R, H>>
2121
where
22-
R: Re,
22+
R: RegexEngine,
2323
H: Haystack<R>,
2424
{
2525
let (from, to) = dot.loc();

src/se/parallel.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
use crate::{
22
compile::Inst,
3-
re::{Haystack, Re},
3+
re::{Haystack, RegexEngine},
44
se::{Dot, Inner, MatchesInner, TaggedCaptures},
55
};
66
use std::sync::Arc;
77

88
pub(super) struct Iter<'s, R, H>
99
where
10-
R: Re,
10+
R: RegexEngine,
1111
H: Haystack<R>,
1212
{
1313
branches: Vec<Branch<'s, R, H>>,
1414
}
1515

1616
impl<'s, R, H> Iter<'s, R, H>
1717
where
18-
R: Re,
18+
R: RegexEngine,
1919
H: Haystack<R>,
2020
{
2121
pub fn new(haystack: H, dot: Dot, branches: &'s [Inst], inner: Arc<Inner<R>>) -> Self {
@@ -33,7 +33,7 @@ where
3333

3434
impl<'s, R, H> Iterator for Iter<'s, R, H>
3535
where
36-
R: Re,
36+
R: RegexEngine,
3737
H: Haystack<R>,
3838
{
3939
type Item = TaggedCaptures<H>;
@@ -67,7 +67,7 @@ where
6767

6868
struct Branch<'s, R, H>
6969
where
70-
R: Re,
70+
R: RegexEngine,
7171
H: Haystack<R>,
7272
{
7373
held: Option<TaggedCaptures<H>>,
@@ -76,7 +76,7 @@ where
7676

7777
impl<'s, R, H> Branch<'s, R, H>
7878
where
79-
R: Re,
79+
R: RegexEngine,
8080
H: Haystack<R>,
8181
{
8282
fn get_match(&self) -> Option<(usize, usize)> {

0 commit comments

Comments
 (0)