artichoke_backend/extn/core/regexp/backend/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::collections::HashMap;
use std::fmt;
use std::hash::{Hash, Hasher};

use crate::extn::core::regexp::{Config, Encoding, Source};
use crate::extn::prelude::*;

#[cfg(feature = "core-regexp-oniguruma")]
pub mod onig;
pub mod regex;

pub type NilableString = Option<Vec<u8>>;
pub type NameToCaptureLocations = Vec<(Vec<u8>, Vec<usize>)>;

#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Scan {
    Collected(Vec<Vec<Option<Vec<u8>>>>),
    Patterns(Vec<Vec<u8>>),
    Haystack,
}

impl TryConvertMut<Scan, Option<Value>> for Artichoke {
    type Error = Error;

    fn try_convert_mut(&mut self, from: Scan) -> Result<Option<Value>, Self::Error> {
        match from {
            Scan::Collected(collected) => {
                let ary = self.try_convert_mut(collected)?;
                Ok(Some(ary))
            }
            Scan::Patterns(patterns) => {
                let ary = self.try_convert_mut(patterns)?;
                Ok(Some(ary))
            }
            Scan::Haystack => Ok(None),
        }
    }
}

pub trait RegexpType {
    fn box_clone(&self) -> Box<dyn RegexpType>;

    fn debug(&self) -> String;

    fn source(&self) -> &Source;

    fn config(&self) -> &Config;

    fn encoding(&self) -> &Encoding;

    fn inspect(&self) -> Vec<u8>;

    fn string(&self) -> &[u8];

    fn captures(&self, haystack: &[u8]) -> Result<Option<Vec<NilableString>>, Error>;

    fn capture_indexes_for_name(&self, name: &[u8]) -> Result<Option<Vec<usize>>, Error>;

    fn captures_len(&self, haystack: Option<&[u8]>) -> Result<usize, Error>;

    fn capture0<'a>(&self, haystack: &'a [u8]) -> Result<Option<&'a [u8]>, Error>;

    fn case_match(&self, interp: &mut Artichoke, haystack: &[u8]) -> Result<bool, Error>;

    fn is_match(&self, haystack: &[u8], pos: Option<i64>) -> Result<bool, Error>;

    fn match_(
        &self,
        interp: &mut Artichoke,
        haystack: &[u8],
        pos: Option<i64>,
        block: Option<Block>,
    ) -> Result<Value, Error>;

    fn match_operator(&self, interp: &mut Artichoke, haystack: &[u8]) -> Result<Option<usize>, Error>;

    fn named_captures(&self) -> Result<NameToCaptureLocations, Error>;

    fn named_captures_for_haystack(&self, haystack: &[u8]) -> Result<Option<HashMap<Vec<u8>, NilableString>>, Error>;

    fn names(&self) -> Vec<Vec<u8>>;

    fn pos(&self, haystack: &[u8], at: usize) -> Result<Option<(usize, usize)>, Error>;

    fn scan(&self, interp: &mut Artichoke, haystack: &[u8], block: Option<Block>) -> Result<Scan, Error>;
}

impl Clone for Box<dyn RegexpType> {
    #[inline]
    fn clone(&self) -> Self {
        self.box_clone()
    }
}

impl fmt::Debug for Box<dyn RegexpType> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.as_ref(), f)
    }
}

impl Hash for Box<dyn RegexpType> {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        Hash::hash(&self.as_ref(), state);
    }
}

impl PartialEq for Box<dyn RegexpType> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        PartialEq::eq(&self.as_ref(), &other.as_ref())
    }
}

impl Eq for Box<dyn RegexpType> {}

impl fmt::Debug for &dyn RegexpType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.debug())
    }
}

impl Hash for &dyn RegexpType {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.source().hash(state);
    }
}

impl PartialEq for &dyn RegexpType {
    fn eq(&self, other: &Self) -> bool {
        self.config().pattern() == other.config().pattern() && self.encoding() == other.encoding()
    }
}

impl Eq for &dyn RegexpType {}