Repository navigation
Expand file tree
/
Copy pathsearch.rs
More file actions
2008 lines (1906 loc) · 72.9 KB
/
Copy pathsearch.rs
File metadata and controls
2008 lines (1906 loc) · 72.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
Types and routines that support the search APIs of most regex engines.
This sub-module isn't exposed directly, but rather, its contents are exported
at the crate root due to the universality of most of the types and routines in
this module.
*/
use core::ops::{Range, RangeBounds};
use crate::util::{escape::DebugByte, primitives::PatternID, utf8};
/// The parameters for a regex search including the haystack to search.
///
/// It turns out that regex searches have a few parameters, and in most cases,
/// those parameters have defaults that work in the vast majority of cases.
/// This `Input` type exists to make that common case seamless while also
/// providing an avenue for changing the parameters of a search. In particular,
/// this type enables doing so without a combinatorial explosion of different
/// methods and/or superfluous parameters in the common cases.
///
/// An `Input` permits configuring the following things:
///
/// * Search only a substring of a haystack, while taking the broader context
/// into account for resolving look-around assertions.
/// * Indicating whether to search for all patterns in a regex, or to
/// only search for one pattern in particular.
/// * Whether to perform an anchored on unanchored search.
/// * Whether to report a match as early as possible.
///
/// All of these parameters, except for the haystack, have sensible default
/// values. This means that the minimal search configuration is simply a call
/// to [`Input::new`] with your haystack. Setting any other parameter is
/// optional.
///
/// Moreover, for any `H` that implements `AsRef<[u8]>`, there exists a
/// `From<H> for Input` implementation. This is useful because many of the
/// search APIs in this crate accept an `Into<Input>`. This means you can
/// provide string or byte strings to these routines directly, and they'll
/// automatically get converted into an `Input` for you.
///
/// The lifetime parameter `'h` refers to the lifetime of the haystack.
///
/// # Organization
///
/// The API of `Input` is split into a few different parts:
///
/// * A builder-like API that transforms a `Input` by value. Examples:
/// [`Input::span`] and [`Input::anchored`].
/// * A setter API that permits mutating parameters in place. Examples:
/// [`Input::set_span`] and [`Input::set_anchored`].
/// * A getter API that permits retrieving any of the search parameters.
/// Examples: [`Input::get_span`] and [`Input::get_anchored`].
/// * A few convenience getter routines that don't conform to the above naming
/// pattern due to how common they are. Examples: [`Input::haystack`],
/// [`Input::start`] and [`Input::end`].
/// * Miscellaneous predicates and other helper routines that are useful
/// in some contexts. Examples: [`Input::is_char_boundary`].
///
/// A `Input` exposes so much because it is meant to be used by both callers of
/// regex engines _and_ implementors of regex engines. A constraining factor is
/// that regex engines should accept a `&Input` as its lowest level API, which
/// means that implementors should only use the "getter" APIs of a `Input`.
///
/// # Valid bounds and search termination
///
/// An `Input` permits setting the bounds of a search via either
/// [`Input::span`] or [`Input::range`]. The bounds set must be valid, or
/// else a panic will occur. Bounds are valid if and only if:
///
/// * The bounds represent a valid range into the input's haystack.
/// * **or** the end bound is a valid ending bound for the haystack *and*
/// the start bound is exactly one greater than the start bound.
///
/// In the latter case, [`Input::is_done`] will return true and indicates any
/// search receiving such an input should immediately return with no match.
///
/// Note that while `Input` is used for reverse searches in this crate, the
/// `Input::is_done` predicate assumes a forward search. Because unsigned
/// offsets are used internally, there is no way to tell from only the offsets
/// whether a reverse search is done or not.
///
/// # Regex engine support
///
/// Any regex engine accepting an `Input` must support at least the following
/// things:
///
/// * Searching a `&[u8]` for matches.
/// * Searching a substring of `&[u8]` for a match, such that any match
/// reported must appear entirely within that substring.
/// * For a forwards search, a match should never be reported when
/// [`Input::is_done`] returns true. (For reverse searches, termination should
/// be handled outside of `Input`.)
///
/// Supporting other aspects of an `Input` are optional, but regex engines
/// should handle aspects they don't support gracefully. How this is done is
/// generally up to the regex engine. This crate generally treats unsupported
/// anchored modes as an error to report for example, but for simplicity, in
/// the meta regex engine, trying to search with an invalid pattern ID just
/// results in no match being reported.
#[derive(Clone)]
pub struct Input<'h> {
haystack: &'h [u8],
span: Span,
anchored: Anchored,
earliest: bool,
}
impl<'h> Input<'h> {
/// Create a new search configuration for the given haystack.
#[inline]
pub fn new<H: ?Sized + AsRef<[u8]>>(haystack: &'h H) -> Input<'h> {
// Perform only one call to `haystack.as_ref()` to protect from incorrect
// implementations that return different values from multiple calls.
// This is important because there's code that relies on `span` not being
// out of bounds with respect to the stored `haystack`.
let haystack = haystack.as_ref();
Input {
haystack,
span: Span { start: 0, end: haystack.len() },
anchored: Anchored::No,
earliest: false,
}
}
/// Set the span for this search.
///
/// This routine does not panic if the span given is not a valid range for
/// this search's haystack. If this search is run with an invalid range,
/// then the most likely outcome is that the actual search execution will
/// panic.
///
/// This routine is generic over how a span is provided. While
/// a [`Span`] may be given directly, one may also provide a
/// `std::ops::Range<usize>`. To provide anything supported by range
/// syntax, use the [`Input::range`] method.
///
/// The default span is the entire haystack.
///
/// Note that [`Input::range`] overrides this method and vice versa.
///
/// # Panics
///
/// This panics if the given span does not correspond to valid bounds in
/// the haystack or the termination of a search.
///
/// # Example
///
/// This example shows how the span of the search can impact whether a
/// match is reported or not. This is particularly relevant for look-around
/// operators, which might take things outside of the span into account
/// when determining whether they match.
///
/// ```
/// # if cfg!(miri) { return Ok(()); } // miri takes too long
/// use regex_automata::{
/// nfa::thompson::pikevm::PikeVM,
/// Match, Input,
/// };
///
/// // Look for 'at', but as a distinct word.
/// let re = PikeVM::new(r"\bat\b")?;
/// let mut cache = re.create_cache();
/// let mut caps = re.create_captures();
///
/// // Our haystack contains 'at', but not as a distinct word.
/// let haystack = "batter";
///
/// // A standard search finds nothing, as expected.
/// let input = Input::new(haystack);
/// re.search(&mut cache, &input, &mut caps);
/// assert_eq!(None, caps.get_match());
///
/// // But if we wanted to search starting at position '1', we might
/// // slice the haystack. If we do this, it's impossible for the \b
/// // anchors to take the surrounding context into account! And thus,
/// // a match is produced.
/// let input = Input::new(&haystack[1..3]);
/// re.search(&mut cache, &input, &mut caps);
/// assert_eq!(Some(Match::must(0, 0..2)), caps.get_match());
///
/// // But if we specify the span of the search instead of slicing the
/// // haystack, then the regex engine can "see" outside of the span
/// // and resolve the anchors correctly.
/// let input = Input::new(haystack).span(1..3);
/// re.search(&mut cache, &input, &mut caps);
/// assert_eq!(None, caps.get_match());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
/// This may seem a little ham-fisted, but this scenario tends to come up
/// if some other regex engine found the match span and now you need to
/// re-process that span to look for capturing groups. (e.g., Run a faster
/// DFA first, find a match, then run the PikeVM on just the match span to
/// resolve capturing groups.) In order to implement that sort of logic
/// correctly, you need to set the span on the search instead of slicing
/// the haystack directly.
///
/// The other advantage of using this routine to specify the bounds of the
/// search is that the match offsets are still reported in terms of the
/// original haystack. For example, the second search in the example above
/// reported a match at position `0`, even though `at` starts at offset
/// `1` because we sliced the haystack.
#[inline]
pub fn span<S: Into<Span>>(mut self, span: S) -> Input<'h> {
self.set_span(span);
self
}
/// Like `Input::span`, but accepts any range instead.
///
/// This routine does not panic if the range given is not a valid range for
/// this search's haystack. If this search is run with an invalid range,
/// then the most likely outcome is that the actual search execution will
/// panic.
///
/// The default range is the entire haystack.
///
/// Note that [`Input::span`] overrides this method and vice versa.
///
/// # Panics
///
/// This routine will panic if the given range could not be converted
/// to a valid [`Range`]. For example, this would panic when given
/// `0..=usize::MAX` since it cannot be represented using a half-open
/// interval in terms of `usize`.
///
/// This also panics if the given range does not correspond to valid bounds
/// in the haystack or the termination of a search.
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let input = Input::new("foobar");
/// assert_eq!(0..6, input.get_range());
///
/// let input = Input::new("foobar").range(2..=4);
/// assert_eq!(2..5, input.get_range());
/// ```
#[inline]
pub fn range<R: RangeBounds<usize>>(mut self, range: R) -> Input<'h> {
self.set_range(range);
self
}
/// Sets the anchor mode of a search.
///
/// When a search is anchored (so that's [`Anchored::Yes`] or
/// [`Anchored::Pattern`]), a match must begin at the start of a search.
/// When a search is not anchored (that's [`Anchored::No`]), regex engines
/// will behave as if the pattern started with a `(?s-u:.)*?`. This prefix
/// permits a match to appear anywhere.
///
/// By default, the anchored mode is [`Anchored::No`].
///
/// **WARNING:** this is subtly different than using a `^` at the start of
/// your regex. A `^` forces a regex to match exclusively at the start of
/// a haystack, regardless of where you begin your search. In contrast,
/// anchoring a search will allow your regex to match anywhere in your
/// haystack, but the match must start at the beginning of a search.
///
/// For example, consider the haystack `aba` and the following searches:
///
/// 1. The regex `^a` is compiled with `Anchored::No` and searches `aba`
/// starting at position `2`. Since `^` requires the match to start at
/// the beginning of the haystack and `2 > 0`, no match is found.
/// 2. The regex `a` is compiled with `Anchored::Yes` and searches `aba`
/// starting at position `2`. This reports a match at `[2, 3]` since
/// the match starts where the search started. Since there is no `^`,
/// there is no requirement for the match to start at the beginning of
/// the haystack.
/// 3. The regex `a` is compiled with `Anchored::Yes` and searches `aba`
/// starting at position `1`. Since `b` corresponds to position `1` and
/// since the search is anchored, it finds no match. While the regex
/// matches at other positions, configuring the search to be anchored
/// requires that it only report a match that begins at the same offset
/// as the beginning of the search.
/// 4. The regex `a` is compiled with `Anchored::No` and searches `aba`
/// starting at position `1`. Since the search is not anchored and
/// the regex does not start with `^`, the search executes as if there
/// is a `(?s:.)*?` prefix that permits it to match anywhere. Thus, it
/// reports a match at `[2, 3]`.
///
/// Note that the [`Anchored::Pattern`] mode is like `Anchored::Yes`,
/// except it only reports matches for a particular pattern.
///
/// # Example
///
/// This demonstrates the differences between an anchored search and
/// a pattern that begins with `^` (as described in the above warning
/// message).
///
/// ```
/// use regex_automata::{
/// nfa::thompson::pikevm::PikeVM,
/// Anchored, Match, Input,
/// };
///
/// let haystack = "aba";
///
/// let re = PikeVM::new(r"^a")?;
/// let (mut cache, mut caps) = (re.create_cache(), re.create_captures());
/// let input = Input::new(haystack).span(2..3).anchored(Anchored::No);
/// re.search(&mut cache, &input, &mut caps);
/// // No match is found because 2 is not the beginning of the haystack,
/// // which is what ^ requires.
/// assert_eq!(None, caps.get_match());
///
/// let re = PikeVM::new(r"a")?;
/// let (mut cache, mut caps) = (re.create_cache(), re.create_captures());
/// let input = Input::new(haystack).span(2..3).anchored(Anchored::Yes);
/// re.search(&mut cache, &input, &mut caps);
/// // An anchored search can still match anywhere in the haystack, it just
/// // must begin at the start of the search which is '2' in this case.
/// assert_eq!(Some(Match::must(0, 2..3)), caps.get_match());
///
/// let re = PikeVM::new(r"a")?;
/// let (mut cache, mut caps) = (re.create_cache(), re.create_captures());
/// let input = Input::new(haystack).span(1..3).anchored(Anchored::Yes);
/// re.search(&mut cache, &input, &mut caps);
/// // No match is found since we start searching at offset 1 which
/// // corresponds to 'b'. Since there is no '(?s:.)*?' prefix, no match
/// // is found.
/// assert_eq!(None, caps.get_match());
///
/// let re = PikeVM::new(r"a")?;
/// let (mut cache, mut caps) = (re.create_cache(), re.create_captures());
/// let input = Input::new(haystack).span(1..3).anchored(Anchored::No);
/// re.search(&mut cache, &input, &mut caps);
/// // Since anchored=no, an implicit '(?s:.)*?' prefix was added to the
/// // pattern. Even though the search starts at 'b', the 'match anything'
/// // prefix allows the search to match 'a'.
/// let expected = Some(Match::must(0, 2..3));
/// assert_eq!(expected, caps.get_match());
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn anchored(mut self, mode: Anchored) -> Input<'h> {
self.set_anchored(mode);
self
}
/// Whether to execute an "earliest" search or not.
///
/// When running a non-overlapping search, an "earliest" search will return
/// the match location as early as possible. For example, given a pattern
/// of `foo[0-9]+` and a haystack of `foo12345`, a normal leftmost search
/// will return `foo12345` as a match. But an "earliest" search for regex
/// engines that support "earliest" semantics will return `foo1` as a
/// match, since as soon as the first digit following `foo` is seen, it is
/// known to have found a match.
///
/// Note that "earliest" semantics generally depend on the regex engine.
/// Different regex engines may determine there is a match at different
/// points. So there is no guarantee that "earliest" matches will always
/// return the same offsets for all regex engines. The "earliest" notion
/// is really about when the particular regex engine determines there is
/// a match rather than a consistent semantic unto itself. This is often
/// useful for implementing "did a match occur or not" predicates, but
/// sometimes the offset is useful as well.
///
/// This is disabled by default.
///
/// # Example
///
/// This example shows the difference between "earliest" searching and
/// normal searching.
///
/// ```
/// use regex_automata::{nfa::thompson::pikevm::PikeVM, Match, Input};
///
/// let re = PikeVM::new(r"foo[0-9]+")?;
/// let mut cache = re.create_cache();
/// let mut caps = re.create_captures();
///
/// // A normal search implements greediness like you expect.
/// let input = Input::new("foo12345");
/// re.search(&mut cache, &input, &mut caps);
/// assert_eq!(Some(Match::must(0, 0..8)), caps.get_match());
///
/// // When 'earliest' is enabled and the regex engine supports
/// // it, the search will bail once it knows a match has been
/// // found.
/// let input = Input::new("foo12345").earliest(true);
/// re.search(&mut cache, &input, &mut caps);
/// assert_eq!(Some(Match::must(0, 0..4)), caps.get_match());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[inline]
pub fn earliest(mut self, yes: bool) -> Input<'h> {
self.set_earliest(yes);
self
}
/// Set the span for this search configuration.
///
/// This is like the [`Input::span`] method, except this mutates the
/// span in place.
///
/// This routine is generic over how a span is provided. While
/// a [`Span`] may be given directly, one may also provide a
/// `std::ops::Range<usize>`.
///
/// # Panics
///
/// This panics if the given span does not correspond to valid bounds in
/// the haystack or the termination of a search.
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let mut input = Input::new("foobar");
/// assert_eq!(0..6, input.get_range());
/// input.set_span(2..4);
/// assert_eq!(2..4, input.get_range());
/// ```
#[inline]
pub fn set_span<S: Into<Span>>(&mut self, span: S) {
let span = span.into();
assert!(
span.end <= self.haystack.len()
&& span.start <= span.end.wrapping_add(1),
"invalid span {:?} for haystack of length {}",
span,
self.haystack.len(),
);
self.span = span;
}
/// Set the span for this search configuration given any range.
///
/// This is like the [`Input::range`] method, except this mutates the
/// span in place.
///
/// This routine does not panic if the range given is not a valid range for
/// this search's haystack. If this search is run with an invalid range,
/// then the most likely outcome is that the actual search execution will
/// panic.
///
/// # Panics
///
/// This routine will panic if the given range could not be converted
/// to a valid [`Range`]. For example, this would panic when given
/// `0..=usize::MAX` since it cannot be represented using a half-open
/// interval in terms of `usize`.
///
/// This also panics if the given span does not correspond to valid bounds
/// in the haystack or the termination of a search.
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let mut input = Input::new("foobar");
/// assert_eq!(0..6, input.get_range());
/// input.set_range(2..=4);
/// assert_eq!(2..5, input.get_range());
/// ```
#[inline]
pub fn set_range<R: RangeBounds<usize>>(&mut self, range: R) {
use core::ops::Bound;
// It's a little weird to convert ranges into spans, and then spans
// back into ranges when we actually slice the haystack. Because
// of that process, we always represent everything as a half-open
// internal. Therefore, handling things like m..=n is a little awkward.
let start = match range.start_bound() {
Bound::Included(&i) => i,
// Can this case ever happen? Range syntax doesn't support it...
Bound::Excluded(&i) => i.checked_add(1).unwrap(),
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
Bound::Included(&i) => i.checked_add(1).unwrap(),
Bound::Excluded(&i) => i,
Bound::Unbounded => self.haystack().len(),
};
self.set_span(Span { start, end });
}
/// Set the starting offset for the span for this search configuration.
///
/// This is a convenience routine for only mutating the start of a span
/// without having to set the entire span.
///
/// # Panics
///
/// This panics if the span resulting from the new start position does not
/// correspond to valid bounds in the haystack or the termination of a
/// search.
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let mut input = Input::new("foobar");
/// assert_eq!(0..6, input.get_range());
/// input.set_start(5);
/// assert_eq!(5..6, input.get_range());
/// ```
#[inline]
pub fn set_start(&mut self, start: usize) {
self.set_span(Span { start, ..self.get_span() });
}
/// Set the ending offset for the span for this search configuration.
///
/// This is a convenience routine for only mutating the end of a span
/// without having to set the entire span.
///
/// # Panics
///
/// This panics if the span resulting from the new end position does not
/// correspond to valid bounds in the haystack or the termination of a
/// search.
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let mut input = Input::new("foobar");
/// assert_eq!(0..6, input.get_range());
/// input.set_end(5);
/// assert_eq!(0..5, input.get_range());
/// ```
#[inline]
pub fn set_end(&mut self, end: usize) {
self.set_span(Span { end, ..self.get_span() });
}
/// Set the anchor mode of a search.
///
/// This is like [`Input::anchored`], except it mutates the search
/// configuration in place.
///
/// # Example
///
/// ```
/// use regex_automata::{Anchored, Input, PatternID};
///
/// let mut input = Input::new("foobar");
/// assert_eq!(Anchored::No, input.get_anchored());
///
/// let pid = PatternID::must(5);
/// input.set_anchored(Anchored::Pattern(pid));
/// assert_eq!(Anchored::Pattern(pid), input.get_anchored());
/// ```
#[inline]
pub fn set_anchored(&mut self, mode: Anchored) {
self.anchored = mode;
}
/// Set whether the search should execute in "earliest" mode or not.
///
/// This is like [`Input::earliest`], except it mutates the search
/// configuration in place.
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let mut input = Input::new("foobar");
/// assert!(!input.get_earliest());
/// input.set_earliest(true);
/// assert!(input.get_earliest());
/// ```
#[inline]
pub fn set_earliest(&mut self, yes: bool) {
self.earliest = yes;
}
/// Return a borrow of the underlying haystack as a slice of bytes.
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let input = Input::new("foobar");
/// assert_eq!(b"foobar", input.haystack());
/// ```
#[inline]
pub fn haystack(&self) -> &'h [u8] {
self.haystack
}
/// Return the start position of this search.
///
/// This is a convenience routine for `search.get_span().start()`.
///
/// When [`Input::is_done`] is `false`, this is guaranteed to return
/// an offset that is less than or equal to [`Input::end`]. Otherwise,
/// the offset is one greater than [`Input::end`].
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let input = Input::new("foobar");
/// assert_eq!(0, input.start());
///
/// let input = Input::new("foobar").span(2..4);
/// assert_eq!(2, input.start());
/// ```
#[inline]
pub fn start(&self) -> usize {
self.get_span().start
}
/// Return the end position of this search.
///
/// This is a convenience routine for `search.get_span().end()`.
///
/// This is guaranteed to return an offset that is a valid exclusive end
/// bound for this input's haystack.
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let input = Input::new("foobar");
/// assert_eq!(6, input.end());
///
/// let input = Input::new("foobar").span(2..4);
/// assert_eq!(4, input.end());
/// ```
#[inline]
pub fn end(&self) -> usize {
self.get_span().end
}
/// Return the span for this search configuration.
///
/// If one was not explicitly set, then the span corresponds to the entire
/// range of the haystack.
///
/// When [`Input::is_done`] is `false`, the span returned is guaranteed
/// to correspond to valid bounds for this input's haystack.
///
/// # Example
///
/// ```
/// use regex_automata::{Input, Span};
///
/// let input = Input::new("foobar");
/// assert_eq!(Span { start: 0, end: 6 }, input.get_span());
/// ```
#[inline]
pub fn get_span(&self) -> Span {
self.span
}
/// Return the span as a range for this search configuration.
///
/// If one was not explicitly set, then the span corresponds to the entire
/// range of the haystack.
///
/// When [`Input::is_done`] is `false`, the range returned is guaranteed
/// to correspond to valid bounds for this input's haystack.
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let input = Input::new("foobar");
/// assert_eq!(0..6, input.get_range());
/// ```
#[inline]
pub fn get_range(&self) -> Range<usize> {
self.get_span().range()
}
/// Return the anchored mode for this search configuration.
///
/// If no anchored mode was set, then it defaults to [`Anchored::No`].
///
/// # Example
///
/// ```
/// use regex_automata::{Anchored, Input, PatternID};
///
/// let mut input = Input::new("foobar");
/// assert_eq!(Anchored::No, input.get_anchored());
///
/// let pid = PatternID::must(5);
/// input.set_anchored(Anchored::Pattern(pid));
/// assert_eq!(Anchored::Pattern(pid), input.get_anchored());
/// ```
#[inline]
pub fn get_anchored(&self) -> Anchored {
self.anchored
}
/// Return whether this search should execute in "earliest" mode.
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let input = Input::new("foobar");
/// assert!(!input.get_earliest());
/// ```
#[inline]
pub fn get_earliest(&self) -> bool {
self.earliest
}
/// Return true if and only if this search can never return any other
/// matches.
///
/// This occurs when the start position of this search is greater than the
/// end position of the search.
///
/// # Example
///
/// ```
/// use regex_automata::Input;
///
/// let mut input = Input::new("foobar");
/// assert!(!input.is_done());
/// input.set_start(6);
/// assert!(!input.is_done());
/// input.set_start(7);
/// assert!(input.is_done());
/// ```
#[inline]
pub fn is_done(&self) -> bool {
self.get_span().start > self.get_span().end
}
/// Returns true if and only if the given offset in this search's haystack
/// falls on a valid UTF-8 encoded codepoint boundary.
///
/// If the haystack is not valid UTF-8, then the behavior of this routine
/// is unspecified.
///
/// # Example
///
/// This shows where codepoint boundaries do and don't exist in valid
/// UTF-8.
///
/// ```
/// use regex_automata::Input;
///
/// let input = Input::new("☃");
/// assert!(input.is_char_boundary(0));
/// assert!(!input.is_char_boundary(1));
/// assert!(!input.is_char_boundary(2));
/// assert!(input.is_char_boundary(3));
/// assert!(!input.is_char_boundary(4));
/// ```
#[inline]
pub fn is_char_boundary(&self, offset: usize) -> bool {
utf8::is_boundary(self.haystack(), offset)
}
}
impl<'h> core::fmt::Debug for Input<'h> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
use crate::util::escape::DebugHaystack;
f.debug_struct("Input")
.field("haystack", &DebugHaystack(self.haystack()))
.field("span", &self.span)
.field("anchored", &self.anchored)
.field("earliest", &self.earliest)
.finish()
}
}
impl<'h, H: ?Sized + AsRef<[u8]>> From<&'h H> for Input<'h> {
fn from(haystack: &'h H) -> Input<'h> {
Input::new(haystack)
}
}
/// A representation of a span reported by a regex engine.
///
/// A span corresponds to the starting and ending _byte offsets_ of a
/// contiguous region of bytes. The starting offset is inclusive while the
/// ending offset is exclusive. That is, a span is a half-open interval.
///
/// A span is used to report the offsets of a match, but it is also used to
/// convey which region of a haystack should be searched via routines like
/// [`Input::span`].
///
/// This is basically equivalent to a `std::ops::Range<usize>`, except this
/// type implements `Copy` which makes it more ergonomic to use in the context
/// of this crate. Like a range, this implements `Index` for `[u8]` and `str`,
/// and `IndexMut` for `[u8]`. For convenience, this also impls `From<Range>`,
/// which means things like `Span::from(5..10)` work.
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct Span {
/// The start offset of the span, inclusive.
pub start: usize,
/// The end offset of the span, exclusive.
pub end: usize,
}
impl Span {
/// Returns this span as a range.
#[inline]
pub fn range(&self) -> Range<usize> {
Range::from(*self)
}
/// Returns true when this span is empty. That is, when `start >= end`.
#[inline]
pub fn is_empty(&self) -> bool {
self.start >= self.end
}
/// Returns the length of this span.
///
/// This returns `0` in precisely the cases that `is_empty` returns `true`.
#[inline]
pub fn len(&self) -> usize {
self.end.saturating_sub(self.start)
}
/// Returns true when the given offset is contained within this span.
///
/// Note that an empty span contains no offsets and will always return
/// false.
#[inline]
pub fn contains(&self, offset: usize) -> bool {
!self.is_empty() && self.start <= offset && offset <= self.end
}
/// Returns a new span with `offset` added to this span's `start` and `end`
/// values.
///
/// # Panics
///
/// This panics if adding `offset` to either part of this `Span` would
/// result in overflow.
#[inline]
pub fn offset(&self, offset: usize) -> Span {
Span {
start: self
.start
.checked_add(offset)
.expect("invalid start+offset"),
end: self.end.checked_add(offset).expect("invalid end+offset"),
}
}
}
impl core::fmt::Debug for Span {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}..{}", self.start, self.end)
}
}
impl core::ops::Index<Span> for [u8] {
type Output = [u8];
#[inline]
fn index(&self, index: Span) -> &[u8] {
&self[index.range()]
}
}
impl core::ops::IndexMut<Span> for [u8] {
#[inline]
fn index_mut(&mut self, index: Span) -> &mut [u8] {
&mut self[index.range()]
}
}
impl core::ops::Index<Span> for str {
type Output = str;
#[inline]
fn index(&self, index: Span) -> &str {
&self[index.range()]
}
}
impl From<Range<usize>> for Span {
#[inline]
fn from(range: Range<usize>) -> Span {
Span { start: range.start, end: range.end }
}
}
impl From<Span> for Range<usize> {
#[inline]
fn from(span: Span) -> Range<usize> {
Range { start: span.start, end: span.end }
}
}
impl PartialEq<Range<usize>> for Span {
#[inline]
fn eq(&self, range: &Range<usize>) -> bool {
self.start == range.start && self.end == range.end
}
}
impl PartialEq<Span> for Range<usize> {
#[inline]
fn eq(&self, span: &Span) -> bool {
self.start == span.start && self.end == span.end
}
}
/// A representation of "half" of a match reported by a DFA.
///
/// This is called a "half" match because it only includes the end location (or
/// start location for a reverse search) of a match. This corresponds to the
/// information that a single DFA scan can report. Getting the other half of
/// the match requires a second scan with a reversed DFA.
///
/// A half match also includes the pattern that matched. The pattern is
/// identified by an ID, which corresponds to its position (starting from `0`)
/// relative to other patterns used to construct the corresponding DFA. If only
/// a single pattern is provided to the DFA, then all matches are guaranteed to
/// have a pattern ID of `0`.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct HalfMatch {
/// The pattern ID.
pattern: PatternID,
/// The offset of the match.
///
/// For forward searches, the offset is exclusive. For reverse searches,
/// the offset is inclusive.
offset: usize,
}
impl HalfMatch {
/// Create a new half match from a pattern ID and a byte offset.
#[inline]
pub fn new(pattern: PatternID, offset: usize) -> HalfMatch {
HalfMatch { pattern, offset }
}
/// Create a new half match from a pattern ID and a byte offset.
///
/// This is like [`HalfMatch::new`], but accepts a `usize` instead of a
/// [`PatternID`]. This panics if the given `usize` is not representable
/// as a `PatternID`.
#[inline]
pub fn must(pattern: usize, offset: usize) -> HalfMatch {
HalfMatch::new(PatternID::new(pattern).unwrap(), offset)
}
/// Returns the ID of the pattern that matched.
///
/// The ID of a pattern is derived from the position in which it was
/// originally inserted into the corresponding DFA. The first pattern has
/// identifier `0`, and each subsequent pattern is `1`, `2` and so on.
#[inline]
pub fn pattern(&self) -> PatternID {
self.pattern
}
/// The position of the match.
///
/// If this match was produced by a forward search, then the offset is
/// exclusive. If this match was produced by a reverse search, then the
/// offset is inclusive.
#[inline]
pub fn offset(&self) -> usize {
self.offset
}
}
/// A representation of a match reported by a regex engine.
///
/// A match has two essential pieces of information: the [`PatternID`] that
/// matches, and the [`Span`] of the match in a haystack.
///
/// The pattern is identified by an ID, which corresponds to its position
/// (starting from `0`) relative to other patterns used to construct the
/// corresponding regex engine. If only a single pattern is provided, then all
/// matches are guaranteed to have a pattern ID of `0`.
///
/// Every match reported by a regex engine guarantees that its span has its
/// start offset as less than or equal to its end offset.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Match {
/// The pattern ID.
pattern: PatternID,
/// The underlying match span.
span: Span,