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
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(clippy::cargo)]
#![allow(clippy::option_if_let_else)]
#![cfg_attr(test, allow(clippy::non_ascii_literal))]
#![allow(unknown_lints)]
// TODO: warn on missing docs once crate is API-complete.
// #![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![warn(missing_copy_implementations)]
#![warn(rust_2018_idioms)]
#![warn(rust_2021_compatibility)]
#![warn(trivial_casts, trivial_numeric_casts)]
#![warn(unused_qualifications)]
#![warn(variant_size_differences)]
#![forbid(unsafe_code)]
// Enable feature callouts in generated documentation:
// https://doc.rust-lang.org/beta/unstable-book/language-features/doc-cfg.html
//
// This approach is borrowed from tokio.
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_alias))]

// Ensure code blocks in README.md compile
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
mod readme {}

use core::fmt;
use core::num::NonZeroUsize;
use std::borrow::Cow;

use bstr::ByteSlice;

mod debug;
mod encoding;
mod error;
mod options;
mod regexp;

pub use debug::Debug;
pub use encoding::{Encoding, InvalidEncodingError};
pub use error::{ArgumentError, Error, RegexpError, SyntaxError};
pub use options::{Options, RegexpOption};

bitflags::bitflags! {
    #[derive(Default)]
    pub struct Flags: u8 {
        const IGNORECASE      = 0b0000_0001;
        const EXTENDED        = 0b0000_0010;
        const MULTILINE       = 0b0000_0100;
        const ALL_REGEXP_OPTS = Self::IGNORECASE.bits | Self::EXTENDED.bits | Self::MULTILINE.bits;

        const FIXEDENCODING   = 0b0001_0000;
        const NOENCODING      = 0b0010_0000;

        const LITERAL         = 0b1000_0000;
    }
}

/// The string matched by the last successful match.
pub const LAST_MATCHED_STRING: &[u8] = b"$&";

/// The string to the left of the last successful match.
pub const STRING_LEFT_OF_MATCH: &[u8] = b"$`";

/// The string to the right of the last successful match.
pub const STRING_RIGHT_OF_MATCH: &[u8] = b"$'";

/// The highest group matched by the last successful match.
// TODO: implement this.
pub const HIGHEST_MATCH_GROUP: &[u8] = b"$+";

/// The information about the last match in the current scope.
pub const LAST_MATCH: &[u8] = b"$~";

/// A `Source` represents the literal contents used to construct a given
/// `Regexp`.
///
/// When [`Regexp`]s are constructed with a `/.../` literal, [`Regexp#source`]
/// refers to the literal characters contained within the `/` delimeters.
/// For example, `/\t/.source.bytes` has byte sequence `[92, 116]`.
///
/// When `Regexp`s are constructed with [`Regexp::compile`], [`Regexp#source`]
/// refers to the argument passed to `compile`. For example,
/// `Regexp.compile("\t").source.bytes` has byte sequence `[9]`.
///
/// [`Regexp#inspect`] prints `"/#{source}/"`.
///
/// [`Regexp`]: https://ruby-doc.org/core-2.6.3/Regexp.html
/// [`Regexp#source`]: https://ruby-doc.org/core-2.6.3/Regexp.html#method-i-source
/// [`Regexp::compile`]: https://ruby-doc.org/core-2.6.3/Regexp.html#method-c-compile
/// [`Regexp#inspect`]: https://ruby-doc.org/core-2.6.3/Regexp.html#method-i-inspect
#[derive(Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Source {
    pattern: Vec<u8>,
    options: Options,
}

impl fmt::Debug for Source {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Source")
            .field("pattern", &self.pattern.as_bstr())
            .field("options", &self.options)
            .finish()
    }
}

impl From<Config> for Source {
    fn from(config: Config) -> Self {
        Self::with_pattern_and_options(config.pattern.clone(), config.options)
    }
}

impl From<&Config> for Source {
    fn from(config: &Config) -> Self {
        Self::with_pattern_and_options(config.pattern.clone(), config.options)
    }
}

impl Source {
    /// Construct a new, empty `Source`.
    ///
    /// # Examples
    ///
    /// ```
    /// use spinoso_regexp::Source;
    ///
    /// const SOURCE: Source = Source::new();
    /// assert!(SOURCE.pattern().is_empty());
    /// assert!(SOURCE.options().as_display_modifier().is_empty());
    /// ```
    #[must_use]
    pub const fn new() -> Self {
        Self {
            pattern: Vec::new(),
            options: Options::new(),
        }
    }

    /// Construct a new `Source` with the given pattern and [`Options`].
    ///
    /// # Examples
    ///
    /// ```
    /// use spinoso_regexp::{Options, Source};
    ///
    /// let source = Source::with_pattern_and_options(
    ///     b"Artichoke( Ruby)?".to_vec(),
    ///     Options::with_ignore_case(),
    /// );
    /// assert_eq!(source.pattern(), b"Artichoke( Ruby)?");
    /// assert_eq!(source.options().as_display_modifier(), "i");
    /// ```
    #[must_use]
    pub const fn with_pattern_and_options(pattern: Vec<u8>, options: Options) -> Self {
        Self { pattern, options }
    }

    /// Whether this source was parsed with ignore case enabled.
    ///
    /// # Examples
    ///
    /// ```
    /// use spinoso_regexp::{Options, Source};
    ///
    /// let source = Source::new();
    /// assert!(!source.is_casefold());
    ///
    /// let source = Source::with_pattern_and_options(
    ///     b"Artichoke( Ruby)?".to_vec(),
    ///     Options::with_ignore_case(),
    /// );
    /// assert!(source.is_casefold());
    /// ```
    #[must_use]
    pub const fn is_casefold(&self) -> bool {
        self.options.ignore_case().is_enabled()
    }

    /// 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.
    ///
    /// A source's literal flag can only be set using [`Options::try_from_int`].
    #[must_use]
    pub const fn is_literal(&self) -> bool {
        self.options.is_literal()
    }

    /// Extracts a slice containing the entire pattern.
    ///
    /// # Examples
    ///
    /// ```
    /// use spinoso_regexp::{Options, Source};
    ///
    /// let source = Source::with_pattern_and_options(
    ///     b"Artichoke( Ruby)?".to_vec(),
    ///     Options::with_ignore_case(),
    /// );
    /// assert_eq!(source.pattern(), b"Artichoke( Ruby)?");
    /// ```
    #[must_use]
    pub fn pattern(&self) -> &[u8] {
        self.pattern.as_slice()
    }

    /// Return a copy of the underlying [`Options`].
    ///
    /// # Examples
    ///
    /// ```
    /// use spinoso_regexp::{Options, Source};
    ///
    /// let source = Source::with_pattern_and_options(
    ///     b"Artichoke( Ruby)?".to_vec(),
    ///     Options::with_ignore_case(),
    /// );
    /// assert_eq!(source.options().as_display_modifier(), "i");
    /// ```
    #[must_use]
    pub const fn options(&self) -> Options {
        self.options
    }
}

/// A `Config` represents the parsed, expanded, and normalized pattern and
/// options used to initialize a `Regexp`.
///
/// A `Config` is derived from a [`Source`].
///
/// When a `Regexp` is cloned, it is cloned from its compiled `Config`.
#[derive(Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Config {
    pattern: Vec<u8>,
    options: Options,
}

impl fmt::Debug for Config {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Source")
            .field("pattern", &self.pattern.as_bstr())
            .field("options", &self.options)
            .finish()
    }
}

impl From<Source> for Config {
    fn from(source: Source) -> Self {
        Self::with_pattern_and_options(source.pattern.clone(), source.options)
    }
}

impl From<&Source> for Config {
    fn from(source: &Source) -> Self {
        Self::with_pattern_and_options(source.pattern.clone(), source.options)
    }
}

impl Config {
    /// Construct a new, empty `Config`.
    ///
    /// # Examples
    ///
    /// ```
    /// use spinoso_regexp::Config;
    ///
    /// const CONFIG: Config = Config::new();
    /// assert!(CONFIG.pattern().is_empty());
    /// assert!(CONFIG.options().as_display_modifier().is_empty());
    /// ```
    #[must_use]
    pub const fn new() -> Self {
        Self {
            pattern: Vec::new(),
            options: Options::new(),
        }
    }

    /// Construct a new `Config` with the given pattern and [`Options`].
    ///
    /// # Examples
    ///
    /// ```
    /// use spinoso_regexp::{Config, Options};
    ///
    /// let config = Config::with_pattern_and_options(
    ///     b"Artichoke( Ruby)?".to_vec(),
    ///     Options::with_ignore_case(),
    /// );
    /// assert_eq!(config.pattern(), b"Artichoke( Ruby)?");
    /// assert_eq!(config.options().as_display_modifier(), "i");
    /// ```
    #[must_use]
    pub const fn with_pattern_and_options(pattern: Vec<u8>, options: Options) -> Self {
        Self { pattern, options }
    }

    /// Extracts a slice containing the entire pattern.
    ///
    /// # Examples
    ///
    /// ```
    /// use spinoso_regexp::{Config, Options};
    ///
    /// let config = Config::with_pattern_and_options(
    ///     b"Artichoke( Ruby)?".to_vec(),
    ///     Options::with_ignore_case(),
    /// );
    /// assert_eq!(config.pattern(), b"Artichoke( Ruby)?");
    /// ```
    #[must_use]
    pub fn pattern(&self) -> &[u8] {
        self.pattern.as_slice()
    }

    /// Return a copy of the underlying [`Options`].
    ///
    /// # Examples
    ///
    /// ```
    /// use spinoso_regexp::{Config, Options};
    ///
    /// let config = Config::with_pattern_and_options(
    ///     b"Artichoke( Ruby)?".to_vec(),
    ///     Options::with_ignore_case(),
    /// );
    /// assert_eq!(config.options().as_display_modifier(), "i");
    /// ```
    #[must_use]
    pub const fn options(&self) -> Options {
        self.options
    }
}

/// Global variable name for the nth capture group from a `Regexp` match.
///
/// Ruby tags captures from the last `Regexp` match with global variables of the
/// form `$1`, `$2`, `$3`, etc. This function accepts [`NonZeroUsize`] because
/// `$0` is not a valid `Regexp` capture group name in Ruby (`$0` refers to the
/// program name).
///
/// This function may return either a `&'static str` or an owned [`String`] for
/// a given capture group name. This function differs from
/// [`nth_match_group_bytes`] by returning `Cow<'static, str>`.
///
///
/// # Examples
///
/// ```
/// use core::num::NonZeroUsize;
/// use spinoso_regexp::nth_match_group;
///
/// # fn example() -> Option<()> {
/// let group = NonZeroUsize::new(1)?;
/// let global_name = nth_match_group(group);
/// assert_eq!(&*global_name, "$1");
///
/// let group = NonZeroUsize::new(27)?;
/// let global_name = nth_match_group(group);
/// assert_eq!(&*global_name, "$27");
/// # None
/// # }
/// ```
#[must_use]
pub fn nth_match_group(group: NonZeroUsize) -> Cow<'static, str> {
    match group.get() {
        1 => Cow::Borrowed("$1"),
        2 => Cow::Borrowed("$2"),
        3 => Cow::Borrowed("$3"),
        4 => Cow::Borrowed("$4"),
        5 => Cow::Borrowed("$5"),
        6 => Cow::Borrowed("$6"),
        7 => Cow::Borrowed("$7"),
        8 => Cow::Borrowed("$8"),
        9 => Cow::Borrowed("$9"),
        10 => Cow::Borrowed("$10"),
        11 => Cow::Borrowed("$11"),
        12 => Cow::Borrowed("$12"),
        13 => Cow::Borrowed("$13"),
        14 => Cow::Borrowed("$14"),
        15 => Cow::Borrowed("$15"),
        16 => Cow::Borrowed("$16"),
        17 => Cow::Borrowed("$17"),
        18 => Cow::Borrowed("$18"),
        19 => Cow::Borrowed("$19"),
        20 => Cow::Borrowed("$20"),
        num => {
            let mut buf = String::from("$");
            // Suppress fmt errors because this function is infallible.
            //
            // In practice `itoa::fmt` will never error because the `fmt::Write`
            // impl for `String` never panics.
            let _ = itoa::fmt(&mut buf, num);
            Cow::Owned(buf)
        }
    }
}

/// Global variable name for the nth capture group from a `Regexp` match.
///
/// Ruby tags captures from the last `Regexp` match with global variables of the
/// form `$1`, `$2`, `$3`, etc. This function accepts [`NonZeroUsize`] because
/// `$0` is not a valid `Regexp` capture group name in Ruby (`$0` refers to the
/// program name).
///
/// This function may return either a `&'static [u8]` or an owned [`Vec<u8>`]
/// for a given capture group name.  This function differs from
/// [`nth_match_group`] by returning `Cow<'static, [u8]>`.
///
/// # Examples
///
/// ```
/// use core::num::NonZeroUsize;
/// use spinoso_regexp::nth_match_group_bytes;
///
/// # fn example() -> Option<()> {
/// let group = NonZeroUsize::new(1)?;
/// let global_name = nth_match_group_bytes(group);
/// assert_eq!(&*global_name, b"$1");
///
/// let group = NonZeroUsize::new(27)?;
/// let global_name = nth_match_group_bytes(group);
/// assert_eq!(&*global_name, b"$27");
/// # None
/// # }
/// ```
///
/// [`Vec<u8>`]: std::vec::Vec
#[must_use]
pub fn nth_match_group_bytes(group: NonZeroUsize) -> Cow<'static, [u8]> {
    match nth_match_group(group) {
        Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()),
        Cow::Owned(s) => Cow::Owned(s.into_bytes()),
    }
}

#[cfg(test)]
mod tests {
    use core::num::NonZeroUsize;
    use std::borrow::Cow;

    use super::{nth_match_group, nth_match_group_bytes};

    #[test]
    fn match_group_symbol() {
        for num in 1..=1024 {
            let num = NonZeroUsize::new(num).unwrap();
            let sym = nth_match_group(num);
            let num = format!("{}", num);
            assert!(sym.len() > 1);
            assert_eq!(&sym[0..1], "$");
            assert_eq!(&sym[1..], num);
        }
    }

    #[test]
    fn some_globals_are_static_slices() {
        for num in 1..=20 {
            let num = NonZeroUsize::new(num).unwrap();
            let sym = nth_match_group(num);
            assert!(matches!(sym, Cow::Borrowed(_)));
        }
        for num in 21..=1024 {
            let num = NonZeroUsize::new(num).unwrap();
            let sym = nth_match_group(num);
            assert!(matches!(sym, Cow::Owned(_)));
        }
    }

    #[test]
    fn nth_group_matches_nth_group_bytes() {
        for num in 1..=1024 {
            let num = NonZeroUsize::new(num).unwrap();
            let sym_str = nth_match_group(num);
            let sym_bytes = nth_match_group_bytes(num);
            assert_eq!(&*sym_str.as_bytes(), &*sym_bytes);
        }
    }
}