Compare commits

...

5 Commits

@ -1,6 +1,6 @@
[package]
name = "rematch"
version = "0.2.0"
version = "0.3.2"
authors = ["Avril <flanchan@cumallover.me>"]
edition = "2024"
@ -25,7 +25,6 @@ unstable = ["regex/unstable"]
[dependencies]
pcre2 = { version = "0.2.9", optional = true }
clap = { version = "4.5.35", features = ["derive", "env", "string"] }
regex = { version = "1.11.1", features = ["use_std"] }
color-eyre = { version = "0.6.3", default-features = false, features = ["track-caller"] }
rayon = "1.10.0"
owo-colors = { version = "3.5.0", features = ["alloc", "supports-colors"] }

@ -1,294 +0,0 @@
//! Arguments and Cli-parsing
use super::*;
use std::{
str,
error, fmt,
borrow::{
Borrow, Cow, ToOwned,
},
path::{
Path, PathBuf,
},
//collections::BTreeSet as Set,
};
use clap::{
Parser,
Args,
Subcommand,
ValueEnum,
};
/// A value that may be provided, or may be deferred to be provided by `stdin` (/ written to `stdout`.)
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub enum MaybeValue<T = String>
{
Stdio,
Value(T),
}
impl<T> MaybeValue<T>
{
pub const STDIO_SYMBOL: &'static str = "-";
#[inline]
pub const fn is_stdio(&self) -> bool
{
match self {
Self::Stdio => true,
_ => false,
}
}
#[inline]
pub const fn value(&self) -> Option<&T>
{
match self {
Self::Value(v) => Some(&v),
_ => None
}
}
#[inline(always)]
pub const fn has_value(&self) -> bool
{
self.value().is_some()
}
/// Convert the value type to `U` (if there is one.)
///
/// e.g. to convert `let _: MaybeValue<PathBuf> = MaybeString::map_into();`
#[inline]
pub fn map_into<U: From<T>>(self) -> MaybeValue<U>
{
match self {
Self::Value(v) => MaybeValue::Value(v.into()),
Self::Stdio => MaybeValue::Stdio,
}
}
/// Consume into the `Value(T)` if possible, if not, return `Err(Self)`.
#[inline]
#[must_use]
pub fn try_into_value(self) -> Result<T, Self>
{
match self {
x @ Self::Stdio => Err(x),
Self::Value(v) => Ok(v),
}
}
}
impl<T: AsRef<str>> AsRef<str> for MaybeValue<T>
{
#[inline]
fn as_ref(&self) -> &str
{
match self {
Self::Stdio => Self::STDIO_SYMBOL,
Self::Value(v) => v.as_ref(),
}
}
}
impl<T: Borrow<str>> Borrow<str> for MaybeValue<T>
{
#[inline]
fn borrow(&self) -> &str
{
match self {
Self::Stdio => Self::STDIO_SYMBOL,
Self::Value(v) => v.borrow(),
}
}
}
impl<T> Default for MaybeValue<T>
{
#[inline]
fn default() -> Self
{
Self::Stdio
}
}
impl<T: Into<String>> MaybeValue<T>
{
#[inline]
pub fn into_string(self) -> Cow<'static, str>
{
match self {
Self::Value(v) => Cow::Owned(v.into()),
Self::Stdio => Cow::Borrowed(Self::STDIO_SYMBOL),
}
}
}
impl<T: Into<PathBuf>> MaybeValue<T>
{
#[inline]
pub fn into_path(self) -> Cow<'static, Path>
{
match self {
Self::Value(v) => Cow::Owned(v.into()),
Self::Stdio => Cow::Borrowed(Path::new(Self::STDIO_SYMBOL)),
}
}
}
impl<T: Into<PathBuf>> From<MaybeValue<T>> for Box<Path>
{
#[inline]
fn from(value: MaybeValue<T>) -> Self {
value.into_path().into_owned().into_boxed_path()
}
}
impl<T: Into<String>> From<MaybeValue<T>> for Box<str>
{
#[inline]
fn from(value: MaybeValue<T>) -> Self {
value.into_string().into_owned().into_boxed_str()
}
}
impl<T: Into<String>> From<MaybeValue<T>> for Cow<'static, str>
{
fn from(from: MaybeValue<T>) -> Self
{
from.into_string()
}
}
impl<T: From<String>> From<String> for MaybeValue<T>
{
#[inline]
fn from(from: String) -> Self
{
match &from[..] {
Self::STDIO_SYMBOL => Self::Stdio,
_ => Self::Value(from.into()),
}
}
}
impl<T> str::FromStr for MaybeValue<T>
where T: str::FromStr {
type Err = T::Err;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
Self::STDIO_SYMBOL => Ok(Self::Stdio),
s => T::from_str(s).map(Self::Value)
}
}
}
/// User-provied configuration of how the program should behave here
#[derive(Debug, Args)]
pub struct Config
{
/// Use the PCRE (JS-like) extended regular expression compiler.
///
/// __NOTE__: The binary must have been compiled with build feature `perl` to use this option.
///
/// # Feature difference
/// By default, the expression syntax does not support things like negative lookahead and other backtrack-requiring regex features.
///
/// ## Efficiency
/// Note that non-PCRE expressions are more efficient in general, and can also enable parallel processing of strings where there are many (e.g. a long list of lines from `stdin` can be matched against in parallel.)
///
/// It is ill-advised to enable PCRE on large inputs unless those features are required.
//TODO: Should we have PCRE on by default or not...? I think we should maybe have it on by default if the feature is enabled... But that will mess with input parallelism... XXX: Perhaps we can auto-detect if to use PCRE or not (e.g. try compiling to regex first, then PCRE if that fails?)
#[arg(short, long)] // XXX: Can we add a clap `value_parser!(FeatureOnBool<"perl">)` which fails to parse its `from_str()` impl if the feature is not enabled. Is this possible with what we currently have? We may be able to with macros, e.g expand a macro to `FeatureOnBool<"perl", const { cfg!(feature="perl") }>` or something similar? (NOTE: If `clap` has a better mechanism for this, use that instead of re-inventing it tho.)
// #[cfg(feature="perl")] //XXX: Do we want this option to be feature-gated? Or should we fail with error `if (! cfg!(feature="perl")) && self.extended)`? I think the latter would make things more easily (since the Regex engine gates PCRE-compilation transparently to the API user [see `crate::re::Regex`], we don't need to gate it this way outside of `re`, if we remove this gate we can just use `cfg!()` everywhere here which makes things **MUCH** cleaner..) It also means the user of a non-PCRE build will at least know why their PCRE flag is failing and that it can be built with the "perl" feature, instead of it being *totally* invisible to the user if the feature is off.
extended: bool,
/// Delimit read input/output strings from/to `stdin`/`stdout` by NUL ('\0') characters instead of newlines.
///
/// This only affects the output of each string's match groups, not the groups themselves, those will still be delimited by TAB literals in the output.
#[arg(short='0', long)]
pub zero: bool, //XXX: Add `--field=`/`--ifs` option, put these in same group. Maybe add `--delimit-groups=` to change the group delimiter from `\t` to user-specified value.
}
impl Config
{
/// Whether it is requested to use PCRE regex instead of regular regex.
///
/// # Interaction with feature gating of ~actual~ PCRE support via `feature="perl"`
/// Note that if the "perl" feature is not enabled, this may still return `true`.
/// If the user requests PCRE where it is not available, the caller should return an error/panic to the user telling her that.
#[inline(always)]
//TODO: Make `extended` public and remove this accessor?
pub fn use_pcre(&self) -> bool
{
//#![allow(unreachable_code)]
//#[cfg(feature="perl")] return self.extended; //TODO: See above comment on un-gating `self.extended`
//false
self.extended
}
}
/// A string value that may be provided to the CLI, or delegated to `stdio`.
pub type MaybeString = MaybeValue<Box<str>>;
/// A path that may represent an `stdio` file-descriptor instead of a named file.
pub type MaybePath = MaybeValue<Box<Path>>;
/// `rematch` is a simple command-line tool for matching & printing capture groups of an input string(s) against a regular expression.
///
/// The input string(s) can be provided in the command-line, or they can be provided as line delimited (by default) stream from `stdin`.
#[derive(Debug, Parser)]
#[command(name = env!("CARGO_PKG_NAME"), version, about, long_about)]
pub struct Cli
{
/// Configuration of the execution
#[command(flatten)]
pub config: Config,
//XXX: Should we make these fields public?
/// The input string to use, or `-` to read from stdin.
//TODO: Support multiple input strings in non-`stdin` case too. (XXX: How should this be handled...?)
string: MaybeString,
/// The regular expression to match `string` on.
regex: String,
/// The regex capture group indecies to print when matches on `string`.
#[arg(required= true, trailing_var_arg = true, allow_hyphen_values = false, num_args=1..)]
//TODO: Allow ranges & fallible captures, so lines that match group 1 but not 2 will not cause output failure if given `1 2?` but will if given `1 2` (XXX: Is this actually meaningful/possible? Can we do this at all? I'm pretty sure `/(?:(.))?/` still creates an (empty) group? So perhaps, syntax for failing on *empty* group matches...? like, `1! 2` for "group #1 *required*, group #2 is not requested?")
groups: Vec<usize>, // TODO: How to dedup (XXX: Do we want to de-dup? Maybe the user wants group `1` twice? I think it's fine (also we need to preserve user ordering of group indecied))
}
impl Cli {
/// Get the input string to match on
///
/// If the requested input is `stdin`, `None` is returned.
#[inline]
pub fn input_string(&self) -> Option<&str>
{
self.string.value().map(AsRef::as_ref)
}
/// Get the string to build the regular expression from
pub fn regex_string(&self) -> &str
{
&self.regex[..]
}
/// Get the match group(s) to print in the output
#[inline]
pub fn groups(&self) -> &[usize]
{
&self.groups[..]
}
/// Get the number of match groups requested.
#[inline]
pub fn num_groups(&self) -> usize
{
self.groups.len()
}
}
/// Parse the command-line arguments passed to the program
pub fn parse_cli() -> Cli
{
clap::Parser::parse()
}

@ -0,0 +1,127 @@
//! Extensions
use super::*;
use std::{
fmt,
};
/// Run an expression on an named value with a result type `Result<T, U>`.
/// Where `T` and `U` have *the same API surface* for the duration of the provided expression.
///
/// # Example
/// If there is a value `let mut value: Result<T, U>`, where `T: Write` & `U: BufWrite`;
/// the expression `value.flush()` is valid for both `T` and `U`.
/// Therefore, it can be simplified to be called as so: `unwrap_either(mut value => value.flush())`.
///
/// # Reference capture vs. `move` capture.
/// Note that by default, the identified value is **moved** *into* the expression.
/// The type of reference can be controlled by appending `ref`, `mut`, or `ref mut` to the ident.
///
/// Identifier capture table:
/// - **none** ~default~ - Capture by move, value is immutable in expression.
/// - `mut` - Capture by move, value is mutable in expression.
/// - `ref` - Capture by ref, value is immutable (`&value`) in expression.
/// - `ref mut` - Capture by mutable ref, value is mutable (`&mut value`) in expression. (__NOTE__: `value` must be defined as mutable to take a mutable reference of it.)
///
/// Essentially the same rules as any `match` branch pattern.
macro_rules! unwrap_either {
($res:ident => $($rest:tt)+) => {
match $res {
Ok(ref mut $res) => $($rest)+,
Err(ref mut $res) => $($rest)+,
}
};
(ref mut $res:ident => $($rest:tt)+) => {
match $res {
Ok(ref mut $res) => $($rest)+,
Err(ref mut $res) => $($rest)+,
}
};
(ref $res:ident => $($rest:tt)+) => {
match $res {
Ok(ref $res) => $($rest)+,
Err(ref $res) => $($rest)+,
}
};
(mut $res:ident => $($rest:tt)+) => {
match $res {
Ok(mut $res) => $($rest)+,
Err(mut $res) => $($rest)+,
}
};
}
pub(crate) use unwrap_either;
#[derive(Debug, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct DisjointString<'a, T: ?Sized>([&'a T]);
macro_rules! disjoint {
[$($ex:expr),+] => {
$crate::ext::DisjointString::from_array(& [$($ex),+])
};
}
impl<'a, T: ?Sized> DisjointString<'a, T>
where T: fmt::Display
{
#[inline]
pub const fn from_array<'o: 'a, const N: usize>(strings: &'o [&'a T; N]) -> &'o Self
{
Self::new(strings.as_slice())
}
#[inline]
pub const fn new<'o: 'a>(strings: &'o [&'a T]) -> &'o Self
{
// SAFETY: Transparent newtype wrapper over `[&'a T]`
unsafe {
std::mem::transmute(strings)
}
}
}
impl<'a, T: ?Sized> DisjointString<'a, T>
{
#[inline]
pub const fn len(&self) -> usize
{
self.0.len()
}
#[inline]
pub fn iter(&self) -> impl Iterator<Item = &T> + ExactSizeIterator + std::iter::FusedIterator + std::iter::DoubleEndedIterator
{
self.0.iter().map(|&x| x)
}
#[inline]
pub fn into_iter<'o: 'a>(&'o self) -> impl Iterator<Item = &'a T> + ExactSizeIterator + std::iter::FusedIterator + std::iter::DoubleEndedIterator + 'o
{
self.0.into_iter().map(|&x|x)
}
}
impl<'a, T: ?Sized> AsRef<[&'a T]> for DisjointString<'a, T>
{
#[inline]
fn as_ref(&self) -> &[&'a T]
{
&self.0
}
}
impl<'a, T: ?Sized> fmt::Display for DisjointString<'a, T>
where T: fmt::Display
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
for &s in &self.0 {
s.fmt(f)?;
}
Ok(())
}
}
pub(crate) use disjoint;

@ -2,7 +2,7 @@
mod re;
mod text;
mod args;
mod ext; use ext::*;
use color_eyre::{
eyre::{
@ -19,15 +19,43 @@ fn initialise() -> eyre::Result<()>
Ok(())
}
fn print_group<S: ?Sized, G, T>(to: &mut S, g: G, group: usize) -> std::io::Result<()>
#[inline]
fn print_groups<'a, S: ?Sized, G, T: 'a, I>(to: &mut S, g: G, groups: I) -> std::io::Result<()>
where S: std::io::Write,
G: IntoIterator<Item = Option<T>>,
T: std::borrow::Borrow<str>
G: IntoIterator<Item = &'a Option<T>> + Clone + Copy, // NOTE: Copy bound to ensure we're not accidentally doing deep clones of `g`.
//G: std::ops::Index<usize>, G::Output: std::borrow::Borrow<Option<T>>,
T: std::borrow::Borrow<str>,
I: IntoIterator<Item: std::borrow::Borrow<usize>/*, IntoIter: ExactSizeIterator*/>,
{
match g.into_iter().nth(group) {
Some(None) => writeln!(to, ""),
Some(Some(g)) => writeln!(to, "{}", g.borrow()),
None => Ok(()),
use std::borrow::Borrow;
let mut first = true;
for group in groups.into_iter() {
let group = group.borrow();
// // Moved to into match group (skipping invalid groups.)
// if !first {
// write!(to, "\t")?;
// }
let print_delim = || first.then_some("").unwrap_or("\t"); // If it's not the first iteration, print `\t`.
match g.into_iter().nth(*group) {
Some(None) => write!(to, "{}", print_delim()),
Some(Some(g)) => write!(to, "{}{}", print_delim(), g.borrow()),
//TODO: What should be the behaviour of a non-existent group index here? (NOTE: This now corresponds to the previous `g.len() > group` check in caller.) // (NOTE: The original behaviour is to just ignore groups that are out of range entirely (i.e. no printing, no delimit char, no error,) maybe treat non-existent groups as non-matched groups and *just* print the delim char?)
// (NOTE: Moved out of branch, see above ^) // None if !first => write!(to, "\t"),
// XXX: Should this do what it does now...? Or should it `break` to prevent the checking for more groups...? Print a warning maybe...?
None => {
eprintln!("Warning: Invalid group index {}!", group);
continue; // Do not set `first = false` if it was an invalid index.
//Ok(())
},
}?;
first = false;
}
// If `first == true`, no groups were printed, so we do not print the new-line.
if !first {
to.write_all(b"\n")
} else {
Ok(())
}
}
@ -35,42 +63,91 @@ fn main() -> eyre::Result<()>
{
initialise().wrap_err("Fatal: Failed to install panic handle")?;
// let cli = args::parse_cli();//.wrap_err("Error parsing command-line arguments")?;
//
// eprintln!("{:#?}", cli);
//let cli = args::parse_cli();//.wrap_err("Error parsing command-line arguments")?;
//eprintln!("{:#?}", cli);
// return Ok(());
let args: Vec<String> = std::env::args().collect();
let args: re::FrozenVec<re::FrozenString> = std::env::args().map(String::into_boxed_str).collect();
if args.len() < 4 {
println!("Usage: {} <str> <regex> <group>", args[0]);
use owo_colors::OwoColorize;
use owo_colors::Stream;
macro_rules! colour {
(in $name:ident: $fmt:expr => $col:ident) => {
$fmt.if_supports_color(Stream::$name, |text| text.$col())
};
($fmt:expr => $col:ident) => {
colour!(in Stdout: $fmt => $col)
}
}
println!("rematch v{}: Regular-expression group matcher", env!("CARGO_PKG_VERSION"));
println!("");
println!("Usage: {} <str> <regex> <group>...", args[0]);
println!("Pass `-' as `<str>' to read lines from stdin");
std::process::exit(1);
println!("");
println!("Enabled Features:");
if cfg!(feature="perl") {
println!("{}\t\t\tEnable PCRE2 (extended) regular-expressions.\n\t\t\tNote that PCRE2 regex engine matches on *bytes*, not *characters*; meaning if a match cuts a vlid UTF8 codepoint into an invalid one, the output will replace the invalid characters with U+FFFD REPLACEMENT CHARACTER.", colour!(disjoint!["+", "perl"] => bright_red));
} else {
println!("{}\t\t\tPCRE2 (extended) features are disabled; a faster but less featureful regular expression engine (that matches on UTF8 strings instead of raw bytes) is used instead.", colour!(disjoint!["-", "perl"] => blue));
}
if cfg!(feature="unstable") {
println!("{}\t\tUnstable optimisations evailable & enabled for build.", colour!(disjoint!["+", "unstable"] => red));
} else {
println!("{}\t\tUnstable optimisations disabled / not available for build.", colour!(disjoint!["-", "unstable"] => bright_blue));
}
std::process::exit(1)
} else {
let re = re::Regex::compile(&args[2])?;
let text = &args[1];
let group: usize = args[3].parse().expect("Invalid group number.");
let groups = &args[3..];
if groups.len() < 1 {
eprintln!("Warning: No capture groups requested.");
// NOTE: Unexpected branch...
return Ok(());
}
let groups = groups.iter().enumerate()
.map(|(i, x)| x.parse()
.with_section(|| format!("{:?}", groups).header("Groups specified were"))
.with_section(|| x.clone().header("Specified capture group index was"))
.with_section(move || i.header("Argument index in provided groups")))
.collect::<Result<Box<[usize]>, _>>()
.wrap_err("Invalid group index specified")?;
//TODO: XXX: How to handle multiple groups in `stdin_lines()` case?
//let group = groups[0]; //args[3].parse().expect("Invalid group number.");
use std::io::Write;
let mut stdout = std::io::stdout();
if text == "-" {
let stdout = if &text[..] == "-" {
let mut stdout = std::io::BufWriter::new(stdout.lock());
text::stdin_lines(|text| -> eyre::Result<bool> {
let mut stdout = stdout.lock();
match re.exec(&text)? {
Some(g) if g.len() > group => print_group(&mut stdout, g, group)?, //println!("{}", &g[group]),
Some(g) /*if g.len() > group*/ => // NOTE: This check branch has now been moved into `print_groups()`
print_groups(&mut stdout, &g, &groups)?, //println!("{}", &g[group]),
_ => (),
}
Ok(true)
})?;
Some(stdout)
} else {
match re.exec(&text)? {
Some(g) if g.len() > group => print_group(&mut stdout, g, group)?,//println!("{}", &g.nth(group).unwrap().map(|x| x.as_ref()).unwrap_or("")),
Some(g) /*if g.len() > group*/ => print_groups(&mut stdout, &g[..], &groups)?,//println!("{}", &g.nth(group).unwrap().map(|x| x.as_ref()).unwrap_or("")),
_ => (),
}
}
stdout.flush().unwrap();
None
}.ok_or_else(move || stdout);
unwrap_either!(mut stdout => stdout.flush()).unwrap();
}
Ok(())
}

Loading…
Cancel
Save