regex_bites/bytes.rs
1//! Search for regex matches in `&[u8]` haystacks.
2//!
3//! TODO: Adapt documentation from `src/bytes.rs`?
4
5use alloc::{
6 borrow::Cow, boxed::Box, string::String, string::ToString, sync::Arc, vec,
7 vec::Vec,
8};
9
10pub use crate::error::Error;
11use crate::{
12 hir::{self, Hir},
13 int::NonMaxUsize,
14 interpolate,
15 nfa::{self, NFA},
16 pikevm::{self, Cache, PikeVM},
17 pool::CachePool,
18};
19
20/// A compiled regular expression for searching Unicode haystacks.
21///
22/// A `Regex` can be used to search haystacks, split haystacks into substrings
23/// or replace substrings in a haystack with a different substring. All
24/// searching is done with an implicit `(?s:.)*?` at the beginning and end of
25/// an pattern. To force an expression to match the whole string (or a prefix
26/// or a suffix), you must use an anchor like `^` or `$` (or `\A` and `\z`).
27///
28/// While this crate will handle Unicode strings (whether in the regular
29/// expression or in the haystack), all positions returned are **byte
30/// offsets**. Every byte offset is guaranteed to be at a Unicode code point
31/// boundary. That is, all offsets returned by the `Regex` API are guaranteed
32/// to be ranges that can slice a `&str` without panicking.
33///
34/// The only methods that allocate new strings are the string replacement
35/// methods. All other methods (searching and splitting) return borrowed
36/// references into the haystack given.
37///
38/// # Example
39///
40/// Find the offsets of a US phone number:
41///
42/// ```
43/// use regex_bites::bytes::Regex;
44///
45/// let re = Regex::new("[0-9]{3}-[0-9]{3}-[0-9]{4}").unwrap();
46/// let m = re.find(b"phone: 111-222-3333").unwrap();
47/// assert_eq!(7..19, m.range());
48/// ```
49///
50/// # Example: extracting capture groups
51///
52/// A common way to use regexes is with capture groups. That is, instead of
53/// just looking for matches of an entire regex, parentheses are used to create
54/// groups that represent part of the match.
55///
56/// For example, consider a haystack with multiple lines, and each line has
57/// three whitespace delimited fields where the second field is expected to be
58/// a number and the third field a boolean. To make this convenient, we use
59/// the [`Captures::extract`] API to put the strings that match each group
60/// into a fixed size array:
61///
62/// ```
63/// use regex_bites::bytes::Regex;
64///
65/// let hay = b"
66/// rabbit 54 true
67/// groundhog 2 true
68/// does not match
69/// fox 109 false
70/// ";
71/// let re = Regex::new(r"(?m)^\s*(\S+)\s+([0-9]+)\s+(true|false)\s*$").unwrap();
72/// let mut fields: Vec<(&[u8], i64, bool)> = vec![];
73/// for (_, [f1, f2, f3]) in re.captures_iter(hay).map(|caps| caps.extract()) {
74/// fields.push((f1, std::str::from_utf8(f2)?.parse()?, std::str::from_utf8(f3)?.parse()?));
75/// }
76/// assert_eq!(fields, vec![
77/// (&b"rabbit"[..], 54, true),
78/// (&b"groundhog"[..], 2, true),
79/// (&b"fox"[..], 109, false),
80/// ]);
81///
82/// # Ok::<(), Box<dyn std::error::Error>>(())
83/// ```
84pub struct Regex {
85 pikevm: Arc<PikeVM>,
86 pool: CachePool,
87}
88
89impl Clone for Regex {
90 fn clone(&self) -> Regex {
91 let pikevm = Arc::clone(&self.pikevm);
92 let pool = {
93 let pikevm = Arc::clone(&self.pikevm);
94 let create = Box::new(move || Cache::new(&pikevm));
95 CachePool::new(create)
96 };
97 Regex { pikevm, pool }
98 }
99}
100
101impl core::fmt::Display for Regex {
102 /// Shows the original regular expression.
103 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
104 write!(f, "{}", self.as_str())
105 }
106}
107
108impl core::fmt::Debug for Regex {
109 /// Shows the original regular expression.
110 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
111 f.debug_tuple("Regex").field(&self.as_str()).finish()
112 }
113}
114
115impl core::str::FromStr for Regex {
116 type Err = Error;
117
118 /// Attempts to parse a string into a regular expression
119 fn from_str(s: &str) -> Result<Regex, Error> {
120 Regex::new(s)
121 }
122}
123
124impl TryFrom<&str> for Regex {
125 type Error = Error;
126
127 /// Attempts to parse a string into a regular expression
128 fn try_from(s: &str) -> Result<Regex, Error> {
129 Regex::new(s)
130 }
131}
132
133impl TryFrom<String> for Regex {
134 type Error = Error;
135
136 /// Attempts to parse a string into a regular expression
137 fn try_from(s: String) -> Result<Regex, Error> {
138 Regex::new(&s)
139 }
140}
141
142/// Core regular expression methods.
143impl Regex {
144 /// Compiles a regular expression. Once compiled, it can be used repeatedly
145 /// to search, split or replace substrings in a haystack.
146 ///
147 /// Note that regex compilation tends to be a somewhat expensive process,
148 /// and unlike higher level environments, compilation is not automatically
149 /// cached for you. One should endeavor to compile a regex once and then
150 /// reuse it. For example, it's a bad idea to compile the same regex
151 /// repeatedly in a loop.
152 ///
153 /// # Errors
154 ///
155 /// If an invalid pattern is given, then an error is returned.
156 /// An error is also returned if the pattern is valid, but would
157 /// produce a regex that is bigger than the configured size limit via
158 /// [`RegexBuilder::size_limit`]. (A reasonable size limit is enabled by
159 /// default.)
160 ///
161 /// # Example
162 ///
163 /// ```
164 /// use regex_bites::bytes::Regex;
165 ///
166 /// // An Invalid pattern because of an unclosed parenthesis
167 /// assert!(Regex::new(r"foo(bar").is_err());
168 /// // An invalid pattern because the regex would be too big
169 /// // because Unicode tends to inflate things.
170 /// assert!(Regex::new(r"\w{1000000}").is_err());
171 /// ```
172 pub fn new(pattern: &str) -> Result<Regex, Error> {
173 RegexBuilder::new(pattern).build()
174 }
175
176 /// Returns true if and only if there is a match for the regex anywhere
177 /// in the haystack given.
178 ///
179 /// It is recommended to use this method if all you need to do is test
180 /// whether a match exists, since the underlying matching engine may be
181 /// able to do less work.
182 ///
183 /// # Example
184 ///
185 /// Test if some haystack contains at least one word with exactly 13
186 /// word characters:
187 ///
188 /// ```
189 /// use regex_bites::bytes::Regex;
190 ///
191 /// let re = Regex::new(r"\b\w{13}\b").unwrap();
192 /// let hay = b"I categorically deny having triskaidekaphobia.";
193 /// assert!(re.is_match(hay));
194 /// ```
195 #[inline]
196 pub fn is_match(&self, haystack: &[u8]) -> bool {
197 self.is_match_at(haystack, 0)
198 }
199
200 /// This routine searches for the first match of this regex in the
201 /// haystack given, and if found, returns a [`Match`]. The `Match`
202 /// provides access to both the byte offsets of the match and the actual
203 /// substring that matched.
204 ///
205 /// Note that this should only be used if you want to find the entire
206 /// match. If instead you just want to test the existence of a match,
207 /// it's potentially faster to use `Regex::is_match(hay)` instead of
208 /// `Regex::find(hay).is_some()`.
209 ///
210 /// # Example
211 ///
212 /// Find the first word with exactly 13 word characters:
213 ///
214 /// ```
215 /// use regex_bites::bytes::Regex;
216 ///
217 /// let re = Regex::new(r"\b\w{13}\b").unwrap();
218 /// let hay = b"I categorically deny having triskaidekaphobia.";
219 /// let mat = re.find(hay).unwrap();
220 /// assert_eq!(2..15, mat.range());
221 /// assert_eq!(b"categorically", mat.as_bytes());
222 /// ```
223 #[inline]
224 pub fn find<'h>(&self, haystack: &'h [u8]) -> Option<Match<'h>> {
225 self.find_at(haystack, 0)
226 }
227
228 /// Returns an iterator that yields successive non-overlapping matches in
229 /// the given haystack. The iterator yields values of type [`Match`].
230 ///
231 /// # Time complexity
232 ///
233 /// Note that since `find_iter` runs potentially many searches on the
234 /// haystack and since each search has worst case `O(m * n)` time
235 /// complexity, the overall worst case time complexity for iteration is
236 /// `O(m * n^2)`.
237 ///
238 /// # Example
239 ///
240 /// Find every word with exactly 13 word characters:
241 ///
242 /// ```
243 /// use regex_bites::bytes::Regex;
244 ///
245 /// let re = Regex::new(r"\b\w{13}\b").unwrap();
246 /// let hay = b"Retroactively relinquishing remunerations is reprehensible.";
247 /// let matches: Vec<_> = re.find_iter(hay).map(|m| m.as_bytes()).collect();
248 /// assert_eq!(matches, vec![
249 /// b"Retroactively",
250 /// b"relinquishing",
251 /// b"remunerations",
252 /// b"reprehensible",
253 /// ]);
254 /// ```
255 #[inline]
256 pub fn find_iter<'r, 'h>(&'r self, haystack: &'h [u8]) -> Matches<'r, 'h> {
257 Matches {
258 haystack,
259 it: self.pikevm.find_iter(self.pool.get(), haystack),
260 }
261 }
262
263 /// This routine searches for the first match of this regex in the haystack
264 /// given, and if found, returns not only the overall match but also the
265 /// matches of each capture group in the regex. If no match is found, then
266 /// `None` is returned.
267 ///
268 /// Capture group `0` always corresponds to an implicit unnamed group that
269 /// includes the entire match. If a match is found, this group is always
270 /// present. Subsequent groups may be named and are numbered, starting
271 /// at 1, by the order in which the opening parenthesis appears in the
272 /// pattern. For example, in the pattern `(?<a>.(?<b>.))(?<c>.)`, `a`,
273 /// `b` and `c` correspond to capture group indices `1`, `2` and `3`,
274 /// respectively.
275 ///
276 /// You should only use `captures` if you need access to the capture group
277 /// matches. Otherwise, [`Regex::find`] is generally faster for discovering
278 /// just the overall match.
279 ///
280 /// # Example
281 ///
282 /// Say you have some haystack with movie names and their release years,
283 /// like "'Citizen Kane' (1941)". It'd be nice if we could search for
284 /// substrings looking like that, while also extracting the movie name and
285 /// its release year separately. The example below shows how to do that.
286 ///
287 /// ```
288 /// use regex_bites::bytes::Regex;
289 ///
290 /// let re = Regex::new(r"'([^']+)'\s+\((\d{4})\)").unwrap();
291 /// let hay = b"Not my favorite movie: 'Citizen Kane' (1941).";
292 /// let caps = re.captures(hay).unwrap();
293 /// assert_eq!(caps.get(0).unwrap().as_bytes(), b"'Citizen Kane' (1941)");
294 /// assert_eq!(caps.get(1).unwrap().as_bytes(), b"Citizen Kane");
295 /// assert_eq!(caps.get(2).unwrap().as_bytes(), b"1941");
296 /// // You can also access the groups by index using the Index notation.
297 /// // Note that this will panic on an invalid index. In this case, these
298 /// // accesses are always correct because the overall regex will only
299 /// // match when these capture groups match.
300 /// assert_eq!(&caps[0], b"'Citizen Kane' (1941)");
301 /// assert_eq!(&caps[1], b"Citizen Kane");
302 /// assert_eq!(&caps[2], b"1941");
303 /// ```
304 ///
305 /// Note that the full match is at capture group `0`. Each subsequent
306 /// capture group is indexed by the order of its opening `(`.
307 ///
308 /// We can make this example a bit clearer by using *named* capture groups:
309 ///
310 /// ```
311 /// use regex_bites::bytes::Regex;
312 ///
313 /// let re = Regex::new(r"'(?<title>[^']+)'\s+\((?<year>\d{4})\)").unwrap();
314 /// let hay = b"Not my favorite movie: 'Citizen Kane' (1941).";
315 /// let caps = re.captures(hay).unwrap();
316 /// assert_eq!(caps.get(0).unwrap().as_bytes(), b"'Citizen Kane' (1941)");
317 /// assert_eq!(caps.name("title").unwrap().as_bytes(), b"Citizen Kane");
318 /// assert_eq!(caps.name("year").unwrap().as_bytes(), b"1941");
319 /// // You can also access the groups by name using the Index notation.
320 /// // Note that this will panic on an invalid group name. In this case,
321 /// // these accesses are always correct because the overall regex will
322 /// // only match when these capture groups match.
323 /// assert_eq!(&caps[0], b"'Citizen Kane' (1941)");
324 /// assert_eq!(&caps["title"], b"Citizen Kane");
325 /// assert_eq!(&caps["year"], b"1941");
326 /// ```
327 ///
328 /// Here we name the capture groups, which we can access with the `name`
329 /// method or the `Index` notation with a `&str`. Note that the named
330 /// capture groups are still accessible with `get` or the `Index` notation
331 /// with a `usize`.
332 ///
333 /// The `0`th capture group is always unnamed, so it must always be
334 /// accessed with `get(0)` or `[0]`.
335 ///
336 /// Finally, one other way to to get the matched substrings is with the
337 /// [`Captures::extract`] API:
338 ///
339 /// ```
340 /// use regex_bites::bytes::Regex;
341 ///
342 /// let re = Regex::new(r"'([^']+)'\s+\((\d{4})\)").unwrap();
343 /// let hay = b"Not my favorite movie: 'Citizen Kane' (1941).";
344 /// let (full, [title, year]) = re.captures(hay).unwrap().extract();
345 /// assert_eq!(full, b"'Citizen Kane' (1941)");
346 /// assert_eq!(title, b"Citizen Kane");
347 /// assert_eq!(year, b"1941");
348 /// ```
349 #[inline]
350 pub fn captures<'h>(&self, haystack: &'h [u8]) -> Option<Captures<'h>> {
351 self.captures_at(haystack, 0)
352 }
353
354 /// Returns an iterator that yields successive non-overlapping matches in
355 /// the given haystack. The iterator yields values of type [`Captures`].
356 ///
357 /// This is the same as [`Regex::find_iter`], but instead of only providing
358 /// access to the overall match, each value yield includes access to the
359 /// matches of all capture groups in the regex. Reporting this extra match
360 /// data is potentially costly, so callers should only use `captures_iter`
361 /// over `find_iter` when they actually need access to the capture group
362 /// matches.
363 ///
364 /// # Time complexity
365 ///
366 /// Note that since `captures_iter` runs potentially many searches on the
367 /// haystack and since each search has worst case `O(m * n)` time
368 /// complexity, the overall worst case time complexity for iteration is
369 /// `O(m * n^2)`.
370 ///
371 /// # Example
372 ///
373 /// We can use this to find all movie titles and their release years in
374 /// some haystack, where the movie is formatted like "'Title' (xxxx)":
375 ///
376 /// ```
377 /// use regex_bites::bytes::Regex;
378 ///
379 /// let re = Regex::new(r"'([^']+)'\s+\(([0-9]{4})\)").unwrap();
380 /// let hay = b"'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931).";
381 /// let mut movies = vec![];
382 /// for (_, [title, year]) in re.captures_iter(hay).map(|c| c.extract()) {
383 /// movies.push((title, std::str::from_utf8(year)?.parse::<i64>()?));
384 /// }
385 /// assert_eq!(movies, vec![
386 /// (&b"Citizen Kane"[..], 1941),
387 /// (&b"The Wizard of Oz"[..], 1939),
388 /// (&b"M"[..], 1931),
389 /// ]);
390 /// # Ok::<(), Box<dyn std::error::Error>>(())
391 /// ```
392 ///
393 /// Or with named groups:
394 ///
395 /// ```
396 /// use regex_bites::bytes::Regex;
397 ///
398 /// let re = Regex::new(r"'(?<title>[^']+)'\s+\((?<year>[0-9]{4})\)").unwrap();
399 /// let hay = b"'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931).";
400 /// let mut it = re.captures_iter(hay);
401 ///
402 /// let caps = it.next().unwrap();
403 /// assert_eq!(&caps["title"], b"Citizen Kane");
404 /// assert_eq!(&caps["year"], b"1941");
405 ///
406 /// let caps = it.next().unwrap();
407 /// assert_eq!(&caps["title"], b"The Wizard of Oz");
408 /// assert_eq!(&caps["year"], b"1939");
409 ///
410 /// let caps = it.next().unwrap();
411 /// assert_eq!(&caps["title"], b"M");
412 /// assert_eq!(&caps["year"], b"1931");
413 /// ```
414 #[inline]
415 pub fn captures_iter<'r, 'h>(
416 &'r self,
417 haystack: &'h [u8],
418 ) -> CaptureMatches<'r, 'h> {
419 CaptureMatches {
420 haystack,
421 re: self,
422 it: self.pikevm.captures_iter(self.pool.get(), haystack),
423 }
424 }
425
426 /// Returns an iterator of substrings of the haystack given, delimited by a
427 /// match of the regex. Namely, each element of the iterator corresponds to
428 /// a part of the haystack that *isn't* matched by the regular expression.
429 ///
430 /// # Time complexity
431 ///
432 /// Since iterators over all matches requires running potentially many
433 /// searches on the haystack, and since each search has worst case
434 /// `O(m * n)` time complexity, the overall worst case time complexity for
435 /// this routine is `O(m * n^2)`.
436 ///
437 /// # Example
438 ///
439 /// To split a string delimited by arbitrary amounts of spaces or tabs:
440 ///
441 /// ```
442 /// use regex_bites::bytes::Regex;
443 ///
444 /// let re = Regex::new(r"[ \t]+").unwrap();
445 /// let hay = b"a b \t c\td e";
446 /// let fields: Vec<&[u8]> = re.split(hay).collect();
447 /// assert_eq!(fields, vec![&b"a"[..], &b"b"[..], &b"c"[..], &b"d"[..], &b"e"[..]]);
448 /// ```
449 ///
450 /// # Example: more cases
451 ///
452 /// Basic usage:
453 ///
454 /// ```
455 /// use regex_bites::bytes::Regex;
456 ///
457 /// let re = Regex::new(r" ").unwrap();
458 /// let hay = b"Mary had a little lamb";
459 /// let got: Vec<&[u8]> = re.split(hay).collect();
460 /// assert_eq!(got, vec![&b"Mary"[..], &b"had"[..], &b"a"[..], &b"little"[..], &b"lamb"[..]]);
461 ///
462 /// let re = Regex::new(r"X").unwrap();
463 /// let hay = b"";
464 /// let got: Vec<&[u8]> = re.split(hay).collect();
465 /// assert_eq!(got, vec![&b""[..]]);
466 ///
467 /// let re = Regex::new(r"X").unwrap();
468 /// let hay = b"lionXXtigerXleopard";
469 /// let got: Vec<&[u8]> = re.split(hay).collect();
470 /// assert_eq!(got, vec![&b"lion"[..], &b""[..], &b"tiger"[..], &b"leopard"[..]]);
471 ///
472 /// let re = Regex::new(r"::").unwrap();
473 /// let hay = b"lion::tiger::leopard";
474 /// let got: Vec<&[u8]> = re.split(hay).collect();
475 /// assert_eq!(got, vec![&b"lion"[..], &b"tiger"[..], &b"leopard"[..]]);
476 /// ```
477 ///
478 /// If a haystack contains multiple contiguous matches, you will end up
479 /// with empty spans yielded by the iterator:
480 ///
481 /// ```
482 /// use regex_bites::bytes::Regex;
483 ///
484 /// let re = Regex::new(r"X").unwrap();
485 /// let hay = b"XXXXaXXbXc";
486 /// let got: Vec<&[u8]> = re.split(hay).collect();
487 /// assert_eq!(got, vec![&b""[..], &b""[..], &b""[..], &b""[..], &b"a"[..], &b""[..], &b"b"[..], &b"c"[..]]);
488 ///
489 /// let re = Regex::new(r"/").unwrap();
490 /// let hay = b"(///)";
491 /// let got: Vec<&[u8]> = re.split(hay).collect();
492 /// assert_eq!(got, vec![&b"("[..], &b""[..], &b""[..], &b")"[..]]);
493 /// ```
494 ///
495 /// Separators at the start or end of a haystack are neighbored by empty
496 /// substring.
497 ///
498 /// ```
499 /// use regex_bites::bytes::Regex;
500 ///
501 /// let re = Regex::new(r"0").unwrap();
502 /// let hay = b"010";
503 /// let got: Vec<&[u8]> = re.split(hay).collect();
504 /// assert_eq!(got, vec![&b""[..], &b"1"[..], &b""[..]]);
505 /// ```
506 ///
507 /// When the empty string is used as a regex, it splits at every valid
508 /// UTF-8 boundary by default (which includes the beginning and end of the
509 /// haystack):
510 ///
511 /// ```
512 /// use regex_bites::bytes::Regex;
513 ///
514 /// let re = Regex::new(r"").unwrap();
515 /// let hay = b"rust";
516 /// let got: Vec<&[u8]> = re.split(hay).collect();
517 /// assert_eq!(got, vec![&b""[..], &b"r"[..], &b"u"[..], &b"s"[..], &b"t"[..], &b""[..]]);
518 ///
519 /// // Splitting by an empty string is UTF-8 aware by default!
520 /// let re = Regex::new(r"").unwrap();
521 /// let hay = "☃".as_bytes();
522 /// let got: Vec<&[u8]> = re.split(hay).collect();
523 /// assert_eq!(got, vec![b"", "☃".as_bytes(), b""]);
524 /// ```
525 ///
526 /// Contiguous separators (commonly shows up with whitespace), can lead to
527 /// possibly surprising behavior. For example, this code is correct:
528 ///
529 /// ```
530 /// use regex_bites::bytes::Regex;
531 ///
532 /// let re = Regex::new(r" ").unwrap();
533 /// let hay = b" a b c";
534 /// let got: Vec<&[u8]> = re.split(hay).collect();
535 /// assert_eq!(got, vec![&b""[..], &b""[..], &b""[..], &b""[..], &b"a"[..], &b""[..], &b"b"[..], &b"c"[..]]);
536 /// ```
537 ///
538 /// It does *not* give you `["a", "b", "c"]`. For that behavior, you'd want
539 /// to match contiguous space characters:
540 ///
541 /// ```
542 /// use regex_bites::bytes::Regex;
543 ///
544 /// let re = Regex::new(r" +").unwrap();
545 /// let hay = b" a b c";
546 /// let got: Vec<&[u8]> = re.split(hay).collect();
547 /// // N.B. This does still include a leading empty span because ' +'
548 /// // matches at the beginning of the haystack.
549 /// assert_eq!(got, vec![&b""[..], &b"a"[..], &b"b"[..], &b"c"[..]]);
550 /// ```
551 #[inline]
552 pub fn split<'r, 'h>(&'r self, haystack: &'h [u8]) -> Split<'r, 'h> {
553 Split { haystack, finder: self.find_iter(haystack), last: 0 }
554 }
555
556 /// Returns an iterator of at most `limit` substrings of the haystack
557 /// given, delimited by a match of the regex. (A `limit` of `0` will return
558 /// no substrings.) Namely, each element of the iterator corresponds to a
559 /// part of the haystack that *isn't* matched by the regular expression.
560 /// The remainder of the haystack that is not split will be the last
561 /// element in the iterator.
562 ///
563 /// # Time complexity
564 ///
565 /// Since iterators over all matches requires running potentially many
566 /// searches on the haystack, and since each search has worst case
567 /// `O(m * n)` time complexity, the overall worst case time complexity for
568 /// this routine is `O(m * n^2)`.
569 ///
570 /// Although note that the worst case time here has an upper bound given
571 /// by the `limit` parameter.
572 ///
573 /// # Example
574 ///
575 /// Get the first two words in some haystack:
576 ///
577 /// ```
578 /// use regex_bites::bytes::Regex;
579 ///
580 /// let re = Regex::new(r"\W+").unwrap();
581 /// let hay = b"Hey! How are you?";
582 /// let fields: Vec<&[u8]> = re.splitn(hay, 3).collect();
583 /// assert_eq!(fields, vec![&b"Hey"[..], &b"How"[..], &b"are you?"[..]]);
584 /// ```
585 ///
586 /// # Examples: more cases
587 ///
588 /// ```
589 /// use regex_bites::bytes::Regex;
590 ///
591 /// let re = Regex::new(r" ").unwrap();
592 /// let hay = b"Mary had a little lamb";
593 /// let got: Vec<&[u8]> = re.splitn(hay, 3).collect();
594 /// assert_eq!(got, vec![&b"Mary"[..], &b"had"[..], &b"a little lamb"[..]]);
595 ///
596 /// let re = Regex::new(r"X").unwrap();
597 /// let hay = b"";
598 /// let got: Vec<&[u8]> = re.splitn(hay, 3).collect();
599 /// assert_eq!(got, vec![&b""[..]]);
600 ///
601 /// let re = Regex::new(r"X").unwrap();
602 /// let hay = b"lionXXtigerXleopard";
603 /// let got: Vec<&[u8]> = re.splitn(hay, 3).collect();
604 /// assert_eq!(got, vec![&b"lion"[..], &b""[..], &b"tigerXleopard"[..]]);
605 ///
606 /// let re = Regex::new(r"::").unwrap();
607 /// let hay = b"lion::tiger::leopard";
608 /// let got: Vec<&[u8]> = re.splitn(hay, 2).collect();
609 /// assert_eq!(got, vec![&b"lion"[..], &b"tiger::leopard"[..]]);
610 ///
611 /// let re = Regex::new(r"X").unwrap();
612 /// let hay = b"abcXdef";
613 /// let got: Vec<&[u8]> = re.splitn(hay, 1).collect();
614 /// assert_eq!(got, vec![&b"abcXdef"[..]]);
615 ///
616 /// let re = Regex::new(r"X").unwrap();
617 /// let hay = b"abcdef";
618 /// let got: Vec<&[u8]> = re.splitn(hay, 2).collect();
619 /// assert_eq!(got, vec![&b"abcdef"[..]]);
620 ///
621 /// let re = Regex::new(r"X").unwrap();
622 /// let hay = b"abcXdef";
623 /// let got: Vec<&[u8]> = re.splitn(hay, 0).collect();
624 /// assert!(got.is_empty());
625 /// ```
626 #[inline]
627 pub fn splitn<'r, 'h>(
628 &'r self,
629 haystack: &'h [u8],
630 limit: usize,
631 ) -> SplitN<'r, 'h> {
632 SplitN { splits: self.split(haystack), limit }
633 }
634
635 /// Replaces the leftmost-first match in the given haystack with the
636 /// replacement provided. The replacement can be a regular string (where
637 /// `$N` and `$name` are expanded to match capture groups) or a function
638 /// that takes a [`Captures`] and returns the replaced string.
639 ///
640 /// If no match is found, then the haystack is returned unchanged. In that
641 /// case, this implementation will likely return a `Cow::Borrowed` value
642 /// such that no allocation is performed.
643 ///
644 /// # Replacement string syntax
645 ///
646 /// All instances of `$ref` in the replacement string are replaced with
647 /// the substring corresponding to the capture group identified by `ref`.
648 ///
649 /// `ref` may be an integer corresponding to the index of the capture group
650 /// (counted by order of opening parenthesis where `0` is the entire match)
651 /// or it can be a name (consisting of letters, digits or underscores)
652 /// corresponding to a named capture group.
653 ///
654 /// If `ref` isn't a valid capture group (whether the name doesn't exist or
655 /// isn't a valid index), then it is replaced with the empty string.
656 ///
657 /// The longest possible name is used. For example, `$1a` looks up the
658 /// capture group named `1a` and not the capture group at index `1`. To
659 /// exert more precise control over the name, use braces, e.g., `${1}a`.
660 ///
661 /// To write a literal `$` use `$$`.
662 ///
663 /// # Example
664 ///
665 /// Note that this function is polymorphic with respect to the replacement.
666 /// In typical usage, this can just be a normal string:
667 ///
668 /// ```
669 /// use regex_bites::bytes::Regex;
670 ///
671 /// let re = Regex::new(r"[^01]+").unwrap();
672 /// assert_eq!(re.replace(b"1078910", &b""[..]), &b"1010"[..]);
673 /// ```
674 ///
675 /// But anything satisfying the [`Replacer`] trait will work. For example,
676 /// a closure of type `|&Captures| -> Vec<u8>` provides direct access to the
677 /// captures corresponding to a match. This allows one to access capturing
678 /// group matches easily:
679 ///
680 /// ```
681 /// use regex_bites::bytes::{Captures, Regex};
682 ///
683 /// let re = Regex::new(r"([^,\s]+),\s+(\S+)").unwrap();
684 /// let result = re.replace(b"Springsteen, Bruce", |caps: &Captures| {
685 /// [&caps[2], b" ", &caps[1]].concat()
686 /// });
687 /// assert_eq!(result, &b"Bruce Springsteen"[..]);
688 /// ```
689 ///
690 /// But this is a bit cumbersome to use all the time. Instead, a simple
691 /// syntax is supported (as described above) that expands `$name` into the
692 /// corresponding capture group. Here's the last example, but using this
693 /// expansion technique with named capture groups:
694 ///
695 /// ```
696 /// use regex_bites::bytes::Regex;
697 ///
698 /// let re = Regex::new(r"(?<last>[^,\s]+),\s+(?<first>\S+)").unwrap();
699 /// let result = re.replace(b"Springsteen, Bruce", &b"$first $last"[..]);
700 /// assert_eq!(result, &b"Bruce Springsteen"[..]);
701 /// ```
702 ///
703 /// Note that using `$2` instead of `$first` or `$1` instead of `$last`
704 /// would produce the same result. To write a literal `$` use `$$`.
705 ///
706 /// Sometimes the replacement string requires use of curly braces to
707 /// delineate a capture group replacement when it is adjacent to some other
708 /// literal text. For example, if we wanted to join two words together with
709 /// an underscore:
710 ///
711 /// ```
712 /// use regex_bites::bytes::Regex;
713 ///
714 /// let re = Regex::new(r"(?<first>\w+)\s+(?<second>\w+)").unwrap();
715 /// let result = re.replace(b"deep fried", &b"${first}_$second"[..]);
716 /// assert_eq!(result, &b"deep_fried"[..]);
717 /// ```
718 ///
719 /// Without the curly braces, the capture group name `first_` would be
720 /// used, and since it doesn't exist, it would be replaced with the empty
721 /// string.
722 ///
723 /// Finally, sometimes you just want to replace a literal string with no
724 /// regard for capturing group expansion. This can be done by wrapping a
725 /// string with [`NoExpand`]:
726 ///
727 /// ```
728 /// use regex_bites::bytes::{NoExpand, Regex};
729 ///
730 /// let re = Regex::new(r"(?<last>[^,\s]+),\s+(\S+)").unwrap();
731 /// let result = re.replace(b"Springsteen, Bruce", NoExpand(b"$2 $last"));
732 /// assert_eq!(result, &b"$2 $last"[..]);
733 /// ```
734 ///
735 /// Using `NoExpand` may also be faster, since the replacement string won't
736 /// need to be parsed for the `$` syntax.
737 #[inline]
738 pub fn replace<'h, R: Replacer>(
739 &self,
740 haystack: &'h [u8],
741 rep: R,
742 ) -> Cow<'h, [u8]> {
743 self.replacen(haystack, 1, rep)
744 }
745
746 /// Replaces all non-overlapping matches in the haystack with the
747 /// replacement provided. This is the same as calling `replacen` with
748 /// `limit` set to `0`.
749 ///
750 /// The documentation for [`Regex::replace`] goes into more detail about
751 /// what kinds of replacement strings are supported.
752 ///
753 /// # Time complexity
754 ///
755 /// Since iterators over all matches requires running potentially many
756 /// searches on the haystack, and since each search has worst case
757 /// `O(m * n)` time complexity, the overall worst case time complexity for
758 /// this routine is `O(m * n^2)`.
759 ///
760 /// # Fallibility
761 ///
762 /// If you need to write a replacement routine where any individual
763 /// replacement might "fail," doing so with this API isn't really feasible
764 /// because there's no way to stop the search process if a replacement
765 /// fails. Instead, if you need this functionality, you should consider
766 /// implementing your own replacement routine:
767 ///
768 /// ```
769 /// use regex_bites::bytes::{Captures, Regex};
770 ///
771 /// fn replace_all<E>(
772 /// re: &Regex,
773 /// haystack: &[u8],
774 /// replacement: impl Fn(&Captures) -> Result<Vec<u8>, E>,
775 /// ) -> Result<Vec<u8>, E> {
776 /// let mut new = Vec::with_capacity(haystack.len());
777 /// let mut last_match = 0;
778 /// for caps in re.captures_iter(haystack) {
779 /// let m = caps.get(0).unwrap();
780 /// new.extend(&haystack[last_match..m.start()]);
781 /// new.extend(&replacement(&caps)?);
782 /// last_match = m.end();
783 /// }
784 /// new.extend(&haystack[last_match..]);
785 /// Ok(new)
786 /// }
787 ///
788 /// // Let's replace each word with the number of bytes in that word.
789 /// // But if we see a word that is "too long," we'll give up.
790 /// let re = Regex::new(r"\w+").unwrap();
791 /// let replacement = |caps: &Captures| -> Result<Vec<u8>, &'static str> {
792 /// if caps[0].len() >= 5 {
793 /// return Err("word too long");
794 /// }
795 /// Ok(caps[0].len().to_string().into_bytes())
796 /// };
797 /// assert_eq!(
798 /// Ok(b"2 3 3 3?".to_vec()),
799 /// replace_all(&re, b"hi how are you?", &replacement),
800 /// );
801 /// assert!(replace_all(&re, b"hi there", &replacement).is_err());
802 /// ```
803 ///
804 /// # Example
805 ///
806 /// This example shows how to flip the order of whitespace delimited
807 /// fields, and normalizes the whitespace that delimits the fields:
808 ///
809 /// ```
810 /// use regex_bites::bytes::Regex;
811 ///
812 /// let re = Regex::new(r"(?m)^(\S+)\s+(\S+)$").unwrap();
813 /// let hay = b"
814 /// Greetings 1973
815 /// Wild\t1973
816 /// BornToRun\t\t\t\t1975
817 /// Darkness 1978
818 /// TheRiver 1980
819 /// ";
820 /// let new = re.replace_all(hay, &b"$2 $1"[..]);
821 /// assert_eq!(new, &b"
822 /// 1973 Greetings
823 /// 1973 Wild
824 /// 1975 BornToRun
825 /// 1978 Darkness
826 /// 1980 TheRiver
827 /// "[..]);
828 /// ```
829 #[inline]
830 pub fn replace_all<'h, R: Replacer>(
831 &self,
832 haystack: &'h [u8],
833 rep: R,
834 ) -> Cow<'h, [u8]> {
835 self.replacen(haystack, 0, rep)
836 }
837
838 /// Replaces at most `limit` non-overlapping matches in the haystack with
839 /// the replacement provided. If `limit` is `0`, then all non-overlapping
840 /// matches are replaced. That is, `Regex::replace_all(hay, rep)` is
841 /// equivalent to `Regex::replacen(hay, 0, rep)`.
842 ///
843 /// The documentation for [`Regex::replace`] goes into more detail about
844 /// what kinds of replacement strings are supported.
845 ///
846 /// # Time complexity
847 ///
848 /// Since iterators over all matches requires running potentially many
849 /// searches on the haystack, and since each search has worst case
850 /// `O(m * n)` time complexity, the overall worst case time complexity for
851 /// this routine is `O(m * n^2)`.
852 ///
853 /// Although note that the worst case time here has an upper bound given
854 /// by the `limit` parameter.
855 ///
856 /// # Fallibility
857 ///
858 /// See the corresponding section in the docs for [`Regex::replace_all`]
859 /// for tips on how to deal with a replacement routine that can fail.
860 ///
861 /// # Example
862 ///
863 /// This example shows how to flip the order of whitespace delimited
864 /// fields, and normalizes the whitespace that delimits the fields. But we
865 /// only do it for the first two matches.
866 ///
867 /// ```
868 /// use regex_bites::bytes::Regex;
869 ///
870 /// let re = Regex::new(r"(?m)^(\S+)\s+(\S+)$").unwrap();
871 /// let hay = b"
872 /// Greetings 1973
873 /// Wild\t1973
874 /// BornToRun\t\t\t\t1975
875 /// Darkness 1978
876 /// TheRiver 1980
877 /// ";
878 /// let new = re.replacen(hay, 2, &b"$2 $1"[..]);
879 /// assert_eq!(new, &b"
880 /// 1973 Greetings
881 /// 1973 Wild
882 /// BornToRun\t\t\t\t1975
883 /// Darkness 1978
884 /// TheRiver 1980
885 /// "[..]);
886 /// ```
887 #[inline]
888 pub fn replacen<'h, R: Replacer>(
889 &self,
890 haystack: &'h [u8],
891 limit: usize,
892 mut rep: R,
893 ) -> Cow<'h, [u8]> {
894 // If we know that the replacement doesn't have any capture expansions,
895 // then we can use the fast path. The fast path can make a tremendous
896 // difference:
897 //
898 // 1) We use `find_iter` instead of `captures_iter`. Not asking for
899 // captures generally makes the regex engines faster.
900 // 2) We don't need to look up all of the capture groups and do
901 // replacements inside the replacement string. We just push it
902 // at each match and be done with it.
903 if let Some(rep) = rep.no_expansion() {
904 let mut it = self.find_iter(haystack).enumerate().peekable();
905 if it.peek().is_none() {
906 return Cow::Borrowed(haystack);
907 }
908 let mut new = Vec::with_capacity(haystack.len());
909 let mut last_match = 0;
910 for (i, m) in it {
911 new.extend(&haystack[last_match..m.start()]);
912 new.extend(&*rep);
913 last_match = m.end();
914 if limit > 0 && i >= limit - 1 {
915 break;
916 }
917 }
918 new.extend(&haystack[last_match..]);
919 return Cow::Owned(new);
920 }
921
922 // The slower path, which we use if the replacement needs access to
923 // capture groups.
924 let mut it = self.captures_iter(haystack).enumerate().peekable();
925 if it.peek().is_none() {
926 return Cow::Borrowed(haystack);
927 }
928 let mut new = Vec::with_capacity(haystack.len());
929 let mut last_match = 0;
930 for (i, cap) in it {
931 // unwrap on 0 is OK because captures only reports matches
932 let m = cap.get(0).unwrap();
933 new.extend(&haystack[last_match..m.start()]);
934 rep.replace_append(&cap, &mut new);
935 last_match = m.end();
936 if limit > 0 && i >= limit - 1 {
937 break;
938 }
939 }
940 new.extend(&haystack[last_match..]);
941 Cow::Owned(new)
942 }
943}
944
945/// A group of advanced or "lower level" search methods. Some methods permit
946/// starting the search at a position greater than `0` in the haystack. Other
947/// methods permit reusing allocations, for example, when extracting the
948/// matches for capture groups.
949impl Regex {
950 /// Returns the end byte offset of the first match in the haystack given.
951 ///
952 /// This method may have the same performance characteristics as
953 /// `is_match`. Behaviorlly, it doesn't just report whether it match
954 /// occurs, but also the end offset for a match. In particular, the offset
955 /// returned *may be shorter* than the proper end of the leftmost-first
956 /// match that you would find via [`Regex::find`].
957 ///
958 /// Note that it is not guaranteed that this routine finds the shortest or
959 /// "earliest" possible match. Instead, the main idea of this API is that
960 /// it returns the offset at the point at which the internal regex engine
961 /// has determined that a match has occurred. This may vary depending on
962 /// which internal regex engine is used, and thus, the offset itself may
963 /// change based on internal heuristics.
964 ///
965 /// # Example
966 ///
967 /// Typically, `a+` would match the entire first sequence of `a` in some
968 /// haystack, but `shortest_match` *may* give up as soon as it sees the
969 /// first `a`.
970 ///
971 /// ```
972 /// use regex_bites::bytes::Regex;
973 ///
974 /// let re = Regex::new(r"a+").unwrap();
975 /// let offset = re.shortest_match(b"aaaaa").unwrap();
976 /// assert_eq!(offset, 1);
977 /// ```
978 #[inline]
979 pub fn shortest_match(&self, haystack: &[u8]) -> Option<usize> {
980 self.shortest_match_at(haystack, 0)
981 }
982
983 /// Returns the same as [`Regex::shortest_match`], but starts the search at
984 /// the given offset.
985 ///
986 /// The significance of the starting point is that it takes the surrounding
987 /// context into consideration. For example, the `\A` anchor can only match
988 /// when `start == 0`.
989 ///
990 /// If a match is found, the offset returned is relative to the beginning
991 /// of the haystack, not the beginning of the search.
992 ///
993 /// # Panics
994 ///
995 /// This panics when `start >= haystack.len() + 1`.
996 ///
997 /// # Example
998 ///
999 /// This example shows the significance of `start` by demonstrating how it
1000 /// can be used to permit look-around assertions in a regex to take the
1001 /// surrounding context into account.
1002 ///
1003 /// ```
1004 /// use regex_bites::bytes::Regex;
1005 ///
1006 /// let re = Regex::new(r"\bchew\b").unwrap();
1007 /// let hay = b"eschew";
1008 /// // We get a match here, but it's probably not intended.
1009 /// assert_eq!(re.shortest_match(&hay[2..]), Some(4));
1010 /// // No match because the assertions take the context into account.
1011 /// assert_eq!(re.shortest_match_at(hay, 2), None);
1012 /// ```
1013 #[inline]
1014 pub fn shortest_match_at(
1015 &self,
1016 haystack: &[u8],
1017 start: usize,
1018 ) -> Option<usize> {
1019 let mut cache = self.pool.get();
1020 let mut slots = [None, None];
1021 let matched = self.pikevm.search(
1022 &mut cache,
1023 haystack,
1024 start,
1025 haystack.len(),
1026 true,
1027 &mut slots,
1028 );
1029 if !matched {
1030 return None;
1031 }
1032 Some(slots[1].unwrap().get())
1033 }
1034
1035 /// Returns the same as [`Regex::is_match`], but starts the search at the
1036 /// given offset.
1037 ///
1038 /// The significance of the starting point is that it takes the surrounding
1039 /// context into consideration. For example, the `\A` anchor can only
1040 /// match when `start == 0`.
1041 ///
1042 /// # Panics
1043 ///
1044 /// This panics when `start >= haystack.len() + 1`.
1045 ///
1046 /// # Example
1047 ///
1048 /// This example shows the significance of `start` by demonstrating how it
1049 /// can be used to permit look-around assertions in a regex to take the
1050 /// surrounding context into account.
1051 ///
1052 /// ```
1053 /// use regex_bites::bytes::Regex;
1054 ///
1055 /// let re = Regex::new(r"\bchew\b").unwrap();
1056 /// let hay = b"eschew";
1057 /// // We get a match here, but it's probably not intended.
1058 /// assert!(re.is_match(&hay[2..]));
1059 /// // No match because the assertions take the context into account.
1060 /// assert!(!re.is_match_at(hay, 2));
1061 /// ```
1062 #[inline]
1063 pub fn is_match_at(&self, haystack: &[u8], start: usize) -> bool {
1064 let mut cache = self.pool.get();
1065 self.pikevm.search(
1066 &mut cache,
1067 haystack,
1068 start,
1069 haystack.len(),
1070 true,
1071 &mut [],
1072 )
1073 }
1074
1075 /// Returns the same as [`Regex::find`], but starts the search at the given
1076 /// offset.
1077 ///
1078 /// The significance of the starting point is that it takes the surrounding
1079 /// context into consideration. For example, the `\A` anchor can only
1080 /// match when `start == 0`.
1081 ///
1082 /// # Panics
1083 ///
1084 /// This panics when `start >= haystack.len() + 1`.
1085 ///
1086 /// # Example
1087 ///
1088 /// This example shows the significance of `start` by demonstrating how it
1089 /// can be used to permit look-around assertions in a regex to take the
1090 /// surrounding context into account.
1091 ///
1092 /// ```
1093 /// use regex_bites::bytes::Regex;
1094 ///
1095 /// let re = Regex::new(r"\bchew\b").unwrap();
1096 /// let hay = b"eschew";
1097 /// // We get a match here, but it's probably not intended.
1098 /// assert_eq!(re.find(&hay[2..]).map(|m| m.range()), Some(0..4));
1099 /// // No match because the assertions take the context into account.
1100 /// assert_eq!(re.find_at(hay, 2), None);
1101 /// ```
1102 #[inline]
1103 pub fn find_at<'h>(
1104 &self,
1105 haystack: &'h [u8],
1106 start: usize,
1107 ) -> Option<Match<'h>> {
1108 let mut cache = self.pool.get();
1109 let mut slots = [None, None];
1110 let matched = self.pikevm.search(
1111 &mut cache,
1112 haystack,
1113 start,
1114 haystack.len(),
1115 false,
1116 &mut slots,
1117 );
1118 if !matched {
1119 return None;
1120 }
1121 let (start, end) = (slots[0].unwrap().get(), slots[1].unwrap().get());
1122 Some(Match::new(haystack, start, end))
1123 }
1124
1125 /// Returns the same as [`Regex::captures`], but starts the search at the
1126 /// given offset.
1127 ///
1128 /// The significance of the starting point is that it takes the surrounding
1129 /// context into consideration. For example, the `\A` anchor can only
1130 /// match when `start == 0`.
1131 ///
1132 /// # Panics
1133 ///
1134 /// This panics when `start >= haystack.len() + 1`.
1135 ///
1136 /// # Example
1137 ///
1138 /// This example shows the significance of `start` by demonstrating how it
1139 /// can be used to permit look-around assertions in a regex to take the
1140 /// surrounding context into account.
1141 ///
1142 /// ```
1143 /// use regex_bites::bytes::Regex;
1144 ///
1145 /// let re = Regex::new(r"\bchew\b").unwrap();
1146 /// let hay = b"eschew";
1147 /// // We get a match here, but it's probably not intended.
1148 /// assert_eq!(&re.captures(&hay[2..]).unwrap()[0], b"chew");
1149 /// // No match because the assertions take the context into account.
1150 /// assert!(re.captures_at(hay, 2).is_none());
1151 /// ```
1152 #[inline]
1153 pub fn captures_at<'h>(
1154 &self,
1155 haystack: &'h [u8],
1156 start: usize,
1157 ) -> Option<Captures<'h>> {
1158 let mut caps = Captures {
1159 haystack,
1160 slots: self.capture_locations(),
1161 pikevm: Arc::clone(&self.pikevm),
1162 };
1163 let mut cache = self.pool.get();
1164 let matched = self.pikevm.search(
1165 &mut cache,
1166 haystack,
1167 start,
1168 haystack.len(),
1169 false,
1170 &mut caps.slots.0,
1171 );
1172 if !matched {
1173 return None;
1174 }
1175 Some(caps)
1176 }
1177
1178 /// This is like [`Regex::captures`], but writes the byte offsets of each
1179 /// capture group match into the locations given.
1180 ///
1181 /// A [`CaptureLocations`] stores the same byte offsets as a [`Captures`],
1182 /// but does *not* store a reference to the haystack. This makes its API
1183 /// a bit lower level and less convenience. But in exchange, callers
1184 /// may allocate their own `CaptureLocations` and reuse it for multiple
1185 /// searches. This may be helpful if allocating a `Captures` shows up in a
1186 /// profile as too costly.
1187 ///
1188 /// To create a `CaptureLocations` value, use the
1189 /// [`Regex::capture_locations`] method.
1190 ///
1191 /// This also returns the overall match if one was found. When a match is
1192 /// found, its offsets are also always stored in `locs` at index `0`.
1193 ///
1194 /// # Panics
1195 ///
1196 /// This routine may panic if the given `CaptureLocations` was not created
1197 /// by this regex.
1198 ///
1199 /// # Example
1200 ///
1201 /// ```
1202 /// use regex_bites::bytes::Regex;
1203 ///
1204 /// let re = Regex::new(r"^([a-z]+)=(\S*)$").unwrap();
1205 /// let mut locs = re.capture_locations();
1206 /// assert!(re.captures_read(&mut locs, b"id=foo123").is_some());
1207 /// assert_eq!(Some((0, 9)), locs.get(0));
1208 /// assert_eq!(Some((0, 2)), locs.get(1));
1209 /// assert_eq!(Some((3, 9)), locs.get(2));
1210 /// ```
1211 #[inline]
1212 pub fn captures_read<'h>(
1213 &self,
1214 locs: &mut CaptureLocations,
1215 haystack: &'h [u8],
1216 ) -> Option<Match<'h>> {
1217 self.captures_read_at(locs, haystack, 0)
1218 }
1219
1220 /// Returns the same as [`Regex::captures_read`], but starts the search at
1221 /// the given offset.
1222 ///
1223 /// The significance of the starting point is that it takes the surrounding
1224 /// context into consideration. For example, the `\A` anchor can only
1225 /// match when `start == 0`.
1226 ///
1227 /// # Panics
1228 ///
1229 /// This panics when `start >= haystack.len() + 1`.
1230 ///
1231 /// This routine may also panic if the given `CaptureLocations` was not
1232 /// created by this regex.
1233 ///
1234 /// # Example
1235 ///
1236 /// This example shows the significance of `start` by demonstrating how it
1237 /// can be used to permit look-around assertions in a regex to take the
1238 /// surrounding context into account.
1239 ///
1240 /// ```
1241 /// use regex_bites::bytes::Regex;
1242 ///
1243 /// let re = Regex::new(r"\bchew\b").unwrap();
1244 /// let hay = b"eschew";
1245 /// let mut locs = re.capture_locations();
1246 /// // We get a match here, but it's probably not intended.
1247 /// assert!(re.captures_read(&mut locs, &hay[2..]).is_some());
1248 /// // No match because the assertions take the context into account.
1249 /// assert!(re.captures_read_at(&mut locs, hay, 2).is_none());
1250 /// ```
1251 #[inline]
1252 pub fn captures_read_at<'h>(
1253 &self,
1254 locs: &mut CaptureLocations,
1255 haystack: &'h [u8],
1256 start: usize,
1257 ) -> Option<Match<'h>> {
1258 let mut cache = self.pool.get();
1259 let matched = self.pikevm.search(
1260 &mut cache,
1261 haystack,
1262 start,
1263 haystack.len(),
1264 false,
1265 &mut locs.0,
1266 );
1267 if !matched {
1268 return None;
1269 }
1270 let (start, end) = locs.get(0).unwrap();
1271 Some(Match::new(haystack, start, end))
1272 }
1273}
1274
1275/// Auxiliary methods.
1276impl Regex {
1277 /// Returns the original string of this regex.
1278 ///
1279 /// # Example
1280 ///
1281 /// ```
1282 /// use regex_bites::bytes::Regex;
1283 ///
1284 /// let re = Regex::new(r"foo\w+bar").unwrap();
1285 /// assert_eq!(re.as_str(), r"foo\w+bar");
1286 /// ```
1287 #[inline]
1288 pub fn as_str(&self) -> &str {
1289 &self.pikevm.nfa().pattern()
1290 }
1291
1292 /// Returns an iterator over the capture names in this regex.
1293 ///
1294 /// The iterator returned yields elements of type `Option<&str>`. That is,
1295 /// the iterator yields values for all capture groups, even ones that are
1296 /// unnamed. The order of the groups corresponds to the order of the group's
1297 /// corresponding opening parenthesis.
1298 ///
1299 /// The first element of the iterator always yields the group corresponding
1300 /// to the overall match, and this group is always unnamed. Therefore, the
1301 /// iterator always yields at least one group.
1302 ///
1303 /// # Example
1304 ///
1305 /// This shows basic usage with a mix of named and unnamed capture groups:
1306 ///
1307 /// ```
1308 /// use regex_bites::bytes::Regex;
1309 ///
1310 /// let re = Regex::new(r"(?<a>.(?<b>.))(.)(?:.)(?<c>.)").unwrap();
1311 /// let mut names = re.capture_names();
1312 /// assert_eq!(names.next(), Some(None));
1313 /// assert_eq!(names.next(), Some(Some("a")));
1314 /// assert_eq!(names.next(), Some(Some("b")));
1315 /// assert_eq!(names.next(), Some(None));
1316 /// // the '(?:.)' group is non-capturing and so doesn't appear here!
1317 /// assert_eq!(names.next(), Some(Some("c")));
1318 /// assert_eq!(names.next(), None);
1319 /// ```
1320 ///
1321 /// The iterator always yields at least one element, even for regexes with
1322 /// no capture groups and even for regexes that can never match:
1323 ///
1324 /// ```
1325 /// use regex_bites::bytes::Regex;
1326 ///
1327 /// let re = Regex::new(r"").unwrap();
1328 /// let mut names = re.capture_names();
1329 /// assert_eq!(names.next(), Some(None));
1330 /// assert_eq!(names.next(), None);
1331 ///
1332 /// let re = Regex::new(r"[^\s\S]").unwrap();
1333 /// let mut names = re.capture_names();
1334 /// assert_eq!(names.next(), Some(None));
1335 /// assert_eq!(names.next(), None);
1336 /// ```
1337 #[inline]
1338 pub fn capture_names(&self) -> CaptureNames<'_> {
1339 CaptureNames(self.pikevm.nfa().capture_names())
1340 }
1341
1342 /// Returns the number of captures groups in this regex.
1343 ///
1344 /// This includes all named and unnamed groups, including the implicit
1345 /// unnamed group that is always present and corresponds to the entire
1346 /// match.
1347 ///
1348 /// Since the implicit unnamed group is always included in this length, the
1349 /// length returned is guaranteed to be greater than zero.
1350 ///
1351 /// # Example
1352 ///
1353 /// ```
1354 /// use regex_bites::bytes::Regex;
1355 ///
1356 /// let re = Regex::new(r"foo").unwrap();
1357 /// assert_eq!(1, re.captures_len());
1358 ///
1359 /// let re = Regex::new(r"(foo)").unwrap();
1360 /// assert_eq!(2, re.captures_len());
1361 ///
1362 /// let re = Regex::new(r"(?<a>.(?<b>.))(.)(?:.)(?<c>.)").unwrap();
1363 /// assert_eq!(5, re.captures_len());
1364 ///
1365 /// let re = Regex::new(r"[^\s\S]").unwrap();
1366 /// assert_eq!(1, re.captures_len());
1367 /// ```
1368 #[inline]
1369 pub fn captures_len(&self) -> usize {
1370 self.pikevm.nfa().group_len()
1371 }
1372
1373 /// Returns the total number of capturing groups that appear in every
1374 /// possible match.
1375 ///
1376 /// If the number of capture groups can vary depending on the match, then
1377 /// this returns `None`. That is, a value is only returned when the number
1378 /// of matching groups is invariant or "static."
1379 ///
1380 /// Note that like [`Regex::captures_len`], this **does** include the
1381 /// implicit capturing group corresponding to the entire match. Therefore,
1382 /// when a non-None value is returned, it is guaranteed to be at least `1`.
1383 /// Stated differently, a return value of `Some(0)` is impossible.
1384 ///
1385 /// # Example
1386 ///
1387 /// This shows a few cases where a static number of capture groups is
1388 /// available and a few cases where it is not.
1389 ///
1390 /// ```
1391 /// use regex_bites::bytes::Regex;
1392 ///
1393 /// let len = |pattern| {
1394 /// Regex::new(pattern).map(|re| re.static_captures_len())
1395 /// };
1396 ///
1397 /// assert_eq!(Some(1), len("a")?);
1398 /// assert_eq!(Some(2), len("(a)")?);
1399 /// assert_eq!(Some(2), len("(a)|(b)")?);
1400 /// assert_eq!(Some(3), len("(a)(b)|(c)(d)")?);
1401 /// assert_eq!(None, len("(a)|b")?);
1402 /// assert_eq!(None, len("a|(b)")?);
1403 /// assert_eq!(None, len("(b)*")?);
1404 /// assert_eq!(Some(2), len("(b)+")?);
1405 ///
1406 /// # Ok::<(), Box<dyn std::error::Error>>(())
1407 /// ```
1408 #[inline]
1409 pub fn static_captures_len(&self) -> Option<usize> {
1410 self.pikevm
1411 .nfa()
1412 .static_explicit_captures_len()
1413 .map(|len| len.saturating_add(1))
1414 }
1415
1416 /// Returns a fresh allocated set of capture locations that can
1417 /// be reused in multiple calls to [`Regex::captures_read`] or
1418 /// [`Regex::captures_read_at`].
1419 ///
1420 /// The returned locations can be used for any subsequent search for this
1421 /// particular regex. There is no guarantee that it is correct to use for
1422 /// other regexes, even if they have the same number of capture groups.
1423 ///
1424 /// # Example
1425 ///
1426 /// ```
1427 /// use regex_bites::bytes::Regex;
1428 ///
1429 /// let re = Regex::new(r"(.)(.)(\w+)").unwrap();
1430 /// let mut locs = re.capture_locations();
1431 /// assert!(re.captures_read(&mut locs, b"Padron").is_some());
1432 /// assert_eq!(locs.get(0), Some((0, 6)));
1433 /// assert_eq!(locs.get(1), Some((0, 1)));
1434 /// assert_eq!(locs.get(2), Some((1, 2)));
1435 /// assert_eq!(locs.get(3), Some((2, 6)));
1436 /// ```
1437 #[inline]
1438 pub fn capture_locations(&self) -> CaptureLocations {
1439 // OK because NFA construction would have failed if this overflowed.
1440 let len = self.pikevm.nfa().group_len().checked_mul(2).unwrap();
1441 CaptureLocations(vec![None; len])
1442 }
1443}
1444
1445/// Represents a single match of a regex in a haystack.
1446///
1447/// A `Match` contains both the start and end byte offsets of the match and the
1448/// actual substring corresponding to the range of those byte offsets. It is
1449/// guaranteed that `start <= end`. When `start == end`, the match is empty.
1450///
1451/// Since this `Match` can only be produced by the top-level `Regex` APIs
1452/// that only support searching UTF-8 encoded strings, the byte offsets for a
1453/// `Match` are guaranteed to fall on valid UTF-8 codepoint boundaries. That
1454/// is, slicing a `&str` with [`Match::range`] is guaranteed to never panic.
1455///
1456/// Values with this type are created by [`Regex::find`] or
1457/// [`Regex::find_iter`]. Other APIs can create `Match` values too. For
1458/// example, [`Captures::get`].
1459///
1460/// The lifetime parameter `'h` refers to the lifetime of the matched of the
1461/// haystack that this match was produced from.
1462///
1463/// # Numbering
1464///
1465/// The byte offsets in a `Match` form a half-open interval. That is, the
1466/// start of the range is inclusive and the end of the range is exclusive.
1467/// For example, given a haystack `abcFOOxyz` and a match of `FOO`, its byte
1468/// offset range starts at `3` and ends at `6`. `3` corresponds to `F` and
1469/// `6` corresponds to `x`, which is one past the end of the match. This
1470/// corresponds to the same kind of slicing that Rust uses.
1471///
1472/// For more on why this was chosen over other schemes (aside from being
1473/// consistent with how Rust the language works), see [this discussion] and
1474/// [Dijkstra's note on a related topic][note].
1475///
1476/// [this discussion]: https://github.com/rust-lang/regex/discussions/866
1477/// [note]: https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
1478///
1479/// # Example
1480///
1481/// This example shows the value of each of the methods on `Match` for a
1482/// particular search.
1483///
1484/// ```
1485/// use regex_bites::bytes::Regex;
1486///
1487/// let re = Regex::new(r"\d+").unwrap();
1488/// let hay = b"numbers: 1234";
1489/// let m = re.find(hay).unwrap();
1490/// assert_eq!(9, m.start());
1491/// assert_eq!(13, m.end());
1492/// assert!(!m.is_empty());
1493/// assert_eq!(4, m.len());
1494/// assert_eq!(9..13, m.range());
1495/// assert_eq!(b"1234", m.as_bytes());
1496/// ```
1497#[derive(Copy, Clone, Eq, PartialEq)]
1498pub struct Match<'h> {
1499 haystack: &'h [u8],
1500 start: usize,
1501 end: usize,
1502}
1503
1504impl<'h> Match<'h> {
1505 /// Creates a new match from the given haystack and byte offsets.
1506 #[inline]
1507 fn new(haystack: &'h [u8], start: usize, end: usize) -> Match<'h> {
1508 Match { haystack, start, end }
1509 }
1510
1511 /// Returns the byte offset of the start of the match in the haystack. The
1512 /// start of the match corresponds to the position where the match begins
1513 /// and includes the first byte in the match.
1514 ///
1515 /// It is guaranteed that `Match::start() <= Match::end()`.
1516 ///
1517 /// This is guaranteed to fall on a valid UTF-8 codepoint boundary. That
1518 /// is, it will never be an offset that appears between the UTF-8 code
1519 /// units of a UTF-8 encoded Unicode scalar value. Consequently, it is
1520 /// always safe to slice the corresponding haystack using this offset.
1521 #[inline]
1522 pub fn start(&self) -> usize {
1523 self.start
1524 }
1525
1526 /// Returns the byte offset of the end of the match in the haystack. The
1527 /// end of the match corresponds to the byte immediately following the last
1528 /// byte in the match. This means that `&slice[start..end]` works as one
1529 /// would expect.
1530 ///
1531 /// It is guaranteed that `Match::start() <= Match::end()`.
1532 ///
1533 /// This is guaranteed to fall on a valid UTF-8 codepoint boundary. That
1534 /// is, it will never be an offset that appears between the UTF-8 code
1535 /// units of a UTF-8 encoded Unicode scalar value. Consequently, it is
1536 /// always safe to slice the corresponding haystack using this offset.
1537 #[inline]
1538 pub fn end(&self) -> usize {
1539 self.end
1540 }
1541
1542 /// Returns true if and only if this match has a length of zero.
1543 ///
1544 /// Note that an empty match can only occur when the regex itself can
1545 /// match the empty string. Here are some examples of regexes that can
1546 /// all match the empty string: `^`, `^$`, `\b`, `a?`, `a*`, `a{0}`,
1547 /// `(foo|\d+|quux)?`.
1548 #[inline]
1549 pub fn is_empty(&self) -> bool {
1550 self.start == self.end
1551 }
1552
1553 /// Returns the length, in bytes, of this match.
1554 #[inline]
1555 pub fn len(&self) -> usize {
1556 self.end - self.start
1557 }
1558
1559 /// Returns the range over the starting and ending byte offsets of the
1560 /// match in the haystack.
1561 ///
1562 /// It is always correct to slice the original haystack searched with this
1563 /// range. That is, because the offsets are guaranteed to fall on valid
1564 /// UTF-8 boundaries, the range returned is always valid.
1565 #[inline]
1566 pub fn range(&self) -> core::ops::Range<usize> {
1567 self.start..self.end
1568 }
1569
1570 /// Returns the substring of the haystack that matched.
1571 #[inline]
1572 pub fn as_bytes(&self) -> &'h [u8] {
1573 &self.haystack[self.range()]
1574 }
1575}
1576
1577impl<'h> core::fmt::Debug for Match<'h> {
1578 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
1579 f.debug_struct("Match")
1580 .field("start", &self.start)
1581 .field("end", &self.end)
1582 .field("bytes", &self.as_bytes())
1583 .finish()
1584 }
1585}
1586
1587impl<'h> From<Match<'h>> for &'h [u8] {
1588 fn from(m: Match<'h>) -> &'h [u8] {
1589 m.as_bytes()
1590 }
1591}
1592
1593impl<'h> From<Match<'h>> for core::ops::Range<usize> {
1594 fn from(m: Match<'h>) -> core::ops::Range<usize> {
1595 m.range()
1596 }
1597}
1598
1599/// Represents the capture groups for a single match.
1600///
1601/// Capture groups refer to parts of a regex enclosed in parentheses. They can
1602/// be optionally named. The purpose of capture groups is to be able to
1603/// reference different parts of a match based on the original pattern. For
1604/// example, say you want to match the individual letters in a 5-letter word:
1605///
1606/// ```text
1607/// (?<first>\w)(\w)(?:\w)\w(?<last>\w)
1608/// ```
1609///
1610/// This regex has 4 capture groups:
1611///
1612/// * The group at index `0` corresponds to the overall match. It is always
1613/// present in every match and never has a name.
1614/// * The group at index `1` with name `first` corresponding to the first
1615/// letter.
1616/// * The group at index `2` with no name corresponding to the second letter.
1617/// * The group at index `3` with name `last` corresponding to the fifth and
1618/// last letter.
1619///
1620/// Notice that `(?:\w)` was not listed above as a capture group despite it
1621/// being enclosed in parentheses. That's because `(?:pattern)` is a special
1622/// syntax that permits grouping but *without* capturing. The reason for not
1623/// treating it as a capture is that tracking and reporting capture groups
1624/// requires additional state that may lead to slower searches. So using as few
1625/// capture groups as possible can help performance. (Although the difference
1626/// in performance of a couple of capture groups is likely immaterial.)
1627///
1628/// Values with this type are created by [`Regex::captures`] or
1629/// [`Regex::captures_iter`].
1630///
1631/// `'h` is the lifetime of the haystack that these captures were matched from.
1632///
1633/// # Example
1634///
1635/// ```
1636/// use regex_bites::bytes::Regex;
1637///
1638/// let re = Regex::new(r"(?<first>\w)(\w)(?:\w)\w(?<last>\w)").unwrap();
1639/// let caps = re.captures(b"toady").unwrap();
1640/// assert_eq!(b"toady", &caps[0]);
1641/// assert_eq!(b"t", &caps["first"]);
1642/// assert_eq!(b"o", &caps[2]);
1643/// assert_eq!(b"y", &caps["last"]);
1644/// ```
1645pub struct Captures<'h> {
1646 haystack: &'h [u8],
1647 slots: CaptureLocations,
1648 // It's a little weird to put the PikeVM in our Captures, but it's the
1649 // simplest thing to do and is cheap. The PikeVM gives us access to the
1650 // NFA and the NFA gives us access to the capture name<->index mapping.
1651 pikevm: Arc<PikeVM>,
1652}
1653
1654impl<'h> Captures<'h> {
1655 /// Returns the `Match` associated with the capture group at index `i`. If
1656 /// `i` does not correspond to a capture group, or if the capture group did
1657 /// not participate in the match, then `None` is returned.
1658 ///
1659 /// When `i == 0`, this is guaranteed to return a non-`None` value.
1660 ///
1661 /// # Examples
1662 ///
1663 /// Get the substring that matched with a default of an empty string if the
1664 /// group didn't participate in the match:
1665 ///
1666 /// ```
1667 /// use regex_bites::bytes::Regex;
1668 ///
1669 /// let re = Regex::new(r"[a-z]+(?:([0-9]+)|([A-Z]+))").unwrap();
1670 /// let caps = re.captures(b"abc123").unwrap();
1671 ///
1672 /// let substr1 = caps.get(1).map_or(&b""[..], |m| m.as_bytes());
1673 /// let substr2 = caps.get(2).map_or(&b""[..], |m| m.as_bytes());
1674 /// assert_eq!(substr1, &b"123"[..]);
1675 /// assert_eq!(substr2, &b""[..]);
1676 /// ```
1677 #[inline]
1678 pub fn get(&self, i: usize) -> Option<Match<'h>> {
1679 self.slots.get(i).map(|(s, e)| Match::new(self.haystack, s, e))
1680 }
1681
1682 /// Returns the `Match` associated with the capture group named `name`. If
1683 /// `name` isn't a valid capture group or it refers to a group that didn't
1684 /// match, then `None` is returned.
1685 ///
1686 /// Note that unlike `caps["name"]`, this returns a `Match` whose lifetime
1687 /// matches the lifetime of the haystack in this `Captures` value.
1688 /// Conversely, the substring returned by `caps["name"]` has a lifetime
1689 /// of the `Captures` value, which is likely shorter than the lifetime of
1690 /// the haystack. In some cases, it may be necessary to use this method to
1691 /// access the matching substring instead of the `caps["name"]` notation.
1692 ///
1693 /// # Examples
1694 ///
1695 /// Get the substring that matched with a default of an empty string if the
1696 /// group didn't participate in the match:
1697 ///
1698 /// ```
1699 /// use regex_bites::bytes::Regex;
1700 ///
1701 /// let re = Regex::new(
1702 /// r"[a-z]+(?:(?<numbers>[0-9]+)|(?<letters>[A-Z]+))",
1703 /// ).unwrap();
1704 /// let caps = re.captures(b"abc123").unwrap();
1705 ///
1706 /// let numbers = caps.name("numbers").map_or(&b""[..], |m| m.as_bytes());
1707 /// let letters = caps.name("letters").map_or(&b""[..], |m| m.as_bytes());
1708 /// assert_eq!(numbers, &b"123"[..]);
1709 /// assert_eq!(letters, &b""[..]);
1710 /// ```
1711 #[inline]
1712 pub fn name(&self, name: &str) -> Option<Match<'h>> {
1713 let i = self.pikevm.nfa().to_index(name)?;
1714 self.get(i)
1715 }
1716
1717 /// This is a convenience routine for extracting the substrings
1718 /// corresponding to matching capture groups.
1719 ///
1720 /// This returns a tuple where the first element corresponds to the full
1721 /// substring of the haystack that matched the regex. The second element is
1722 /// an array of substrings, with each corresponding to the substring that
1723 /// matched for a particular capture group.
1724 ///
1725 /// # Panics
1726 ///
1727 /// This panics if the number of possible matching groups in this
1728 /// `Captures` value is not fixed to `N` in all circumstances.
1729 /// More precisely, this routine only works when `N` is equivalent to
1730 /// [`Regex::static_captures_len`].
1731 ///
1732 /// Stated more plainly, if the number of matching capture groups in a
1733 /// regex can vary from match to match, then this function always panics.
1734 ///
1735 /// For example, `(a)(b)|(c)` could produce two matching capture groups
1736 /// or one matching capture group for any given match. Therefore, one
1737 /// cannot use `extract` with such a pattern.
1738 ///
1739 /// But a pattern like `(a)(b)|(c)(d)` can be used with `extract` because
1740 /// the number of capture groups in every match is always equivalent,
1741 /// even if the capture _indices_ in each match are not.
1742 ///
1743 /// # Example
1744 ///
1745 /// ```
1746 /// use regex_bites::bytes::Regex;
1747 ///
1748 /// let re = Regex::new(r"([0-9]{4})-([0-9]{2})-([0-9]{2})").unwrap();
1749 /// let hay = b"On 2010-03-14, I became a Tenneessee lamb.";
1750 /// let Some((full, [year, month, day])) =
1751 /// re.captures(hay).map(|caps| caps.extract()) else { return };
1752 /// assert_eq!(b"2010-03-14", full);
1753 /// assert_eq!(b"2010", year);
1754 /// assert_eq!(b"03", month);
1755 /// assert_eq!(b"14", day);
1756 /// ```
1757 ///
1758 /// # Example: iteration
1759 ///
1760 /// This example shows how to use this method when iterating over all
1761 /// `Captures` matches in a haystack.
1762 ///
1763 /// ```
1764 /// use regex_bites::bytes::Regex;
1765 ///
1766 /// let re = Regex::new(r"([0-9]{4})-([0-9]{2})-([0-9]{2})").unwrap();
1767 /// let hay = b"1973-01-05, 1975-08-25 and 1980-10-18";
1768 ///
1769 /// let mut dates: Vec<(&[u8], &[u8], &[u8])> = vec![];
1770 /// for (_, [y, m, d]) in re.captures_iter(hay).map(|c| c.extract()) {
1771 /// dates.push((y, m, d));
1772 /// }
1773 /// assert_eq!(dates, vec![
1774 /// (&b"1973"[..], &b"01"[..], &b"05"[..]),
1775 /// (&b"1975"[..], &b"08"[..], &b"25"[..]),
1776 /// (&b"1980"[..], &b"10"[..], &b"18"[..]),
1777 /// ]);
1778 /// ```
1779 ///
1780 /// # Example: parsing different formats
1781 ///
1782 /// This API is particularly useful when you need to extract a particular
1783 /// value that might occur in a different format. Consider, for example,
1784 /// an identifier that might be in double quotes or single quotes:
1785 ///
1786 /// ```
1787 /// use regex_bites::bytes::Regex;
1788 ///
1789 /// let re = Regex::new(r#"id:(?:"([^"]+)"|'([^']+)')"#).unwrap();
1790 /// let hay = br#"The first is id:"foo" and the second is id:'bar'."#;
1791 /// let mut ids = vec![];
1792 /// for (_, [id]) in re.captures_iter(hay).map(|c| c.extract()) {
1793 /// ids.push(id);
1794 /// }
1795 /// assert_eq!(ids, vec![b"foo", b"bar"]);
1796 /// ```
1797 pub fn extract<const N: usize>(&self) -> (&'h [u8], [&'h [u8]; N]) {
1798 let len = self
1799 .pikevm
1800 .nfa()
1801 .static_explicit_captures_len()
1802 .expect("number of capture groups can vary in a match");
1803 assert_eq!(N, len, "asked for {} groups, but must ask for {}", N, len);
1804 let mut matched = self.iter().flatten();
1805 let whole_match = matched.next().expect("a match").as_bytes();
1806 let group_matches = [0; N].map(|_| {
1807 matched.next().expect("too few matching groups").as_bytes()
1808 });
1809 (whole_match, group_matches)
1810 }
1811
1812 /// Expands all instances of `$ref` in `replacement` to the corresponding
1813 /// capture group, and writes them to the `dst` buffer given. A `ref` can
1814 /// be a capture group index or a name. If `ref` doesn't refer to a capture
1815 /// group that participated in the match, then it is replaced with the
1816 /// empty string.
1817 ///
1818 /// # Format
1819 ///
1820 /// The format of the replacement string supports two different kinds of
1821 /// capture references: unbraced and braced.
1822 ///
1823 /// For the unbraced format, the format supported is `$ref` where `name`
1824 /// can be any character in the class `[0-9A-Za-z_]`. `ref` is always
1825 /// the longest possible parse. So for example, `$1a` corresponds to the
1826 /// capture group named `1a` and not the capture group at index `1`. If
1827 /// `ref` matches `^[0-9]+$`, then it is treated as a capture group index
1828 /// itself and not a name.
1829 ///
1830 /// For the braced format, the format supported is `${ref}` where `ref` can
1831 /// be any sequence of bytes except for `}`. If no closing brace occurs,
1832 /// then it is not considered a capture reference. As with the unbraced
1833 /// format, if `ref` matches `^[0-9]+$`, then it is treated as a capture
1834 /// group index and not a name.
1835 ///
1836 /// The braced format is useful for exerting precise control over the name
1837 /// of the capture reference. For example, `${1}a` corresponds to the
1838 /// capture group reference `1` followed by the letter `a`, where as `$1a`
1839 /// (as mentioned above) corresponds to the capture group reference `1a`.
1840 /// The braced format is also useful for expressing capture group names
1841 /// that use characters not supported by the unbraced format. For example,
1842 /// `${foo[bar].baz}` refers to the capture group named `foo[bar].baz`.
1843 ///
1844 /// If a capture group reference is found and it does not refer to a valid
1845 /// capture group, then it will be replaced with the empty string.
1846 ///
1847 /// To write a literal `$`, use `$$`.
1848 ///
1849 /// # Example
1850 ///
1851 /// ```
1852 /// use regex_bites::bytes::Regex;
1853 ///
1854 /// let re = Regex::new(
1855 /// r"(?<day>[0-9]{2})-(?<month>[0-9]{2})-(?<year>[0-9]{4})",
1856 /// ).unwrap();
1857 /// let hay = b"On 14-03-2010, I became a Tenneessee lamb.";
1858 /// let caps = re.captures(hay).unwrap();
1859 ///
1860 /// let mut dst = Vec::new();
1861 /// caps.expand(b"year=$year, month=$month, day=$day", &mut dst);
1862 /// assert_eq!(dst, b"year=2010, month=03, day=14");
1863 /// ```
1864 #[inline]
1865 pub fn expand(&self, replacement: &[u8], dst: &mut Vec<u8>) {
1866 interpolate::bytes(
1867 replacement,
1868 |index, dst| {
1869 let m = match self.get(index) {
1870 None => return,
1871 Some(m) => m,
1872 };
1873 dst.extend(&self.haystack[m.range()]);
1874 },
1875 |name| self.pikevm.nfa().to_index(name),
1876 dst,
1877 );
1878 }
1879
1880 /// Returns an iterator over all capture groups. This includes both
1881 /// matching and non-matching groups.
1882 ///
1883 /// The iterator always yields at least one matching group: the first group
1884 /// (at index `0`) with no name. Subsequent groups are returned in the order
1885 /// of their opening parenthesis in the regex.
1886 ///
1887 /// The elements yielded have type `Option<Match<'h>>`, where a non-`None`
1888 /// value is present if the capture group matches.
1889 ///
1890 /// # Example
1891 ///
1892 /// ```
1893 /// use regex_bites::bytes::Regex;
1894 ///
1895 /// let re = Regex::new(r"(\w)(\d)?(\w)").unwrap();
1896 /// let caps = re.captures(b"AZ").unwrap();
1897 ///
1898 /// let mut it = caps.iter();
1899 /// assert_eq!(it.next().unwrap().map(|m| m.as_bytes()), Some(&b"AZ"[..]));
1900 /// assert_eq!(it.next().unwrap().map(|m| m.as_bytes()), Some(&b"A"[..]));
1901 /// assert_eq!(it.next().unwrap().map(|m| m.as_bytes()), None);
1902 /// assert_eq!(it.next().unwrap().map(|m| m.as_bytes()), Some(&b"Z"[..]));
1903 /// assert_eq!(it.next(), None);
1904 /// ```
1905 #[inline]
1906 pub fn iter<'c>(&'c self) -> SubCaptureMatches<'c, 'h> {
1907 SubCaptureMatches {
1908 caps: self,
1909 it: self.pikevm.nfa().capture_names().enumerate(),
1910 }
1911 }
1912
1913 /// Returns the total number of capture groups. This includes both
1914 /// matching and non-matching groups.
1915 ///
1916 /// The length returned is always equivalent to the number of elements
1917 /// yielded by [`Captures::iter`]. Consequently, the length is always
1918 /// greater than zero since every `Captures` value always includes the
1919 /// match for the entire regex.
1920 ///
1921 /// # Example
1922 ///
1923 /// ```
1924 /// use regex_bites::bytes::Regex;
1925 ///
1926 /// let re = Regex::new(r"(\w)(\d)?(\w)").unwrap();
1927 /// let caps = re.captures(b"AZ").unwrap();
1928 /// assert_eq!(caps.len(), 4);
1929 /// ```
1930 #[inline]
1931 pub fn len(&self) -> usize {
1932 self.pikevm.nfa().group_len()
1933 }
1934}
1935
1936impl<'h> core::fmt::Debug for Captures<'h> {
1937 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
1938 /// A little helper type to provide a nice map-like debug
1939 /// representation for our capturing group spans.
1940 ///
1941 /// regex-automata has something similar, but it includes the pattern
1942 /// ID in its debug output, which is confusing. It also doesn't include
1943 /// that strings that match because a regex-automata `Captures` doesn't
1944 /// borrow the haystack.
1945 struct CapturesDebugMap<'a> {
1946 caps: &'a Captures<'a>,
1947 }
1948
1949 impl<'a> core::fmt::Debug for CapturesDebugMap<'a> {
1950 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1951 let mut map = f.debug_map();
1952 let names = self.caps.pikevm.nfa().capture_names();
1953 for (group_index, maybe_name) in names.enumerate() {
1954 let key = Key(group_index, maybe_name);
1955 match self.caps.get(group_index) {
1956 None => map.entry(&key, &None::<()>),
1957 Some(mat) => map.entry(&key, &Value(mat)),
1958 };
1959 }
1960 map.finish()
1961 }
1962 }
1963
1964 struct Key<'a>(usize, Option<&'a str>);
1965
1966 impl<'a> core::fmt::Debug for Key<'a> {
1967 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1968 write!(f, "{}", self.0)?;
1969 if let Some(name) = self.1 {
1970 write!(f, "/{:?}", name)?;
1971 }
1972 Ok(())
1973 }
1974 }
1975
1976 struct Value<'a>(Match<'a>);
1977
1978 impl<'a> core::fmt::Debug for Value<'a> {
1979 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
1980 write!(
1981 f,
1982 "{}..{}/{:?}",
1983 self.0.start(),
1984 self.0.end(),
1985 self.0.as_bytes()
1986 )
1987 }
1988 }
1989
1990 f.debug_tuple("Captures")
1991 .field(&CapturesDebugMap { caps: self })
1992 .finish()
1993 }
1994}
1995
1996/// Get a matching capture group's haystack substring by index.
1997///
1998/// The haystack substring returned can't outlive the `Captures` object if this
1999/// method is used, because of how `Index` is defined (normally `a[i]` is part
2000/// of `a` and can't outlive it). To work around this limitation, do that, use
2001/// [`Captures::get`] instead.
2002///
2003/// `'h` is the lifetime of the matched haystack, but the lifetime of the
2004/// `&str` returned by this implementation is the lifetime of the `Captures`
2005/// value itself.
2006///
2007/// # Panics
2008///
2009/// If there is no matching group at the given index.
2010impl<'h> core::ops::Index<usize> for Captures<'h> {
2011 type Output = [u8];
2012
2013 // The lifetime is written out to make it clear that the &str returned
2014 // does NOT have a lifetime equivalent to 'h.
2015 fn index(&self, i: usize) -> &[u8] {
2016 self.get(i)
2017 .map(|m| m.as_bytes())
2018 .unwrap_or_else(|| panic!("no group at index '{}'", i))
2019 }
2020}
2021
2022/// Get a matching capture group's haystack substring by name.
2023///
2024/// The haystack substring returned can't outlive the `Captures` object if this
2025/// method is used, because of how `Index` is defined (normally `a[i]` is part
2026/// of `a` and can't outlive it). To work around this limitation, do that, use
2027/// [`Captures::get`] instead.
2028///
2029/// `'h` is the lifetime of the matched haystack, but the lifetime of the
2030/// `&str` returned by this implementation is the lifetime of the `Captures`
2031/// value itself.
2032///
2033/// `'n` is the lifetime of the group name used to index the `Captures` value.
2034///
2035/// # Panics
2036///
2037/// If there is no matching group at the given name.
2038impl<'h, 'n> core::ops::Index<&'n str> for Captures<'h> {
2039 type Output = [u8];
2040
2041 fn index<'a>(&'a self, name: &'n str) -> &'a [u8] {
2042 self.name(name)
2043 .map(|m| m.as_bytes())
2044 .unwrap_or_else(|| panic!("no group named '{}'", name))
2045 }
2046}
2047
2048/// A low level representation of the byte offsets of each capture group.
2049///
2050/// You can think of this as a lower level [`Captures`], where this type does
2051/// not support named capturing groups directly and it does not borrow the
2052/// haystack that these offsets were matched on.
2053///
2054/// Primarily, this type is useful when using the lower level `Regex` APIs such
2055/// as [`Regex::captures_read`], which permits amortizing the allocation in
2056/// which capture match offsets are stored.
2057///
2058/// In order to build a value of this type, you'll need to call the
2059/// [`Regex::capture_locations`] method. The value returned can then be reused
2060/// in subsequent searches for that regex. Using it for other regexes may
2061/// result in a panic or otherwise incorrect results.
2062///
2063/// # Example
2064///
2065/// This example shows how to create and use `CaptureLocations` in a search.
2066///
2067/// ```
2068/// use regex_bites::bytes::Regex;
2069///
2070/// let re = Regex::new(r"(?<first>\w+)\s+(?<last>\w+)").unwrap();
2071/// let mut locs = re.capture_locations();
2072/// let m = re.captures_read(&mut locs, b"Bruce Springsteen").unwrap();
2073/// assert_eq!(0..17, m.range());
2074/// assert_eq!(Some((0, 17)), locs.get(0));
2075/// assert_eq!(Some((0, 5)), locs.get(1));
2076/// assert_eq!(Some((6, 17)), locs.get(2));
2077///
2078/// // Asking for an invalid capture group always returns None.
2079/// assert_eq!(None, locs.get(3));
2080/// # // literals are too big for 32-bit usize: #1041
2081/// # #[cfg(target_pointer_width = "64")]
2082/// assert_eq!(None, locs.get(34973498648));
2083/// # #[cfg(target_pointer_width = "64")]
2084/// assert_eq!(None, locs.get(9944060567225171988));
2085/// ```
2086#[derive(Clone, Debug)]
2087pub struct CaptureLocations(Vec<Option<NonMaxUsize>>);
2088
2089impl CaptureLocations {
2090 /// Returns the start and end byte offsets of the capture group at index
2091 /// `i`. This returns `None` if `i` is not a valid capture group or if the
2092 /// capture group did not match.
2093 ///
2094 /// # Example
2095 ///
2096 /// ```
2097 /// use regex_bites::bytes::Regex;
2098 ///
2099 /// let re = Regex::new(r"(?<first>\w+)\s+(?<last>\w+)").unwrap();
2100 /// let mut locs = re.capture_locations();
2101 /// re.captures_read(&mut locs, b"Bruce Springsteen").unwrap();
2102 /// assert_eq!(Some((0, 17)), locs.get(0));
2103 /// assert_eq!(Some((0, 5)), locs.get(1));
2104 /// assert_eq!(Some((6, 17)), locs.get(2));
2105 /// ```
2106 #[inline]
2107 pub fn get(&self, i: usize) -> Option<(usize, usize)> {
2108 let slot = i.checked_mul(2)?;
2109 let start = self.0.get(slot).copied()??.get();
2110 let slot = slot.checked_add(1)?;
2111 let end = self.0.get(slot).copied()??.get();
2112 Some((start, end))
2113 }
2114
2115 /// Returns the total number of capture groups (even if they didn't match).
2116 /// That is, the length returned is unaffected by the result of a search.
2117 ///
2118 /// This is always at least `1` since every regex has at least `1`
2119 /// capturing group that corresponds to the entire match.
2120 ///
2121 /// # Example
2122 ///
2123 /// ```
2124 /// use regex_bites::bytes::Regex;
2125 ///
2126 /// let re = Regex::new(r"(?<first>\w+)\s+(?<last>\w+)").unwrap();
2127 /// let mut locs = re.capture_locations();
2128 /// assert_eq!(3, locs.len());
2129 /// re.captures_read(&mut locs, b"Bruce Springsteen").unwrap();
2130 /// assert_eq!(3, locs.len());
2131 /// ```
2132 ///
2133 /// Notice that the length is always at least `1`, regardless of the regex:
2134 ///
2135 /// ```
2136 /// use regex_bites::bytes::Regex;
2137 ///
2138 /// let re = Regex::new(r"").unwrap();
2139 /// let locs = re.capture_locations();
2140 /// assert_eq!(1, locs.len());
2141 ///
2142 /// // [a&&b] is a regex that never matches anything.
2143 /// let re = Regex::new(r"[^\s\S]").unwrap();
2144 /// let locs = re.capture_locations();
2145 /// assert_eq!(1, locs.len());
2146 /// ```
2147 #[inline]
2148 pub fn len(&self) -> usize {
2149 // We always have twice as many slots as groups.
2150 self.0.len().checked_shr(1).unwrap()
2151 }
2152}
2153
2154/// An iterator over all non-overlapping matches in a haystack.
2155///
2156/// This iterator yields [`Match`] values. The iterator stops when no more
2157/// matches can be found.
2158///
2159/// `'r` is the lifetime of the compiled regular expression and `'h` is the
2160/// lifetime of the haystack.
2161///
2162/// This iterator is created by [`Regex::find_iter`].
2163///
2164/// # Time complexity
2165///
2166/// Note that since an iterator runs potentially many searches on the haystack
2167/// and since each search has worst case `O(m * n)` time complexity, the
2168/// overall worst case time complexity for iteration is `O(m * n^2)`.
2169#[derive(Debug)]
2170pub struct Matches<'r, 'h> {
2171 haystack: &'h [u8],
2172 it: pikevm::FindMatches<'r, 'h>,
2173}
2174
2175impl<'r, 'h> Iterator for Matches<'r, 'h> {
2176 type Item = Match<'h>;
2177
2178 #[inline]
2179 fn next(&mut self) -> Option<Match<'h>> {
2180 self.it.next().map(|(s, e)| Match::new(self.haystack, s, e))
2181 }
2182
2183 #[inline]
2184 fn count(self) -> usize {
2185 self.it.count()
2186 }
2187}
2188
2189impl<'r, 'h> core::iter::FusedIterator for Matches<'r, 'h> {}
2190
2191/// An iterator over all non-overlapping capture matches in a haystack.
2192///
2193/// This iterator yields [`Captures`] values. The iterator stops when no more
2194/// matches can be found.
2195///
2196/// `'r` is the lifetime of the compiled regular expression and `'h` is the
2197/// lifetime of the matched string.
2198///
2199/// This iterator is created by [`Regex::captures_iter`].
2200///
2201/// # Time complexity
2202///
2203/// Note that since an iterator runs potentially many searches on the haystack
2204/// and since each search has worst case `O(m * n)` time complexity, the
2205/// overall worst case time complexity for iteration is `O(m * n^2)`.
2206#[derive(Debug)]
2207pub struct CaptureMatches<'r, 'h> {
2208 haystack: &'h [u8],
2209 re: &'r Regex,
2210 it: pikevm::CapturesMatches<'r, 'h>,
2211}
2212
2213impl<'r, 'h> Iterator for CaptureMatches<'r, 'h> {
2214 type Item = Captures<'h>;
2215
2216 #[inline]
2217 fn next(&mut self) -> Option<Captures<'h>> {
2218 self.it.next().map(|slots| Captures {
2219 haystack: self.haystack,
2220 slots: CaptureLocations(slots),
2221 pikevm: Arc::clone(&self.re.pikevm),
2222 })
2223 }
2224
2225 #[inline]
2226 fn count(self) -> usize {
2227 self.it.count()
2228 }
2229}
2230
2231impl<'r, 'h> core::iter::FusedIterator for CaptureMatches<'r, 'h> {}
2232
2233/// An iterator over all substrings delimited by a regex match.
2234///
2235/// `'r` is the lifetime of the compiled regular expression and `'h` is the
2236/// lifetime of the byte string being split.
2237///
2238/// This iterator is created by [`Regex::split`].
2239///
2240/// # Time complexity
2241///
2242/// Note that since an iterator runs potentially many searches on the haystack
2243/// and since each search has worst case `O(m * n)` time complexity, the
2244/// overall worst case time complexity for iteration is `O(m * n^2)`.
2245#[derive(Debug)]
2246pub struct Split<'r, 'h> {
2247 haystack: &'h [u8],
2248 finder: Matches<'r, 'h>,
2249 last: usize,
2250}
2251
2252impl<'r, 'h> Iterator for Split<'r, 'h> {
2253 type Item = &'h [u8];
2254
2255 #[inline]
2256 fn next(&mut self) -> Option<&'h [u8]> {
2257 match self.finder.next() {
2258 None => {
2259 let len = self.haystack.len();
2260 if self.last > len {
2261 None
2262 } else {
2263 let range = self.last..len;
2264 self.last = len + 1; // Next call will return None
2265 Some(&self.haystack[range])
2266 }
2267 }
2268 Some(m) => {
2269 let range = self.last..m.start();
2270 self.last = m.end();
2271 Some(&self.haystack[range])
2272 }
2273 }
2274 }
2275}
2276
2277impl<'r, 't> core::iter::FusedIterator for Split<'r, 't> {}
2278
2279/// An iterator over at most `N` substrings delimited by a regex match.
2280///
2281/// The last substring yielded by this iterator will be whatever remains after
2282/// `N-1` splits.
2283///
2284/// `'r` is the lifetime of the compiled regular expression and `'h` is the
2285/// lifetime of the byte string being split.
2286///
2287/// This iterator is created by [`Regex::splitn`].
2288///
2289/// # Time complexity
2290///
2291/// Note that since an iterator runs potentially many searches on the haystack
2292/// and since each search has worst case `O(m * n)` time complexity, the
2293/// overall worst case time complexity for iteration is `O(m * n^2)`.
2294///
2295/// Although note that the worst case time here has an upper bound given
2296/// by the `limit` parameter to [`Regex::splitn`].
2297#[derive(Debug)]
2298pub struct SplitN<'r, 'h> {
2299 splits: Split<'r, 'h>,
2300 limit: usize,
2301}
2302
2303impl<'r, 'h> Iterator for SplitN<'r, 'h> {
2304 type Item = &'h [u8];
2305
2306 #[inline]
2307 fn next(&mut self) -> Option<&'h [u8]> {
2308 if self.limit == 0 {
2309 return None;
2310 }
2311
2312 self.limit -= 1;
2313 if self.limit > 0 {
2314 return self.splits.next();
2315 }
2316
2317 let len = self.splits.haystack.len();
2318 if self.splits.last > len {
2319 // We've already returned all substrings.
2320 None
2321 } else {
2322 // self.n == 0, so future calls will return None immediately
2323 Some(&self.splits.haystack[self.splits.last..len])
2324 }
2325 }
2326
2327 #[inline]
2328 fn size_hint(&self) -> (usize, Option<usize>) {
2329 self.splits.size_hint()
2330 }
2331}
2332
2333impl<'r, 't> core::iter::FusedIterator for SplitN<'r, 't> {}
2334
2335/// An iterator over the names of all capture groups in a regex.
2336///
2337/// This iterator yields values of type `Option<&str>` in order of the opening
2338/// capture group parenthesis in the regex pattern. `None` is yielded for
2339/// groups with no name. The first element always corresponds to the implicit
2340/// and unnamed group for the overall match.
2341///
2342/// `'r` is the lifetime of the compiled regular expression.
2343///
2344/// This iterator is created by [`Regex::capture_names`].
2345#[derive(Clone, Debug)]
2346pub struct CaptureNames<'r>(nfa::CaptureNames<'r>);
2347
2348impl<'r> Iterator for CaptureNames<'r> {
2349 type Item = Option<&'r str>;
2350
2351 #[inline]
2352 fn next(&mut self) -> Option<Option<&'r str>> {
2353 self.0.next()
2354 }
2355
2356 #[inline]
2357 fn size_hint(&self) -> (usize, Option<usize>) {
2358 self.0.size_hint()
2359 }
2360
2361 #[inline]
2362 fn count(self) -> usize {
2363 self.0.count()
2364 }
2365}
2366
2367impl<'r> ExactSizeIterator for CaptureNames<'r> {}
2368
2369impl<'r> core::iter::FusedIterator for CaptureNames<'r> {}
2370
2371/// An iterator over all group matches in a [`Captures`] value.
2372///
2373/// This iterator yields values of type `Option<Match<'h>>`, where `'h` is the
2374/// lifetime of the haystack that the matches are for. The order of elements
2375/// yielded corresponds to the order of the opening parenthesis for the group
2376/// in the regex pattern. `None` is yielded for groups that did not participate
2377/// in the match.
2378///
2379/// The first element always corresponds to the implicit group for the overall
2380/// match. Since this iterator is created by a [`Captures`] value, and a
2381/// `Captures` value is only created when a match occurs, it follows that the
2382/// first element yielded by this iterator is guaranteed to be non-`None`.
2383///
2384/// The lifetime `'c` corresponds to the lifetime of the `Captures` value that
2385/// created this iterator, and the lifetime `'h` corresponds to the originally
2386/// matched haystack.
2387#[derive(Clone, Debug)]
2388pub struct SubCaptureMatches<'c, 'h> {
2389 caps: &'c Captures<'h>,
2390 it: core::iter::Enumerate<nfa::CaptureNames<'c>>,
2391}
2392
2393impl<'c, 'h> Iterator for SubCaptureMatches<'c, 'h> {
2394 type Item = Option<Match<'h>>;
2395
2396 #[inline]
2397 fn next(&mut self) -> Option<Option<Match<'h>>> {
2398 let (group_index, _) = self.it.next()?;
2399 Some(self.caps.get(group_index))
2400 }
2401
2402 #[inline]
2403 fn size_hint(&self) -> (usize, Option<usize>) {
2404 self.it.size_hint()
2405 }
2406
2407 #[inline]
2408 fn count(self) -> usize {
2409 self.it.count()
2410 }
2411}
2412
2413impl<'c, 'h> ExactSizeIterator for SubCaptureMatches<'c, 'h> {}
2414
2415impl<'c, 'h> core::iter::FusedIterator for SubCaptureMatches<'c, 'h> {}
2416
2417/// A trait for types that can be used to replace matches in a haystack.
2418///
2419/// In general, users of this crate shouldn't need to implement this trait,
2420/// since implementations are already provided for `&[u8]` along with other
2421/// variants of string types, as well as `FnMut(&Captures) -> Vec<u8>` (or any
2422/// `FnMut(&Captures) -> T` where `T: AsRef<[u8]>`). Those cover most use cases,
2423/// but callers can implement this trait directly if necessary.
2424///
2425/// # Example
2426///
2427/// This example shows a basic implementation of the `Replacer` trait. This
2428/// can be done much more simply using the replacement string interpolation
2429/// support (e.g., `$first $last`), but this approach avoids needing to parse
2430/// the replacement string at all.
2431///
2432/// ```
2433/// use regex_bites::bytes::{Captures, Regex, Replacer};
2434///
2435/// struct NameSwapper;
2436///
2437/// impl Replacer for NameSwapper {
2438/// fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2439/// dst.extend(&caps["first"]);
2440/// dst.extend(b" ");
2441/// dst.extend(&caps["last"]);
2442/// }
2443/// }
2444///
2445/// let re = Regex::new(r"(?<last>[^,\s]+),\s+(?<first>\S+)").unwrap();
2446/// let result = re.replace(b"Springsteen, Bruce", NameSwapper);
2447/// assert_eq!(result, &b"Bruce Springsteen"[..]);
2448/// ```
2449pub trait Replacer {
2450 /// Appends possibly empty data to `dst` to replace the current match.
2451 ///
2452 /// The current match is represented by `caps`, which is guaranteed to
2453 /// have a match at capture group `0`.
2454 ///
2455 /// For example, a no-op replacement would be `dst.extend(&caps[0])`.
2456 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>);
2457
2458 /// Return a fixed unchanging replacement string.
2459 ///
2460 /// When doing replacements, if access to [`Captures`] is not needed (e.g.,
2461 /// the replacement string does not need `$` expansion), then it can be
2462 /// beneficial to avoid finding sub-captures.
2463 ///
2464 /// In general, this is called once for every call to a replacement routine
2465 /// such as [`Regex::replace_all`].
2466 fn no_expansion<'r>(&'r mut self) -> Option<Cow<'r, [u8]>> {
2467 None
2468 }
2469
2470 /// Returns a type that implements `Replacer`, but that borrows and wraps
2471 /// this `Replacer`.
2472 ///
2473 /// This is useful when you want to take a generic `Replacer` (which might
2474 /// not be cloneable) and use it without consuming it, so it can be used
2475 /// more than once.
2476 ///
2477 /// # Example
2478 ///
2479 /// ```
2480 /// use regex_bites::bytes::{Regex, Replacer};
2481 ///
2482 /// fn replace_all_twice<R: Replacer>(
2483 /// re: Regex,
2484 /// src: &[u8],
2485 /// mut rep: R,
2486 /// ) -> Vec<u8> {
2487 /// let dst = re.replace_all(src, rep.by_ref());
2488 /// let dst = re.replace_all(&dst, rep.by_ref());
2489 /// dst.into_owned()
2490 /// }
2491 /// ```
2492 fn by_ref<'r>(&'r mut self) -> ReplacerRef<'r, Self> {
2493 ReplacerRef(self)
2494 }
2495}
2496
2497impl<'a> Replacer for &'a [u8] {
2498 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2499 caps.expand(*self, dst);
2500 }
2501
2502 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2503 no_expansion(self)
2504 }
2505}
2506
2507impl<'a> Replacer for &'a Vec<u8> {
2508 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2509 self.as_slice().replace_append(caps, dst)
2510 }
2511
2512 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2513 no_expansion(self)
2514 }
2515}
2516
2517impl Replacer for Vec<u8> {
2518 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2519 self.as_slice().replace_append(caps, dst)
2520 }
2521
2522 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2523 no_expansion(self)
2524 }
2525}
2526
2527impl<'a> Replacer for Cow<'a, [u8]> {
2528 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2529 self.as_ref().replace_append(caps, dst)
2530 }
2531
2532 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2533 no_expansion(self)
2534 }
2535}
2536
2537impl<'a> Replacer for &'a Cow<'a, [u8]> {
2538 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2539 self.as_ref().replace_append(caps, dst)
2540 }
2541
2542 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2543 no_expansion(self)
2544 }
2545}
2546
2547impl<F, T> Replacer for F
2548where
2549 F: FnMut(&Captures<'_>) -> T,
2550 T: AsRef<[u8]>,
2551{
2552 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2553 dst.extend((*self)(caps).as_ref());
2554 }
2555}
2556
2557/// A by-reference adaptor for a [`Replacer`].
2558///
2559/// This permits reusing the same `Replacer` value in multiple calls to a
2560/// replacement routine like [`Regex::replace_all`].
2561///
2562/// This type is created by [`Replacer::by_ref`].
2563#[derive(Debug)]
2564pub struct ReplacerRef<'a, R: ?Sized>(&'a mut R);
2565
2566impl<'a, R: Replacer + ?Sized + 'a> Replacer for ReplacerRef<'a, R> {
2567 fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut Vec<u8>) {
2568 self.0.replace_append(caps, dst)
2569 }
2570
2571 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2572 self.0.no_expansion()
2573 }
2574}
2575
2576/// A helper type for forcing literal string replacement.
2577///
2578/// It can be used with routines like [`Regex::replace`] and
2579/// [`Regex::replace_all`] to do a literal string replacement without expanding
2580/// `$name` to their corresponding capture groups. This can be both convenient
2581/// (to avoid escaping `$`, for example) and faster (since capture groups
2582/// don't need to be found).
2583///
2584/// `'s` is the lifetime of the literal string to use.
2585///
2586/// # Example
2587///
2588/// ```
2589/// use regex_bites::bytes::{NoExpand, Regex};
2590///
2591/// let re = Regex::new(r"(?<last>[^,\s]+),\s+(\S+)").unwrap();
2592/// let result = re.replace(b"Springsteen, Bruce", NoExpand(b"$2 $last"));
2593/// assert_eq!(result, &b"$2 $last"[..]);
2594/// ```
2595#[derive(Clone, Debug)]
2596pub struct NoExpand<'t>(pub &'t [u8]);
2597
2598impl<'t> Replacer for NoExpand<'t> {
2599 fn replace_append(&mut self, _: &Captures<'_>, dst: &mut Vec<u8>) {
2600 dst.extend(self.0);
2601 }
2602
2603 fn no_expansion(&mut self) -> Option<Cow<'_, [u8]>> {
2604 Some(Cow::Borrowed(self.0))
2605 }
2606}
2607
2608/// Quickly checks the given replacement string for whether interpolation
2609/// should be done on it. It returns `None` if a `$` was found anywhere in the
2610/// given string, which suggests interpolation needs to be done. But if there's
2611/// no `$` anywhere, then interpolation definitely does not need to be done. In
2612/// that case, the given string is returned as a borrowed `Cow`.
2613///
2614/// This is meant to be used to implement the `Replacer::no_expandsion` method
2615/// in its various trait impls.
2616fn no_expansion<T: AsRef<[u8]>>(t: &T) -> Option<Cow<'_, [u8]>> {
2617 let s = t.as_ref();
2618 match s.contains(&b'$') {
2619 true => None,
2620 false => Some(Cow::Borrowed(s)),
2621 }
2622}
2623
2624/// A configurable builder for a [`Regex`].
2625///
2626/// This builder can be used to programmatically set flags such as `i` (case
2627/// insensitive) and `x` (for verbose mode). This builder can also be used to
2628/// configure things like a size limit on the compiled regular expression.
2629#[derive(Debug)]
2630pub struct RegexBuilder {
2631 pattern: String,
2632 hir_config: hir::Config,
2633 nfa_config: nfa::Config,
2634}
2635
2636impl RegexBuilder {
2637 /// Create a new builder with a default configuration for the given
2638 /// pattern.
2639 ///
2640 /// If the pattern is invalid or exceeds the configured size limits, then
2641 /// an error will be returned when [`RegexBuilder::build`] is called.
2642 pub fn new(pattern: &str) -> RegexBuilder {
2643 RegexBuilder {
2644 pattern: pattern.to_string(),
2645 hir_config: hir::Config::default(),
2646 nfa_config: nfa::Config::default(),
2647 }
2648 }
2649
2650 /// Compiles the pattern given to `RegexBuilder::new` with the
2651 /// configuration set on this builder.
2652 ///
2653 /// If the pattern isn't a valid regex or if a configured size limit was
2654 /// exceeded, then an error is returned.
2655 pub fn build(&self) -> Result<Regex, Error> {
2656 let hir = Hir::parse(self.hir_config, &self.pattern)?;
2657 let nfa = NFA::new(self.nfa_config, self.pattern.clone(), &hir)?;
2658 let pikevm = Arc::new(PikeVM::new(nfa));
2659 let pool = {
2660 let pikevm = Arc::clone(&pikevm);
2661 let create = Box::new(move || Cache::new(&pikevm));
2662 CachePool::new(create)
2663 };
2664 Ok(Regex { pikevm, pool })
2665 }
2666
2667 /// This configures whether to enable ASCII case insensitive matching for
2668 /// the entire pattern.
2669 ///
2670 /// This setting can also be configured using the inline flag `i`
2671 /// in the pattern. For example, `(?i:foo)` matches `foo` case
2672 /// insensitively while `(?-i:foo)` matches `foo` case sensitively.
2673 ///
2674 /// The default for this is `false`.
2675 ///
2676 /// # Example
2677 ///
2678 /// ```
2679 /// use regex_bites::bytes::RegexBuilder;
2680 ///
2681 /// let re = RegexBuilder::new(r"foo(?-i:bar)quux")
2682 /// .case_insensitive(true)
2683 /// .build()
2684 /// .unwrap();
2685 /// assert!(re.is_match(b"FoObarQuUx"));
2686 /// // Even though case insensitive matching is enabled in the builder,
2687 /// // it can be locally disabled within the pattern. In this case,
2688 /// // `bar` is matched case sensitively.
2689 /// assert!(!re.is_match(b"fooBARquux"));
2690 /// ```
2691 pub fn case_insensitive(&mut self, yes: bool) -> &mut RegexBuilder {
2692 self.hir_config.flags.case_insensitive = yes;
2693 self
2694 }
2695
2696 /// This configures multi-line mode for the entire pattern.
2697 ///
2698 /// Enabling multi-line mode changes the behavior of the `^` and `$` anchor
2699 /// assertions. Instead of only matching at the beginning and end of a
2700 /// haystack, respectively, multi-line mode causes them to match at the
2701 /// beginning and end of a line *in addition* to the beginning and end of
2702 /// a haystack. More precisely, `^` will match at the position immediately
2703 /// following a `\n` and `$` will match at the position immediately
2704 /// preceding a `\n`.
2705 ///
2706 /// The behavior of this option is impacted by the [`RegexBuilder::crlf`]
2707 /// setting. Namely, CRLF mode changes the line terminator to be either
2708 /// `\r` or `\n`, but never at the position between a `\r` and `\`n.
2709 ///
2710 /// This setting can also be configured using the inline flag `m` in the
2711 /// pattern.
2712 ///
2713 /// The default for this is `false`.
2714 ///
2715 /// # Example
2716 ///
2717 /// ```
2718 /// use regex_bites::bytes::RegexBuilder;
2719 ///
2720 /// let re = RegexBuilder::new(r"^foo$")
2721 /// .multi_line(true)
2722 /// .build()
2723 /// .unwrap();
2724 /// assert_eq!(Some(1..4), re.find(b"\nfoo\n").map(|m| m.range()));
2725 /// ```
2726 pub fn multi_line(&mut self, yes: bool) -> &mut RegexBuilder {
2727 self.hir_config.flags.multi_line = yes;
2728 self
2729 }
2730
2731 /// This configures dot-matches-new-line mode for the entire pattern.
2732 ///
2733 /// Perhaps surprisingly, the default behavior for `.` is not to match
2734 /// any character, but rather, to match any character except for the line
2735 /// terminator (which is `\n` by default). When this mode is enabled, the
2736 /// behavior changes such that `.` truly matches any character.
2737 ///
2738 /// This setting can also be configured using the inline flag `s` in the
2739 /// pattern.
2740 ///
2741 /// The default for this is `false`.
2742 ///
2743 /// # Example
2744 ///
2745 /// ```
2746 /// use regex_bites::bytes::RegexBuilder;
2747 ///
2748 /// let re = RegexBuilder::new(r"foo.bar")
2749 /// .dot_matches_new_line(true)
2750 /// .build()
2751 /// .unwrap();
2752 /// let hay = b"foo\nbar";
2753 /// assert_eq!(Some(&b"foo\nbar"[..]), re.find(hay).map(|m| m.as_bytes()));
2754 /// ```
2755 pub fn dot_matches_new_line(&mut self, yes: bool) -> &mut RegexBuilder {
2756 self.hir_config.flags.dot_matches_new_line = yes;
2757 self
2758 }
2759
2760 /// This configures CRLF mode for the entire pattern.
2761 ///
2762 /// When CRLF mode is enabled, both `\r` ("carriage return" or CR for
2763 /// short) and `\n` ("line feed" or LF for short) are treated as line
2764 /// terminators. This results in the following:
2765 ///
2766 /// * Unless dot-matches-new-line mode is enabled, `.` will now match any
2767 /// character except for `\n` and `\r`.
2768 /// * When multi-line mode is enabled, `^` will match immediately
2769 /// following a `\n` or a `\r`. Similarly, `$` will match immediately
2770 /// preceding a `\n` or a `\r`. Neither `^` nor `$` will ever match between
2771 /// `\r` and `\n`.
2772 ///
2773 /// This setting can also be configured using the inline flag `R` in
2774 /// the pattern.
2775 ///
2776 /// The default for this is `false`.
2777 ///
2778 /// # Example
2779 ///
2780 /// ```
2781 /// use regex_bites::bytes::RegexBuilder;
2782 ///
2783 /// let re = RegexBuilder::new(r"^foo$")
2784 /// .multi_line(true)
2785 /// .crlf(true)
2786 /// .build()
2787 /// .unwrap();
2788 /// let hay = b"\r\nfoo\r\n";
2789 /// // If CRLF mode weren't enabled here, then '$' wouldn't match
2790 /// // immediately after 'foo', and thus no match would be found.
2791 /// assert_eq!(Some(&b"foo"[..]), re.find(hay).map(|m| m.as_bytes()));
2792 /// ```
2793 ///
2794 /// This example demonstrates that `^` will never match at a position
2795 /// between `\r` and `\n`. (`$` will similarly not match between a `\r`
2796 /// and a `\n`.)
2797 ///
2798 /// ```
2799 /// use regex_bites::bytes::RegexBuilder;
2800 ///
2801 /// let re = RegexBuilder::new(r"^")
2802 /// .multi_line(true)
2803 /// .crlf(true)
2804 /// .build()
2805 /// .unwrap();
2806 /// let hay = b"\r\n\r\n";
2807 /// let ranges: Vec<_> = re.find_iter(hay).map(|m| m.range()).collect();
2808 /// assert_eq!(ranges, vec![0..0, 2..2, 4..4]);
2809 /// ```
2810 pub fn crlf(&mut self, yes: bool) -> &mut RegexBuilder {
2811 self.hir_config.flags.crlf = yes;
2812 self
2813 }
2814
2815 /// This configures swap-greed mode for the entire pattern.
2816 ///
2817 /// When swap-greed mode is enabled, patterns like `a+` will become
2818 /// non-greedy and patterns like `a+?` will become greedy. In other words,
2819 /// the meanings of `a+` and `a+?` are switched.
2820 ///
2821 /// This setting can also be configured using the inline flag `U` in the
2822 /// pattern.
2823 ///
2824 /// The default for this is `false`.
2825 ///
2826 /// # Example
2827 ///
2828 /// ```
2829 /// use regex_bites::bytes::RegexBuilder;
2830 ///
2831 /// let re = RegexBuilder::new(r"a+")
2832 /// .swap_greed(true)
2833 /// .build()
2834 /// .unwrap();
2835 /// assert_eq!(Some(&b"a"[..]), re.find(b"aaa").map(|m| m.as_bytes()));
2836 /// ```
2837 pub fn swap_greed(&mut self, yes: bool) -> &mut RegexBuilder {
2838 self.hir_config.flags.swap_greed = yes;
2839 self
2840 }
2841
2842 /// This configures verbose mode for the entire pattern.
2843 ///
2844 /// When enabled, whitespace will treated as insignifcant in the pattern
2845 /// and `#` can be used to start a comment until the next new line.
2846 ///
2847 /// Normally, in most places in a pattern, whitespace is treated literally.
2848 /// For example ` +` will match one or more ASCII whitespace characters.
2849 ///
2850 /// When verbose mode is enabled, `\#` can be used to match a literal `#`
2851 /// and `\ ` can be used to match a literal ASCII whitespace character.
2852 ///
2853 /// Verbose mode is useful for permitting regexes to be formatted and
2854 /// broken up more nicely. This may make them more easily readable.
2855 ///
2856 /// This setting can also be configured using the inline flag `x` in the
2857 /// pattern.
2858 ///
2859 /// The default for this is `false`.
2860 ///
2861 /// # Example
2862 ///
2863 /// ```
2864 /// use regex_bites::bytes::RegexBuilder;
2865 ///
2866 /// let pat = r"
2867 /// \b
2868 /// (?<first>[A-Z]\w*) # always start with uppercase letter
2869 /// \s+ # whitespace should separate names
2870 /// (?: # middle name can be an initial!
2871 /// (?:(?<initial>[A-Z])\.|(?<middle>[A-Z]\w*))
2872 /// \s+
2873 /// )?
2874 /// (?<last>[A-Z]\w*)
2875 /// \b
2876 /// ";
2877 /// let re = RegexBuilder::new(pat)
2878 /// .ignore_whitespace(true)
2879 /// .build()
2880 /// .unwrap();
2881 ///
2882 /// let caps = re.captures(b"Harry Potter").unwrap();
2883 /// assert_eq!(b"Harry", &caps["first"]);
2884 /// assert_eq!(b"Potter", &caps["last"]);
2885 ///
2886 /// let caps = re.captures(b"Harry J. Potter").unwrap();
2887 /// assert_eq!(b"Harry", &caps["first"]);
2888 /// // Since a middle name/initial isn't required for an overall match,
2889 /// // we can't assume that 'initial' or 'middle' will be populated!
2890 /// assert_eq!(Some(&b"J"[..]), caps.name("initial").map(|m| m.as_bytes()));
2891 /// assert_eq!(None, caps.name("middle").map(|m| m.as_bytes()));
2892 /// assert_eq!(b"Potter", &caps["last"]);
2893 ///
2894 /// let caps = re.captures(b"Harry James Potter").unwrap();
2895 /// assert_eq!(b"Harry", &caps["first"]);
2896 /// // Since a middle name/initial isn't required for an overall match,
2897 /// // we can't assume that 'initial' or 'middle' will be populated!
2898 /// assert_eq!(None, caps.name("initial").map(|m| m.as_bytes()));
2899 /// assert_eq!(Some(&b"James"[..]), caps.name("middle").map(|m| m.as_bytes()));
2900 /// assert_eq!(b"Potter", &caps["last"]);
2901 /// ```
2902 pub fn ignore_whitespace(&mut self, yes: bool) -> &mut RegexBuilder {
2903 self.hir_config.flags.ignore_whitespace = yes;
2904 self
2905 }
2906
2907 /// Sets the approximate size limit, in bytes, of the compiled regex.
2908 ///
2909 /// This roughly corresponds to the number of heap memory, in bytes,
2910 /// occupied by a single regex. If the regex would otherwise approximately
2911 /// exceed this limit, then compiling that regex will fail.
2912 ///
2913 /// The main utility of a method like this is to avoid compiling regexes
2914 /// that use an unexpected amount of resources, such as time and memory.
2915 /// Even if the memory usage of a large regex is acceptable, its search
2916 /// time may not be. Namely, worst case time complexity for search is `O(m
2917 /// * n)`, where `m ~ len(pattern)` and `n ~ len(haystack)`. That is,
2918 /// search time depends, in part, on the size of the compiled regex. This
2919 /// means that putting a limit on the size of the regex limits how much a
2920 /// regex can impact search time.
2921 ///
2922 /// The default for this is some reasonable number that permits most
2923 /// patterns to compile successfully.
2924 ///
2925 /// # Example
2926 ///
2927 /// ```
2928 /// use regex_bites::bytes::RegexBuilder;
2929 ///
2930 /// assert!(RegexBuilder::new(r"\w").size_limit(100).build().is_err());
2931 /// ```
2932 pub fn size_limit(&mut self, limit: usize) -> &mut RegexBuilder {
2933 self.nfa_config.size_limit = Some(limit);
2934 self
2935 }
2936
2937 /// Set the nesting limit for this parser.
2938 ///
2939 /// The nesting limit controls how deep the abstract syntax tree is allowed
2940 /// to be. If the AST exceeds the given limit (e.g., with too many nested
2941 /// groups), then an error is returned by the parser.
2942 ///
2943 /// The purpose of this limit is to act as a heuristic to prevent stack
2944 /// overflow for consumers that do structural induction on an AST using
2945 /// explicit recursion. While this crate never does this (instead using
2946 /// constant stack space and moving the call stack to the heap), other
2947 /// crates may.
2948 ///
2949 /// This limit is not checked until the entire AST is parsed. Therefore, if
2950 /// callers want to put a limit on the amount of heap space used, then they
2951 /// should impose a limit on the length, in bytes, of the concrete pattern
2952 /// string. In particular, this is viable since this parser implementation
2953 /// will limit itself to heap space proportional to the length of the
2954 /// pattern string. See also the [untrusted inputs](crate#untrusted-input)
2955 /// section in the top-level crate documentation for more information about
2956 /// this.
2957 ///
2958 /// Note that a nest limit of `0` will return a nest limit error for most
2959 /// patterns but not all. For example, a nest limit of `0` permits `a` but
2960 /// not `ab`, since `ab` requires an explicit concatenation, which results
2961 /// in a nest depth of `1`. In general, a nest limit is not something that
2962 /// manifests in an obvious way in the concrete syntax, therefore, it
2963 /// should not be used in a granular way.
2964 ///
2965 /// # Example
2966 ///
2967 /// ```
2968 /// use regex_bites::bytes::RegexBuilder;
2969 ///
2970 /// assert!(RegexBuilder::new(r"").nest_limit(0).build().is_ok());
2971 /// assert!(RegexBuilder::new(r"a").nest_limit(0).build().is_ok());
2972 /// assert!(RegexBuilder::new(r"(a)").nest_limit(0).build().is_err());
2973 /// ```
2974 pub fn nest_limit(&mut self, limit: u32) -> &mut RegexBuilder {
2975 self.hir_config.nest_limit = limit;
2976 self
2977 }
2978}