-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
29 changed files
with
7,381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use { | ||
lexopt::{Arg, Parser}, | ||
regex_automata::util::syntax, | ||
regex_lite::Regex, | ||
}; | ||
|
||
use crate::args::{self, Configurable, Usage}; | ||
|
||
/// Exposes the configuration for the top-level `Regex` API. | ||
#[derive(Debug, Default)] | ||
pub struct Config { | ||
size_limit: Option<usize>, | ||
} | ||
|
||
impl Config { | ||
/// Builds a `Regex` from the given syntax configuration and sequence of | ||
/// patterns. This returns an error is `patterns.len() != 1`. | ||
/// | ||
/// Note that this also returns an error if any syntax options are set | ||
/// that aren't supported by `regex-lite`. | ||
pub fn from_patterns( | ||
&self, | ||
syntax: &syntax::Config, | ||
patterns: &[String], | ||
) -> anyhow::Result<Regex> { | ||
anyhow::ensure!( | ||
patterns.len() == 1, | ||
"API-level regex requires exactly one pattern, \ | ||
but {} were given", | ||
patterns.len(), | ||
); | ||
anyhow::ensure!( | ||
!syntax.get_octal(), | ||
"regex-lite does not support octal mode", | ||
); | ||
anyhow::ensure!( | ||
syntax.get_utf8(), | ||
"regex-lite does not support disabling UTF-8 mode", | ||
); | ||
anyhow::ensure!( | ||
syntax.get_unicode(), | ||
"regex-lite does not support disabling Unicode mode", | ||
); | ||
let mut b = regex_lite::RegexBuilder::new(&patterns[0]); | ||
b.case_insensitive(syntax.get_case_insensitive()); | ||
b.multi_line(syntax.get_multi_line()); | ||
b.crlf(syntax.get_crlf()); | ||
b.dot_matches_new_line(syntax.get_dot_matches_new_line()); | ||
b.swap_greed(syntax.get_swap_greed()); | ||
b.ignore_whitespace(syntax.get_ignore_whitespace()); | ||
b.nest_limit(syntax.get_nest_limit()); | ||
b.size_limit(self.size_limit.unwrap_or(usize::MAX)); | ||
b.build().map_err(anyhow::Error::from) | ||
} | ||
} | ||
|
||
impl Configurable for Config { | ||
fn configure( | ||
&mut self, | ||
p: &mut Parser, | ||
arg: &mut Arg, | ||
) -> anyhow::Result<bool> { | ||
match *arg { | ||
Arg::Long("size-limit") => { | ||
self.size_limit = args::parse_maybe(p, "--size-limit")?; | ||
} | ||
_ => return Ok(false), | ||
} | ||
Ok(true) | ||
} | ||
|
||
fn usage(&self) -> &[Usage] { | ||
const USAGES: &'static [Usage] = &[Usage::new( | ||
"--size-limit", | ||
"Set a limit on heap used by a regex.", | ||
r#" | ||
This sets a limit, in bytes, on the heap memory used by a regex. | ||
The special value 'none' indicates that no size limit should be imposed. | ||
"#, | ||
)]; | ||
USAGES | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[package] | ||
name = "regex-lite" | ||
version = "0.1.0" #:version | ||
authors = ["The Rust Project Developers", "Andrew Gallant <jamslam@gmail.com>"] | ||
license = "MIT OR Apache-2.0" | ||
repository = "https://github.com/rust-lang/regex/tree/master/regex-lite" | ||
documentation = "https://docs.rs/regex-lite" | ||
description = """ | ||
A lightweight regex engine that optimizes for binary size and compilation time. | ||
""" | ||
workspace = ".." | ||
edition = "2021" | ||
rust-version = "1.60.0" | ||
autotests = false | ||
|
||
# Features are documented in the "Crate features" section of the crate docs: | ||
# https://docs.rs/regex-syntax/*/#crate-features | ||
[features] | ||
default = ["std"] | ||
std = [] | ||
|
||
[dev-dependencies] | ||
anyhow = "1.0.69" | ||
regex-test = { path = "../regex-test", version = "0.1.0" } | ||
|
||
[[test]] | ||
path = "tests/lib.rs" | ||
name = "integration" | ||
|
||
[package.metadata.docs.rs] | ||
# We want to document all features. | ||
all-features = true | ||
# To test this locally, run: | ||
# | ||
# RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features | ||
rustdoc-args = ["--cfg", "docsrs"] |
Oops, something went wrong.