1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//! Parse encoding parameter to `Regexp#initialize` and `Regexp::compile`.

use core::convert::TryFrom;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::mem;
use std::error;

use bstr::ByteSlice;

use crate::Flags;

#[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct InvalidEncodingError {
    _private: (),
}

impl InvalidEncodingError {
    /// Constructs a new, default `InvalidEncodingError`.
    #[must_use]
    pub const fn new() -> Self {
        Self { _private: () }
    }
}

impl fmt::Display for InvalidEncodingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Invalid Regexp encoding")
    }
}

impl error::Error for InvalidEncodingError {}

/// The encoding of a Regexp literal.
///
/// Regexps are assumed to use the source encoding but literals may override
/// the encoding with a Regexp modifier.
///
/// See [`Regexp` encoding][regexp-encoding].
///
/// [regexp-encoding]: https://ruby-doc.org/core-2.6.3/Regexp.html#class-Regexp-label-Encoding
#[derive(Debug, Clone, Copy, PartialOrd, Ord)]
pub enum Encoding {
    Fixed,
    No,
    None,
}

impl Default for Encoding {
    fn default() -> Self {
        Self::None
    }
}

impl Hash for Encoding {
    fn hash<H: Hasher>(&self, state: &mut H) {
        let discriminant = mem::discriminant(self);
        discriminant.hash(state);
    }
}

impl PartialEq for Encoding {
    fn eq(&self, other: &Self) -> bool {
        use Encoding::{Fixed, No, None};

        matches!((self, other), (No | None, No | None) | (Fixed, Fixed))
    }
}

impl Eq for Encoding {}

impl TryFrom<Flags> for Encoding {
    type Error = InvalidEncodingError;

    fn try_from(mut flags: Flags) -> Result<Self, Self::Error> {
        flags.set(Flags::ALL_REGEXP_OPTS, false);
        if flags.intersects(Flags::FIXEDENCODING) {
            Ok(Self::Fixed)
        } else if flags.intersects(Flags::NOENCODING) {
            Ok(Encoding::No)
        } else if flags.is_empty() {
            Ok(Encoding::new())
        } else {
            Err(InvalidEncodingError::new())
        }
    }
}

impl TryFrom<u8> for Encoding {
    type Error = InvalidEncodingError;

    fn try_from(flags: u8) -> Result<Self, Self::Error> {
        let flags = Flags::from_bits(flags).ok_or_else(InvalidEncodingError::new)?;
        Self::try_from(flags)
    }
}

impl TryFrom<i64> for Encoding {
    type Error = InvalidEncodingError;

    fn try_from(flags: i64) -> Result<Self, Self::Error> {
        let [byte, _, _, _, _, _, _, _] = flags.to_le_bytes();
        Self::try_from(byte)
    }
}

impl TryFrom<&str> for Encoding {
    type Error = InvalidEncodingError;

    fn try_from(encoding: &str) -> Result<Self, Self::Error> {
        if encoding.contains('u') && encoding.contains('n') {
            return Err(InvalidEncodingError::new());
        }
        let mut enc = None;
        for flag in encoding.bytes() {
            match flag {
                b'u' | b's' | b'e' if enc.is_none() => enc = Some(Encoding::Fixed),
                b'n' if enc.is_none() => enc = Some(Encoding::No),
                b'i' | b'm' | b'x' | b'o' => continue,
                _ => return Err(InvalidEncodingError::new()),
            }
        }
        Ok(enc.unwrap_or_default())
    }
}

impl TryFrom<&[u8]> for Encoding {
    type Error = InvalidEncodingError;

    fn try_from(encoding: &[u8]) -> Result<Self, Self::Error> {
        if encoding.find_byte(b'u').is_some() && encoding.find_byte(b'n').is_some() {
            return Err(InvalidEncodingError::new());
        }
        let mut enc = None;
        for &flag in encoding {
            match flag {
                b'u' | b's' | b'e' if enc.is_none() => enc = Some(Encoding::Fixed),
                b'n' if enc.is_none() => enc = Some(Encoding::No),
                b'i' | b'm' | b'x' | b'o' => continue,
                _ => return Err(InvalidEncodingError::new()),
            }
        }
        Ok(enc.unwrap_or_default())
    }
}

impl TryFrom<String> for Encoding {
    type Error = InvalidEncodingError;

    fn try_from(encoding: String) -> Result<Self, Self::Error> {
        Self::try_from(encoding.as_str())
    }
}

impl TryFrom<Vec<u8>> for Encoding {
    type Error = InvalidEncodingError;

    fn try_from(encoding: Vec<u8>) -> Result<Self, Self::Error> {
        Self::try_from(encoding.as_slice())
    }
}

impl From<Encoding> for Flags {
    /// Convert an `Encoding` to its bit flag representation.
    fn from(encoding: Encoding) -> Self {
        encoding.flags()
    }
}

impl From<&Encoding> for Flags {
    /// Convert an `Encoding` to its bit flag representation.
    fn from(encoding: &Encoding) -> Self {
        encoding.flags()
    }
}

impl From<Encoding> for u8 {
    /// Convert an `Encoding` to its bit representation.
    fn from(encoding: Encoding) -> Self {
        encoding.into_bits()
    }
}

impl From<&Encoding> for u8 {
    /// Convert an `Encoding` to its bit representation.
    fn from(encoding: &Encoding) -> Self {
        encoding.into_bits()
    }
}

impl From<Encoding> for i64 {
    /// Convert an `Encoding` to its widened bit representation.
    fn from(encoding: Encoding) -> Self {
        encoding.into_bits().into()
    }
}

impl From<&Encoding> for i64 {
    /// Convert an `Encoding` to its widened bit representation.
    fn from(encoding: &Encoding) -> Self {
        encoding.into_bits().into()
    }
}

impl fmt::Display for Encoding {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_modifier_str())
    }
}

impl Encoding {
    /// Construct a new [`None`] encoding.
    ///
    /// [`None`]: Self::None
    #[must_use]
    pub const fn new() -> Self {
        Self::None
    }

    /// Convert an `Encoding` to its bit flag representation.
    ///
    /// Alias for the corresponding `Into<Flags>` implementation.
    #[must_use]
    pub const fn flags(self) -> Flags {
        match self {
            Self::Fixed => Flags::FIXEDENCODING,
            Self::No => Flags::NOENCODING,
            Self::None => Flags::empty(),
        }
    }

    /// Convert an `Encoding` to its bit representation.
    ///
    /// Alias for the corresponding `Into<u8>` implementation.
    #[must_use]
    pub const fn into_bits(self) -> u8 {
        self.flags().bits()
    }

    /// Serialize the encoding flags to a string suitable for a `Regexp` display
    /// or debug implementation.
    ///
    /// See also [`Regexp#inspect`][regexp-inspect].
    ///
    /// [regexp-inspect]: https://ruby-doc.org/core-2.6.3/Regexp.html#method-i-inspect
    #[must_use]
    pub const fn as_modifier_str(self) -> &'static str {
        match self {
            Self::Fixed | Self::None => "",
            Self::No => "n",
        }
    }
}