replace regex crate with fancy_regex (#14646)

# Description

We removed the regex crate long ago but there were a few instances where
we could not remove it because fancy-regex did not have a split/splitn,
and maybe other functions. Those functions now exist in the latest
fancy-regex crate so we can now remove it.
 
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the
tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
Darren Schroeder
2025-01-01 17:37:50 -06:00
committed by GitHub
parent c6523eb8d9
commit f69b22f00b
9 changed files with 181 additions and 124 deletions

View File

@ -1,7 +1,6 @@
use fancy_regex::{escape, Regex};
use nu_engine::command_prelude::*;
use regex::Regex;
#[derive(Clone)]
pub struct SubCommand;
@ -182,7 +181,7 @@ fn split_column(
let regex = if args.has_regex {
Regex::new(&args.separator.item)
} else {
let escaped = regex::escape(&args.separator.item);
let escaped = escape(&args.separator.item);
Regex::new(&escaped)
}
.map_err(|e| ShellError::GenericError {
@ -220,10 +219,12 @@ fn split_column_helper(
let split_result: Vec<_> = match max_split {
Some(max_split) => separator
.splitn(&s, max_split)
.filter_map(|x| x.ok())
.filter(|x| !(collapse_empty && x.is_empty()))
.collect(),
None => separator
.split(&s)
.filter_map(|x| x.ok())
.filter(|x| !(collapse_empty && x.is_empty()))
.collect(),
};

View File

@ -1,7 +1,6 @@
use fancy_regex::Regex;
use nu_engine::command_prelude::*;
use regex::Regex;
#[derive(Clone)]
pub struct SubCommand;
@ -193,7 +192,7 @@ impl Matcher {
Ok(match self {
Matcher::Regex(regex) => {
if let Ok(rhs_str) = rhs.coerce_str() {
regex.is_match(&rhs_str)
regex.is_match(&rhs_str).unwrap_or(false)
} else {
false
}

View File

@ -1,7 +1,6 @@
use fancy_regex::{escape, Regex};
use nu_engine::command_prelude::*;
use regex::Regex;
#[derive(Clone)]
pub struct SubCommand;
@ -158,7 +157,7 @@ fn split_row(
let regex = if args.has_regex {
Regex::new(&args.separator.item)
} else {
let escaped = regex::escape(&args.separator.item);
let escaped = escape(&args.separator.item);
Regex::new(&escaped)
}
.map_err(|e| ShellError::GenericError {
@ -187,11 +186,35 @@ fn split_row_helper(v: &Value, regex: &Regex, max_split: Option<usize>, name: Sp
match max_split {
Some(max_split) => regex
.splitn(&s, max_split)
.map(|x: &str| Value::string(x, v_span))
.map(|x| match x {
Ok(val) => Value::string(val, v_span),
Err(err) => Value::error(
ShellError::GenericError {
error: "Error with regular expression".into(),
msg: err.to_string(),
span: Some(v_span),
help: None,
inner: vec![],
},
v_span,
),
})
.collect(),
None => regex
.split(&s)
.map(|x: &str| Value::string(x, v_span))
.map(|x| match x {
Ok(val) => Value::string(val, v_span),
Err(err) => Value::error(
ShellError::GenericError {
error: "Error with regular expression".into(),
msg: err.to_string(),
span: Some(v_span),
help: None,
inner: vec![],
},
v_span,
),
})
.collect(),
}
} else {