diff --git a/Cargo.lock b/Cargo.lock index 3a3e4b2c4d2..a4f648d164d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1413,28 +1413,6 @@ version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" -[[package]] -name = "onig" -version = "6.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c4b31c8722ad9171c6d77d3557db078cab2bd50afcc9d09c8b315c59df8ca4f" -dependencies = [ - "bitflags 1.3.2", - "libc", - "once_cell", - "onig_sys", -] - -[[package]] -name = "onig_sys" -version = "69.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b829e3d7e9cc74c7e315ee8edb185bf4190da5acde74afd7fc59c35b1f086e7" -dependencies = [ - "cc", - "pkg-config", -] - [[package]] name = "ordered-multimap" version = "0.6.0" @@ -1543,12 +1521,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" -[[package]] -name = "pkg-config" -version = "0.3.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" - [[package]] name = "platform-info" version = "2.0.2" @@ -2459,7 +2431,7 @@ dependencies = [ "clap", "num-bigint", "num-traits", - "onig", + "regex", "uucore", ] diff --git a/Cargo.toml b/Cargo.toml index fac6f5e899a..1d54905fe2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -299,7 +299,6 @@ num-bigint = "0.4.4" num-traits = "0.2.17" number_prefix = "0.4" once_cell = "1.18.0" -onig = { version = "~6.4", default-features = false } parse_datetime = "0.5.0" phf = "0.11.2" phf_codegen = "0.11.2" diff --git a/src/uu/expr/Cargo.toml b/src/uu/expr/Cargo.toml index 3f0b5ca76ad..c0eb5148c01 100644 --- a/src/uu/expr/Cargo.toml +++ b/src/uu/expr/Cargo.toml @@ -18,7 +18,7 @@ path = "src/expr.rs" clap = { workspace = true } num-bigint = { workspace = true } num-traits = { workspace = true } -onig = { workspace = true } +regex = { workspace = true } uucore = { workspace = true } [[bin]] diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index c55fb0bdc6a..46f339b74be 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -12,7 +12,7 @@ use num_bigint::BigInt; use num_traits::Zero; -use onig::{Regex, RegexOptions, Syntax}; +use regex::Regex; use crate::tokens::Token; @@ -482,16 +482,15 @@ fn infix_operator_and(values: &[String]) -> String { fn operator_match(values: &[String]) -> Result { assert!(values.len() == 2); - let re = Regex::with_options(&values[1], RegexOptions::REGEX_OPTION_NONE, Syntax::grep()) - .map_err(|err| err.description().to_string())?; - Ok(if re.captures_len() > 0 { + let re = Regex::new(&values[1]).map_err(|err| err.to_string())?; + Ok(if re.captures_len() > 1 { re.captures(&values[0]) - .map(|captures| captures.at(1).unwrap()) + .map(|captures| captures.get(1).unwrap().as_str()) .unwrap_or("") .to_string() } else { re.find(&values[0]) - .map_or("0".to_string(), |(start, end)| (end - start).to_string()) + .map_or("0".to_string(), |m| m.len().to_string()) }) } diff --git a/tests/by-util/test_expr.rs b/tests/by-util/test_expr.rs index 28cfcf0ec90..767e184aebf 100644 --- a/tests/by-util/test_expr.rs +++ b/tests/by-util/test_expr.rs @@ -245,26 +245,52 @@ fn test_length_mb() { } #[test] -fn test_regex() { - // FixME: [2022-12-19; rivy] test disabled as it currently fails due to 'oniguruma' bug (see GH:kkos/oniguruma/issues/279) - // new_ucmd!() - // .args(&["a^b", ":", "a^b"]) - // .succeeds() - // .stdout_only("3\n"); +#[ignore = "requires custom regex syntax"] +fn test_regex_hat() { + new_ucmd!() + .args(&["a^b", ":", "a^b"]) + .succeeds() + .stdout_only("3\n"); +} + +#[test] +#[ignore = "requires custom regex syntax"] +fn test_regex_hat2() { new_ucmd!() .args(&["a^b", ":", "a\\^b"]) .succeeds() .stdout_only("3\n"); +} + +#[test] +#[ignore = "requires custom regex syntax"] +fn test_regex_dollar() { new_ucmd!() .args(&["a$b", ":", "a\\$b"]) .succeeds() .stdout_only("3\n"); +} + +#[test] +#[ignore = "requires custom regex syntax"] +fn test_regex_number() { new_ucmd!() .args(&["-5", ":", "-\\{0,1\\}[0-9]*$"]) .succeeds() .stdout_only("2\n"); } +#[test] +fn test_regex_temporary() { + // This test should fail, but currently passes, because our regex syntax is + // different from GNU. This is only here to check that regex is working + // even if it's the wrong syntax. + new_ucmd!() + .args(&["-5", ":", "-{0,1}[0-9]*$"]) + .succeeds() + .stdout_only("2\n"); +} + #[test] fn test_substr() { new_ucmd!()