1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! Parse options parameter to `Regexp#initialize` and `Regexp::compile`.

use bstr::ByteSlice;
use core::convert::TryFrom;
use core::fmt;

use crate::Flags;

/// The state of a Regexp engine flag in [`Options`].
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum RegexpOption {
    /// Engine feature is disabled.
    ///
    /// Features are disabled by default.
    Disabled,
    /// Engine feature is disabled.
    Enabled,
}

impl RegexpOption {
    /// Construct a new, disabled `RegexpOption`.
    #[must_use]
    pub const fn new() -> Self {
        Self::Disabled
    }

    /// Return whether this option is enabled.
    ///
    /// An option is enabled if it is equal to [`RegexpOption::Enabled`].
    #[must_use]
    pub const fn is_enabled(self) -> bool {
        matches!(self, Self::Enabled)
    }
}

impl Default for RegexpOption {
    /// Create a disabled `RegexpOption`.
    fn default() -> Self {
        Self::Disabled
    }
}

impl From<bool> for RegexpOption {
    /// Convert from `bool` to its `RegexpOption` representation.
    ///
    /// `true` creates a [RegexpOption::Enabled`]. `false` creates a
    /// [`RegexpOption::Disabled`].
    fn from(value: bool) -> Self {
        if value {
            Self::Enabled
        } else {
            Self::Disabled
        }
    }
}

impl From<RegexpOption> for bool {
    /// Convert from `RegexpOption` to its boolean representation.
    ///
    /// See also [`is_enabled`].
    ///
    /// [`is_enabled`]: RegexpOption::is_enabled
    fn from(value: RegexpOption) -> Self {
        matches!(value, RegexpOption::Enabled)
    }
}

/// Configuration options for Ruby Regexps.
///
/// Options can be supplied either as an `Integer` object to `Regexp::new` or
/// inline in Regexp literals like `/artichoke/i`.
#[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Options {
    flags: Flags,
}

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

impl From<Options> for u8 {
    /// Convert an `Options` to its bit representation.
    fn from(opts: Options) -> Self {
        opts.flags.bits()
    }
}

impl From<Options> for i64 {
    /// Convert an `Options` to its widened bit representation.
    fn from(opts: Options) -> Self {
        opts.flags.bits().into()
    }
}

impl From<Flags> for Options {
    fn from(mut flags: Flags) -> Self {
        flags.remove(Flags::FIXEDENCODING | Flags::NOENCODING | Flags::LITERAL);
        Self { flags }
    }
}

impl From<u8> for Options {
    fn from(flags: u8) -> Self {
        let flags = Flags::from_bits_truncate(flags);
        Self::from(flags)
    }
}

impl From<i64> for Options {
    /// Truncate the given `i64` to one byte and generate flags.
    ///
    /// See `From<u8>`. For a conversion that fails if the given `i64` is
    /// larger than [`u8::MAX`], see [`try_from_int`].
    ///
    /// [`try_from_int`]: Self::try_from_int
    fn from(flags: i64) -> Self {
        let [byte, _, _, _, _, _, _, _] = flags.to_le_bytes();
        Self::from(byte)
    }
}

impl From<Option<bool>> for Options {
    fn from(options: Option<bool>) -> Self {
        match options {
            Some(false) | None => Self::new(),
            Some(true) => Self::with_ignore_case(),
        }
    }
}

impl From<&str> for Options {
    fn from(options: &str) -> Self {
        let mut flags = Flags::empty();
        flags.set(Flags::MULTILINE, options.contains('m'));
        flags.set(Flags::IGNORECASE, options.contains('i'));
        flags.set(Flags::EXTENDED, options.contains('x'));
        Self { flags }
    }
}

impl From<&[u8]> for Options {
    fn from(options: &[u8]) -> Self {
        let mut flags = Flags::empty();
        flags.set(Flags::MULTILINE, options.find_byte(b'm').is_some());
        flags.set(Flags::IGNORECASE, options.find_byte(b'i').is_some());
        flags.set(Flags::EXTENDED, options.find_byte(b'x').is_some());
        Self { flags }
    }
}

impl From<String> for Options {
    fn from(options: String) -> Self {
        Self::from(options.as_str())
    }
}

impl From<Vec<u8>> for Options {
    fn from(options: Vec<u8>) -> Self {
        Self::from(options.as_slice())
    }
}

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

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

    /// An options instance that has only case insensitive mode enabled.
    #[must_use]
    pub const fn with_ignore_case() -> Self {
        Self {
            flags: Flags::IGNORECASE,
        }
    }

    /// Try to parse an `Options` from a full-width `i64`.
    ///
    /// If `options` cannot be converted losslessly to a `u8`, this function
    /// returns [`None`]. See `From<u8>`.
    ///
    /// For a conversion from `i64` that truncates the given `options` to `u8`,
    /// see `From<i64>`.
    #[must_use]
    pub fn try_from_int(options: i64) -> Option<Self> {
        let options = u8::try_from(options).ok()?;
        Some(Self::from(options))
    }

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

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

    /// Whether these `Options` are configured for multiline mode.
    pub const fn multiline(self) -> RegexpOption {
        if self.flags.intersects(Flags::MULTILINE) {
            RegexpOption::Enabled
        } else {
            RegexpOption::Disabled
        }
    }

    /// Whether these `Options` are configured for case-insensitive mode.
    pub const fn ignore_case(self) -> RegexpOption {
        if self.flags.intersects(Flags::IGNORECASE) {
            RegexpOption::Enabled
        } else {
            RegexpOption::Disabled
        }
    }

    /// Whether these `Options` are configured for extended mode with
    /// insignificant whitespace.
    pub const fn extended(self) -> RegexpOption {
        if self.flags.intersects(Flags::EXTENDED) {
            RegexpOption::Enabled
        } else {
            RegexpOption::Disabled
        }
    }

    /// Whether the Regexp was parsed as a literal, e.g. `'/artichoke/i`.
    ///
    /// This enables Ruby parsers to inject whether a Regexp is a literal to the
    /// core library. Literal Regexps have some special behavior regrding
    /// capturing groups and report parse failures differently.
    pub const fn is_literal(self) -> bool {
        self.flags.intersects(Flags::LITERAL)
    }

    /// Serialize the option 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_display_modifier(self) -> &'static str {
        use RegexpOption::{Disabled, Enabled};

        match (self.multiline(), self.ignore_case(), self.extended()) {
            (Enabled, Enabled, Enabled) => "mix",
            (Enabled, Enabled, Disabled) => "mi",
            (Enabled, Disabled, Enabled) => "mx",
            (Enabled, Disabled, Disabled) => "m",
            (Disabled, Enabled, Enabled) => "ix",
            (Disabled, Enabled, Disabled) => "i",
            (Disabled, Disabled, Enabled) => "x",
            (Disabled, Disabled, Disabled) => "",
        }
    }

    /// Serialize the option flags to a string suitable for including in a raw
    /// pattern for configuring an underlying `Regexp`.
    #[must_use]
    pub const fn as_inline_modifier(self) -> &'static str {
        use RegexpOption::{Disabled, Enabled};

        match (self.multiline(), self.ignore_case(), self.extended()) {
            (Enabled, Enabled, Enabled) => "mix",
            (Enabled, Enabled, Disabled) => "mi-x",
            (Enabled, Disabled, Enabled) => "mx-i",
            (Enabled, Disabled, Disabled) => "m-ix",
            (Disabled, Enabled, Enabled) => "ix-m",
            (Disabled, Enabled, Disabled) => "i-mx",
            (Disabled, Disabled, Enabled) => "x-mi",
            (Disabled, Disabled, Disabled) => "-mix",
        }
    }

    /// Inserts or removes the specified flags depending on the passed value.
    pub fn set(&mut self, other: Flags, value: bool) {
        self.flags.set(other, value)
    }
}

#[cfg(test)]
mod tests {
    use super::{Options, RegexpOption};
    use crate::Flags;

    #[test]
    fn new_is_empty_flags() {
        assert_eq!(Options::new(), Options::from(Flags::empty()));
    }

    #[test]
    fn from_all_flags_ignores_encoding_and_literal() {
        assert_eq!(Options::from(Flags::all()), Options::from(Flags::ALL_REGEXP_OPTS));
    }

    // If options is an `Integer`, it should be one or more of the constants
    // `Regexp::EXTENDED`, `Regexp::IGNORECASE`, and `Regexp::MULTILINE`, or-ed
    // together. Otherwise, if options is not `nil` or `false`, the regexp will
    // be case insensitive.
    #[test]
    fn parse_options() {
        assert_eq!(Options::from(None), Options::new());
        assert_eq!(Options::from(Some(false)), Options::new());
        assert_eq!(Options::from(Some(true)), Options::with_ignore_case());

        let mut opts = Options::new();
        opts.flags |= Flags::IGNORECASE;
        assert_eq!(Options::with_ignore_case(), opts);

        let mut opts = Options::new();
        opts.flags |= Flags::EXTENDED;
        assert_eq!(Options::from(Flags::EXTENDED), opts);
        assert_eq!(
            Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | i64::MAX),
            None
        );
        assert_eq!(Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | 4096), None);
        assert_eq!(Options::from(Flags::EXTENDED.bits() | 64), opts);
        assert_ne!(Options::from(Flags::EXTENDED | Flags::IGNORECASE), opts);
        assert_ne!(Options::from(Flags::EXTENDED | Flags::MULTILINE), opts);
        assert_ne!(
            Options::from(Flags::EXTENDED | Flags::IGNORECASE | Flags::MULTILINE),
            opts
        );
        assert_eq!(opts.ignore_case(), RegexpOption::Disabled);
        assert_eq!(opts.extended(), RegexpOption::Enabled);
        assert_eq!(opts.multiline(), RegexpOption::Disabled);

        let mut opts = Options::new();
        opts.flags |= Flags::IGNORECASE;
        assert_eq!(Options::from(Flags::IGNORECASE), opts);
        assert_eq!(
            Options::try_from_int(i64::from(Flags::IGNORECASE.bits()) | i64::MAX),
            None
        );
        assert_eq!(Options::try_from_int(i64::from(Flags::IGNORECASE.bits()) | 4096), None);
        assert_eq!(Options::from(Flags::IGNORECASE.bits() | 64), opts);
        assert_ne!(Options::from(Flags::IGNORECASE | Flags::EXTENDED), opts);
        assert_ne!(Options::from(Flags::IGNORECASE | Flags::MULTILINE), opts);
        assert_ne!(
            Options::from(Flags::EXTENDED | Flags::IGNORECASE | Flags::MULTILINE),
            opts
        );
        assert_eq!(opts.ignore_case(), RegexpOption::Enabled);
        assert_eq!(opts.extended(), RegexpOption::Disabled);
        assert_eq!(opts.multiline(), RegexpOption::Disabled);

        let mut opts = Options::new();
        opts.flags |= Flags::MULTILINE;
        assert_eq!(Options::from(Flags::MULTILINE), opts);
        assert_eq!(
            Options::try_from_int(i64::from(Flags::MULTILINE.bits()) | i64::MAX),
            None
        );
        assert_eq!(Options::try_from_int(i64::from(Flags::MULTILINE.bits()) | 4096), None);
        assert_eq!(Options::from(Flags::MULTILINE.bits() | 64), opts);
        assert_ne!(Options::from(Flags::MULTILINE | Flags::IGNORECASE), opts);
        assert_ne!(Options::from(Flags::MULTILINE | Flags::EXTENDED), opts);
        assert_ne!(
            Options::from(Flags::EXTENDED | Flags::IGNORECASE | Flags::MULTILINE),
            opts
        );
        assert_eq!(opts.ignore_case(), RegexpOption::Disabled);
        assert_eq!(opts.extended(), RegexpOption::Disabled);
        assert_eq!(opts.multiline(), RegexpOption::Enabled);

        let mut opts = Options::new();
        opts.flags |= Flags::EXTENDED | Flags::IGNORECASE;
        assert_ne!(Options::from(Flags::EXTENDED), opts);
        assert_ne!(Options::from(Flags::IGNORECASE), opts);
        assert_eq!(
            Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | i64::MAX),
            None
        );
        assert_eq!(
            Options::try_from_int(i64::from(Flags::IGNORECASE.bits()) | i64::MAX),
            None
        );
        assert_eq!(
            Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | i64::from(Flags::IGNORECASE.bits()) | i64::MAX),
            None
        );
        assert_eq!(Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | 4096), None);
        assert_eq!(Options::try_from_int(i64::from(Flags::MULTILINE.bits()) | 4096), None);
        assert_eq!(
            Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | i64::from(Flags::MULTILINE.bits()) | 4096),
            None
        );
        assert_eq!(
            Options::from(Flags::EXTENDED.bits() | Flags::IGNORECASE.bits() | 64),
            opts
        );
        assert_eq!(Options::from(Flags::EXTENDED | Flags::IGNORECASE), opts);
        assert_ne!(
            Options::from(Flags::EXTENDED | Flags::IGNORECASE | Flags::MULTILINE),
            opts
        );
        assert_eq!(opts.ignore_case(), RegexpOption::Enabled);
        assert_eq!(opts.extended(), RegexpOption::Enabled);
        assert_eq!(opts.multiline(), RegexpOption::Disabled);

        let mut opts = Options::new();
        opts.flags |= Flags::EXTENDED | Flags::IGNORECASE | Flags::MULTILINE;
        assert_ne!(Options::from(Flags::EXTENDED), opts);
        assert_ne!(Options::from(Flags::IGNORECASE), opts);
        assert_ne!(Options::from(Flags::MULTILINE), opts);
        assert_eq!(
            Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | i64::MAX),
            None
        );
        assert_eq!(
            Options::try_from_int(i64::from(Flags::IGNORECASE.bits()) | i64::MAX),
            None
        );
        assert_eq!(
            Options::try_from_int(i64::from(Flags::MULTILINE.bits()) | i64::MAX),
            None
        );
        assert_eq!(Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | 4096), None);
        assert_eq!(Options::try_from_int(i64::from(Flags::IGNORECASE.bits()) | 4096), None);
        assert_eq!(Options::try_from_int(i64::from(Flags::MULTILINE.bits()) | 4096), None);
        assert_eq!(
            Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | i64::from(Flags::MULTILINE.bits()) | 4096),
            None
        );
        assert_ne!(Options::from(Flags::EXTENDED.bits() | 64), opts);
        assert_ne!(Options::from(Flags::IGNORECASE.bits() | 64), opts);
        assert_ne!(Options::from(Flags::MULTILINE.bits() | 64), opts);
        assert_ne!(
            Options::from(Flags::EXTENDED.bits() | Flags::MULTILINE.bits() | 64),
            opts
        );
        assert_ne!(Options::from(Flags::EXTENDED | Flags::IGNORECASE), opts);
        assert_ne!(Options::from(Flags::MULTILINE | Flags::IGNORECASE), opts);
        assert_eq!(
            Options::from(Flags::EXTENDED | Flags::IGNORECASE | Flags::MULTILINE),
            opts
        );
        assert_eq!(Options::from(Flags::ALL_REGEXP_OPTS), opts);
        assert_eq!(opts.ignore_case(), RegexpOption::Enabled);
        assert_eq!(opts.extended(), RegexpOption::Enabled);
        assert_eq!(opts.multiline(), RegexpOption::Enabled);

        // `ALL_REGEXP_OPTS` is equivalent to `EXTENDED | IGNORECASE | MULTILINE` flags.
        let mut opts = Options::new();
        opts.flags |= Flags::ALL_REGEXP_OPTS;
        assert_ne!(Options::from(Flags::EXTENDED), opts);
        assert_ne!(Options::from(Flags::IGNORECASE), opts);
        assert_ne!(Options::from(Flags::MULTILINE), opts);
        assert_eq!(
            Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | i64::MAX),
            None
        );
        assert_eq!(
            Options::try_from_int(i64::from(Flags::IGNORECASE.bits()) | i64::MAX),
            None
        );
        assert_eq!(
            Options::try_from_int(i64::from(Flags::MULTILINE.bits()) | i64::MAX),
            None
        );
        assert_eq!(Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | 4096), None);
        assert_eq!(Options::try_from_int(i64::from(Flags::IGNORECASE.bits()) | 4096), None);
        assert_eq!(Options::try_from_int(i64::from(Flags::MULTILINE.bits()) | 4096), None);
        assert_eq!(
            Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | i64::from(Flags::MULTILINE.bits()) | 4096),
            None
        );
        assert_ne!(Options::from(Flags::EXTENDED.bits() | 64), opts);
        assert_ne!(Options::from(Flags::IGNORECASE.bits() | 64), opts);
        assert_ne!(Options::from(Flags::MULTILINE.bits() | 64), opts);
        assert_ne!(
            Options::from(Flags::EXTENDED.bits() | Flags::MULTILINE.bits() | 64),
            opts
        );
        assert_ne!(Options::from(Flags::EXTENDED | Flags::IGNORECASE), opts);
        assert_ne!(Options::from(Flags::MULTILINE | Flags::IGNORECASE), opts);
        assert_eq!(
            Options::from(Flags::EXTENDED | Flags::IGNORECASE | Flags::MULTILINE),
            opts
        );
        assert_eq!(Options::from(Flags::ALL_REGEXP_OPTS), opts);
        assert_eq!(opts.ignore_case(), RegexpOption::Enabled);
        assert_eq!(opts.extended(), RegexpOption::Enabled);
        assert_eq!(opts.multiline(), RegexpOption::Enabled);

        // Ignore encoding and literal flags.
        let opts = Options::from(Flags::all());
        assert_ne!(Options::from(Flags::EXTENDED), opts);
        assert_ne!(Options::from(Flags::IGNORECASE), opts);
        assert_ne!(Options::from(Flags::MULTILINE), opts);
        assert_eq!(
            Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | i64::MAX),
            None
        );
        assert_eq!(
            Options::try_from_int(i64::from(Flags::IGNORECASE.bits()) | i64::MAX),
            None
        );
        assert_eq!(
            Options::try_from_int(i64::from(Flags::MULTILINE.bits()) | i64::MAX),
            None
        );
        assert_eq!(Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | 4096), None);
        assert_eq!(Options::try_from_int(i64::from(Flags::IGNORECASE.bits()) | 4096), None);
        assert_eq!(Options::try_from_int(i64::from(Flags::MULTILINE.bits()) | 4096), None);
        assert_eq!(
            Options::try_from_int(i64::from(Flags::EXTENDED.bits()) | i64::from(Flags::MULTILINE.bits()) | 4096),
            None
        );
        assert_ne!(Options::from(Flags::EXTENDED.bits() | 64), opts);
        assert_ne!(Options::from(Flags::IGNORECASE.bits() | 64), opts);
        assert_ne!(Options::from(Flags::MULTILINE.bits() | 64), opts);
        assert_ne!(
            Options::from(Flags::EXTENDED.bits() | Flags::MULTILINE.bits() | 64),
            opts
        );
        assert_ne!(Options::from(Flags::EXTENDED | Flags::IGNORECASE), opts);
        assert_ne!(Options::from(Flags::MULTILINE | Flags::IGNORECASE), opts);
        assert_eq!(
            Options::from(Flags::EXTENDED | Flags::IGNORECASE | Flags::MULTILINE),
            opts
        );
        assert_eq!(Options::from(Flags::ALL_REGEXP_OPTS), opts);
        assert_eq!(opts.ignore_case(), RegexpOption::Enabled);
        assert_eq!(opts.extended(), RegexpOption::Enabled);
        assert_eq!(opts.multiline(), RegexpOption::Enabled);
    }
}