pub struct Source { /* private fields */ }
Expand description
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 /
delimiters.
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}/"
.
Implementations§
Source§impl Source
impl Source
Sourcepub const fn new() -> Self
pub const fn new() -> Self
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());
Sourcepub const fn with_pattern_and_options(
pattern: Vec<u8>,
options: Options,
) -> Self
pub const fn with_pattern_and_options( pattern: Vec<u8>, options: Options, ) -> Self
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");
Sourcepub const fn is_casefold(&self) -> bool
pub const fn is_casefold(&self) -> bool
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());
Sourcepub const fn is_literal(&self) -> bool
pub const fn is_literal(&self) -> bool
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 regarding capturing groups and report parse failures differently.
A source’s literal flag can only be set using Options::try_from_int
.
Sourcepub fn pattern(&self) -> &[u8] ⓘ
pub fn pattern(&self) -> &[u8] ⓘ
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)?");