ct_regex_internal/expr/
captures.rs

1use std::{fmt::Debug, ops::Range};
2
3use standard_lib::collections::cons::ConsBranch;
4
5use crate::haystack::Haystack;
6
7#[derive(#[automatically_derived]
impl ::core::fmt::Debug for IndexedCaptures {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f,
            "IndexedCaptures", &&self.0)
    }
}Debug, #[automatically_derived]
impl ::core::default::Default for IndexedCaptures {
    #[inline]
    fn default() -> IndexedCaptures {
        IndexedCaptures(::core::default::Default::default())
    }
}Default, #[automatically_derived]
impl ::core::clone::Clone for IndexedCaptures {
    #[inline]
    fn clone(&self) -> IndexedCaptures {
        IndexedCaptures(::core::clone::Clone::clone(&self.0))
    }
}Clone)]
8pub struct IndexedCaptures(pub ConsBranch<IndexedCapture>);
9
10#[derive(#[automatically_derived]
impl ::core::fmt::Debug for IndexedCapture {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "IndexedCapture", "index", &self.index, "cap", &&self.cap)
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for IndexedCapture {
    #[inline]
    fn clone(&self) -> IndexedCapture {
        IndexedCapture {
            index: ::core::clone::Clone::clone(&self.index),
            cap: ::core::clone::Clone::clone(&self.cap),
        }
    }
}Clone)]
11pub struct IndexedCapture {
12    pub index: usize,
13    pub cap: Range<usize>,
14}
15
16impl IndexedCaptures {
17    pub fn push(&mut self, index: usize, cap: Range<usize>) {
18        self.0.push(IndexedCapture {
19            index,
20            cap
21        });
22    }
23
24    // May contain duplicates for a certain index. To avoid backtracking and overriding, we deal
25    // with this here.
26    pub fn into_array<const N: usize>(self) -> [Option<Range<usize>>; N] {
27        let mut res = [const { None }; N];
28
29        for item in self.0.into_iter_owned() {
30            match res.get(item.index) {
31                None => {
    ::core::panicking::panic_fmt(format_args!("capture index exceeds maximum group number"));
}panic!("capture index exceeds maximum group number"),
32                Some(None) => res[item.index] = Some(item.cap.clone()),
33                // We're traversing captures backwards, so we need to keep the value written into
34                // the array first.
35                Some(_) => (),
36            }
37        }
38
39        res
40    }
41}
42
43pub trait CaptureFromRanges<'a, H: Haystack<'a>, const N: usize>: Sized + Debug {
44    fn from_ranges(ranges: [Option<Range<usize>>; N], hay: H) -> Option<Self>;
45}