Skip to content

find, locate: replace onig with fancy-regex - #864

Open
wtcpython wants to merge 1 commit into
uutils:mainfrom
wtcpython:replace-onig-with-fancy-regex
Open

wtcpython wants to merge 1 commit into
uutils:mainfrom
wtcpython:replace-onig-with-fancy-regex

Conversation

@wtcpython

Copy link
Copy Markdown
Contributor

Closes #212

@wtcpython
wtcpython force-pushed the replace-onig-with-fancy-regex branch from 4becade to 571d3a2 Compare September 18, 2026 11:39
@codspeed

codspeed Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 57.88%

Warning

uutils has exceeded the Hobby plan limit of 5 active contributors. Upgrade your plan or apply to the open source program by December 8, 2026 to keep using CodSpeed.

2 warnings

⚠️ 15 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 7 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚡ 7 improved benchmarks
✅ 13 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ regex_path 47.5 ms 23.6 ms ×2
⚡ combined_expr 48.2 ms 28.5 ms +68.87%
⚡ iname_glob 33.5 ms 21.5 ms +55.76%
⚡ name_glob 33.6 ms 22 ms +52.62%
⚡ prune 12.1 ms 8 ms +51.52%
⚡ regex 18.7 ms 12.9 ms +44.62%
⚡ build_pruned 31.4 ms 22.7 ms +38.27%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing wtcpython:replace-onig-with-fancy-regex (a6ded96) with main (7a9df76)

Open in CodSpeed

@oech3

oech3 commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 36.61%

Oh no...

Comment thread Cargo.toml Outdated
fancy-regex = { version = "0.19.2", default-features = false, features = ["std", "unicode"] }
regex = "1.12"
uucore = { version = "0.10.0", features = ["entries", "fs", "fsext", "mode"] }
uucore = { git = "https://github.com/wtcpython/coreutils.git", branch = "uucore-regex", features = ["entries", "fs", "fsext", "mode", "regex"] }

This comment was marked as outdated.

This comment was marked as outdated.

@wtcpython
wtcpython force-pushed the replace-onig-with-fancy-regex branch 2 times, most recently from 70d7f30 to 671b7ca Compare September 23, 2026 01:01
@github-actions

Copy link
Copy Markdown

Commit 70d7f30 has test result changes:

bfs testsuite:

Test results comparison:
  Current:   TOTAL: 316 / PASSED: 273 / FAILED: 37 / SKIPPED: 6
  Reference: TOTAL: 317 / PASSED: 274 / FAILED: 37 / SKIPPED: 6

Changes from main branch:
  TOTAL: -1
  PASSED: -1
  FAILED: +0

No result in this run (1) - hung, crashed, or renamed:
  ? posix/depth (was PASS)

@wtcpython
wtcpython marked this pull request as ready for review September 23, 2026 01:09
Copilot AI lite review requested due to automatic review settings September 23, 2026 01:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@sylvestre

Copy link
Copy Markdown
Contributor

the ci is red :)

Comment thread src/find/matchers/regex.rs Outdated
RegexType::PosixBasic => Syntax::posix_basic(),
RegexType::PosixExtended => Syntax::posix_extended(),
// GNU find's -regex does full-path matching, so anchor the pattern.
let anchored = if pattern.starts_with('^') && pattern.ends_with('$') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anchoring by concatenation breaks alternation: a|b becomes ^a|b$, which means ^a or b$. please wrap it instead, something like ^(?:{pattern})$

Comment thread src/find/matchers/regex.rs Outdated
} else if pattern.ends_with('$') {
format!("^{pattern}")
} else {
format!("^{pattern}$")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this changes -regex/-name matching semantics, please add tests in tests/test_find.rs (alternation, ^/$ in the middle, -iregex, each -regextype)

Comment thread src/find/matchers/regex.rs Outdated
fn matches(&self, file_info: &WalkEntry, _: &mut MatcherIO) -> bool {
self.regex
.is_match(file_info.path().to_string_lossy().as_ref())
.unwrap_or(false)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap_or(false) silently turns a backtrack-limit error into "no match", is that what we want?

Comment thread src/find/matchers/regex_transpile.rs Outdated
}

match chars.next() {
Some(c) if "(){}|+?".contains(c) => output.push(c),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in emacs syntax a bare | is a literal and only \| is alternation, so | needs to be escaped in the other branch. the doc comment above says the opposite

Comment thread src/find/matchers/glob.rs Outdated
// so anchor the regex to prevent partial matches.
let anchored = format!("^{r}$");
regex_transpile::compile(&anchored, super::regex::RegexType::PosixBasic, caseless)
.unwrap()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap() on a user-supplied pattern, could you please propagate the error? the "should never fail" comment was true for parse_bre, not for the new transpiler

@wtcpython
wtcpython force-pushed the replace-onig-with-fancy-regex branch from 671b7ca to 097a4d1 Compare September 23, 2026 14:02
Copilot AI review requested due to automatic review settings September 23, 2026 14:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@wtcpython
wtcpython force-pushed the replace-onig-with-fancy-regex branch from 097a4d1 to 6e93bd7 Compare September 25, 2026 13:27
Copilot AI review requested due to automatic review settings September 25, 2026 13:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.41624% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.42%. Comparing base (b6e66fd) to head (d3c0604).

Files with missing lines Patch % Lines
src/find/matchers/bre_to_ere.rs 92.76% 10 Missing and 17 partials ⚠️
src/find/matchers/glob.rs 92.10% 3 Missing ⚠️
src/find/matchers/regex_transpile.rs 98.00% 1 Missing and 1 partial ⚠️
src/find/matchers/mod.rs 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #864      +/-   ##
==========================================
+ Coverage   92.29%   92.42%   +0.12%     
==========================================
  Files          38       40       +2     
  Lines        8714     9223     +509     
  Branches      429      480      +51     
==========================================
+ Hits         8043     8524     +481     
- Misses        532      543      +11     
- Partials      139      156      +17     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI lite review requested due to automatic review settings October 10, 2026 14:40
@wtcpython
wtcpython force-pushed the replace-onig-with-fancy-regex branch from 6e93bd7 to 0695864 Compare October 10, 2026 14:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread src/find/matchers/bre_to_ere.rs Outdated
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

//! Unified regular expression infrastructure for uutils.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this module doc does not match this crate, please make it shorter

Comment thread Cargo.toml Outdated
@@ -23,7 +23,7 @@ chrono = "0.4.45"
clap = { version = "4.6", features = ["env"] }
faccess = "0.2.4"
nix = { version = "0.31", features = ["fs", "user"] }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codspeed shows about 30% slower, which is way above what we accept.
do you know where it comes from? leftmost_longest?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N.B. fancy-regex 0.20.0 was just released which contains a performance improvement for leftmost longest among a few others, in case it helps

Comment thread src/find/matchers/glob.rs Outdated
}

if parse_bre(&expr, RegexOptions::REGEX_OPTION_NONE).is_ok() {
if bre_to_ere(&expr, false).is_ok_and(|re| Regex::new(&re).is_ok()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not regex_transpile::compile(&expr, RegexType::PosixBasic, false).is_ok()? here it is built without the same builder options

Comment thread src/find/matchers/glob.rs Outdated
self.regex.as_ref().is_some_and(|r| r.is_match(string))
self.regex
.as_ref()
.is_some_and(|r| r.is_match(string).unwrap_or(false))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as for -regex: the error is silently turned into "no match" here

Comment thread src/find/matchers/regex.rs Outdated
let anchored = match regex_type {
RegexType::PosixExtended => format!("^(?:{pattern})$"),
RegexType::Emacs | RegexType::PosixBasic | RegexType::Grep => {
format!(r"^\({pattern}\)$")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this adds a capture group, so backrefs are shifted: \(a\)\1 becomes ^\(\(a\)\1\)$ and \1 now points to the outer group.
could you please anchor after transpiling instead, with ^(?:...)$ in compile()? glob.rs could use it too.
and please add a test with a backref

@wtcpython
wtcpython force-pushed the replace-onig-with-fancy-regex branch from 0695864 to add13e3 Compare October 24, 2026 11:18
Copilot AI lite review requested due to automatic review settings October 24, 2026 11:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@wtcpython
wtcpython force-pushed the replace-onig-with-fancy-regex branch from add13e3 to c67cfd3 Compare October 24, 2026 11:31
Copilot AI lite review requested due to automatic review settings October 24, 2026 11:31

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@github-actions

Copy link
Copy Markdown

Commit c67cfd3 has test result changes:

bfs testsuite:

Test results comparison:
  Current:   TOTAL: 316 / PASSED: 276 / FAILED: 34 / SKIPPED: 6
  Reference: TOTAL: 316 / PASSED: 276 / FAILED: 34 / SKIPPED: 6

No result in this run (1) - hung, crashed, or renamed:
  ? posix/o (was PASS)

@sylvestre

Copy link
Copy Markdown
Contributor

hi, this one can't be merged as it is:

  • it needs a rebase
  • the 2 comments above are still open
    could you please have a look? otherwise, i will close it, thanks

@oech3

This comment was marked as resolved.

@wtcpython
wtcpython force-pushed the replace-onig-with-fancy-regex branch from 4647195 to e5f8fe1 Compare November 10, 2026 01:03
@sylvestre

Copy link
Copy Markdown
Contributor

thanks for tackling this!
still a few things open: tests in tests/test_find.rs for -iregex, emacs and posix-extended, and glob matches() still returns "no match" on an error without setting the exit code.
the last bfs run lost posix/o (hung?), could you please check it on the current head?

@wtcpython
wtcpython force-pushed the replace-onig-with-fancy-regex branch from e5f8fe1 to 675538c Compare November 13, 2026 11:23
@wtcpython
wtcpython force-pushed the replace-onig-with-fancy-regex branch from 675538c to d3c0604 Compare November 24, 2026 13:15
Comment thread src/find/matchers/bre_to_ere.rs Outdated
///
/// If `anchored` is `true`, the pattern is implicitly anchored at the start (`^`), as required by POSIX `expr`.
/// Otherwise, standard unanchored BRE transpilation is performed (e.g. for `grep` and `sed`).
pub fn bre_to_ere(pattern_str: &str, anchored: bool) -> Result<String, RegexError> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anchored is always false in this crate, do we need it?
the doc here (and on check_posix_regex_errors) still talks about expr, grep, sed and GNU coreutils

Comment thread src/find/matchers/regex.rs Outdated
{
Ok(matched) => matched,
Err(e) => {
let _ = writeln!(&mut stderr(), "find: {e}");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use program_name() instead of a hardcoded find:, same in glob.rs

} else if "(){}|".contains(ch) {
output.push('\\');
}
output.push(ch);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens with -regex '*foo' in emacs syntax (the default)? bre_to_ere escapes a leading *, this doesn't
same question for emacs \`` and '`
please add a test

@wtcpython
wtcpython force-pushed the replace-onig-with-fancy-regex branch from d3c0604 to a6ded96 Compare November 25, 2026 05:22
@github-actions

Copy link
Copy Markdown

Commit a6ded96 has test result changes:

bfs testsuite:

Test results comparison:
  Current:   TOTAL: 317 / PASSED: 285 / FAILED: 26 / SKIPPED: 6
  Reference: TOTAL: 317 / PASSED: 286 / FAILED: 25 / SKIPPED: 6

Changes from main branch:
  TOTAL: +0
  PASSED: -1
  FAILED: +1

New test failures (1):
  - gnu/regextype_emacs

@sylvestre

Copy link
Copy Markdown
Contributor

i guess you saw - gnu/regextype_emacs

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for picking this up!
CI is red on every job and bfs gnu/regextype_emacs now fails. The bracket handling inline might be the cause.
Could you please check the CI logs on the current head?


while let Some(ch) = chars.next() {
if in_bracket {
if ch == ']' && !output.ends_with(['[', '^']) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in emacs syntax a backslash is literal inside brackets, so [\] should match a \. here it goes through as is and fancy-regex reads it as an escape (unclosed class)
also ends_with(['[', '^']) looks at the output, so [a^] or [[] never close the bracket and the rest is left untranspiled
please add tests, could it be what breaks gnu/regextype_emacs?

prev_is_escaped = false;
continue;
}
if curr == ']' && !curr_is_escaped && re_string != "[" && re_string != "[^" {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this keeps the backslash as an escape inside brackets. glob needs that but -regex does not: -regextype posix-basic -regex '.*[\]' used to match a trailing \ and now fails to compile.
onig only enabled it for globs (SYNTAX_BEHAVIOR_BACKSLASH_ESCAPE_IN_CC), could you please make it an option of compile()?

let transpiled = match regex_type {
RegexType::PosixBasic | RegexType::Grep => bre_to_ere(pattern)?,
RegexType::Emacs => transpile_emacs(pattern),
RegexType::PosixExtended => pattern.to_owned(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

posix-extended is passed as is, so it has the same [\] problem, and fancy-regex extensions like (?=...) are accepted too. is that ok?

Comment thread src/locate/mod.rs
patterns.iter().any(|s| entry.contains(s.as_str()))
}
Self::Regex(v) => v.iter().any(|r| r.find(entry).is_some()),
Self::Regex(v) => v.iter().any(|r| r.is_match(entry).unwrap_or(false)),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as for find: a matching error silently becomes "no match" here (and in all() below)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

consider making onig optional

5 participants