Skip to content

grep: select zero-width matches under -w and -x - #52

Merged
sylvestre merged 1 commit into
uutils:mainfrom
wondr-wclabs:codex/empty-match-word-line
Jun 5, 2026
Merged

grep: select zero-width matches under -w and -x#52
sylvestre merged 1 commit into
uutils:mainfrom
wondr-wclabs:codex/empty-match-word-line

Conversation

@wondr-wclabs

Copy link
Copy Markdown
Contributor

Closes #18.

This fixes the way -w/-x handle zero-width matches. The previous implementation used the filtered list of printable/highlight spans as the source of truth for whether a line matched under -w or -x. That loses valid zero-width matches, because they deliberately have no span to print.

The patch separates those two concepts:

  • any_selected records whether a regex match survived -w/-x filtering.
  • The returned positions still omit zero-length spans, so output/highlighting does not gain empty ranges.

There is a second necessary piece: the cursor now allows one search at EOF (offset == line.len()) before stopping. Without that, an empty line cannot surface the zero-width $ match at all. The existing zero-length start + 1 advance moves the cursor past EOF afterward, so this does not introduce an infinite loop.

Validation run locally:

  • cargo fmt --all -- --check
  • cargo test regexp -- --nocapture
  • printf 'abc\n\nx\n' | cargo run --quiet -- -e '$' -w | od -An -tx1 -> 0a, exit 0
  • printf 'abc\n\nx\n' | cargo run --quiet -- -e '$' -x | od -An -tx1 -> 0a, exit 0
  • cargo test
  • cargo test --no-fail-fast
  • cargo clippy --all-targets --workspace -p uu_grep -- -D warnings
  • git diff --check

@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown

GNU grep testsuite comparison:

Test results comparison:
  Current:   TOTAL: 128 / PASSED: 71 / FAILED: 36 / SKIPPED: 21
  Reference: TOTAL: 128 / PASSED: 70 / FAILED: 37 / SKIPPED: 21

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

Test improvements (1):
  + empty

@codspeed-hq

codspeed-hq Bot commented Jun 5, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 10 untouched benchmarks
⏩ 17 skipped benchmarks1


Comparing wondr-wclabs:codex/empty-match-word-line (ad7595f) with main (f4798cb)

Open in CodSpeed

Footnotes

  1. 17 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@sylvestre

Copy link
Copy Markdown
Contributor

note: this isn't useful, please add to your prompt not do it anymore :)

Validation run locally:

    cargo fmt --all -- --check
    cargo test regexp -- --nocapture
    printf 'abc\n\nx\n' | cargo run --quiet -- -e '$' -w | od -An -tx1 -> 0a, exit 0
    printf 'abc\n\nx\n' | cargo run --quiet -- -e '$' -x | od -An -tx1 -> 0a, exit 0
    cargo test
    cargo test --no-fail-fast
    cargo clippy --all-targets --workspace -p uu_grep -- -D warnings
    git diff --check

@sylvestre
sylvestre merged commit 337b7c7 into uutils:main Jun 5, 2026
14 checks passed
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.

handles empty-matching patterns under -w/-x differently from GNU (empty line never selected)

2 participants