Skip to content

rust2(grep): GNU grep 3.11 library port on winux-regex (#1488) - #1631

Closed
caomengxuan666 wants to merge 3 commits into
mainfrom
rust2/grep-impl
Closed

caomengxuan666 wants to merge 3 commits into
mainfrom
rust2/grep-impl

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Library landing for the grep big-ticket lane. Wire (bridge route + dispatcher + PCRE2 split + C++ body retirement) follows in a second commit series — this PR is the library + differential evidence.

Scope

  • 8-module port (mod/opts/pattern/scan/walk/bytes/text/tests); -F literal multi-pattern, -E/-G via winux-regex, -P stays on C++ PCRE2 (uses_perl exported for the wire), -X awk approximated as Extended (documented)
  • Exit-code contract, binary-file two-state reporting, recursive -r/-R symlink semantics, gnulib excluded_file_name include/exclude, full getopt_long grammar, glibc regerror wording

Evidence

  • Gate: 162 rows green (grep 142 / egrep 11 / fgrep 9; 14 rows added during review) — ledger rust2/target/gate/ledger.tsv
  • 28-case oracle probe set byte-identical (target/grep-review/)
  • Perf 64MiB -c literal: ~99-138 ms vs GNU grep 3.11 ~103 ms warm, C++ release ~370-554 ms

Known deviations (documented in mod.rs header)
--color=auto never colors (injected handles); -T stdin width; -i folds ASCII only (engine-level, tracked separately); no stdin rewinding.

Expected CI status: i18n-drift may report new keys until the wire commit lands tr! keys for the walk/regerror message face (2.0 checkpoint I18N prerequisite). Compile/test legs should be green.

caomengxuan666 and others added 3 commits November 14, 2026 12:51
Adds the `dfa` module (additive: one `pub mod` line in lib.rs, no change
to the parser/matcher/fastpath): the same parsed node tree the
interpreter runs on is compiled into a Thompson NFA and determinized
lazily per (state, byte class), answering "does this line match" for a
SET of regexes in one table lookup per byte — GNU grep's dfa.c/regexec
split (DFA selects lines, the interpreter only runs for back-references
and match spans).

Equivalence contract (tests/dfa_equiv.rs, 6000-round generated
differential + every matrix.tsv row, 150000 rounds clean locally):
LineMode::Contains == find_at_bytes(line, 0).is_some(), LineMode::Entire
== matches_entire_bytes(line). Literals/brackets reuse chars_equal /
class_matches (identical -i folding); the unanchored scan attempts
exactly the interpreter's scan_positions through a next-is-not-a-
continuation lookahead. `.` is exact on well-formed UTF-8, word
assertions on ASCII lines (input_requirement() tells the caller which
lines to route to the interpreter); back-references, oversized NFAs and
counted repeats over conditionally-empty bodies (where the interpreter's
empty-iteration rule reaches fewer positions than GNU, e.g.
`(^|\w){2} +` on "_ ") return None and stay on the interpreter.
find_line() scans a whole buffer of eol-terminated lines (eol = \n, or
\0 for grep -z) and returns the first matching line's bounds.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
)

Additive method on Regex (no change to existing entry points): the
deduplicated end positions of all matches anchored at a start, longest
first. grep -w needs it for GNU EGexecute's "try a shorter match at the
same place" walk (re_match with a shrinking limit), which the
best-end-only match_at_bytes cannot express. The longest end always
equals match_at_bytes; a tripped work budget is the conservative empty
answer. Tests: tests/match_ends.rs (expected end lists + every listed end
re-verified as an anchored full match of the prefix).

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Eight-module port (mod/opts/pattern/scan/walk/bytes/text/tests):
- -F literal multi-pattern via LitSet trie + LineDfa; packed-pair
  Finder for single case-sensitive literal patterns
- -E/-G carried by winux-regex Basic/Extended; -P stays on the C++
  PCRE2 path (bridge route lands with the wire commit, uses_perl
  already exported); -X awk approximated as Extended (documented)
- exit-code contract 0/1/2 with -q early-exit ordering; binary-file
  two-state reporting incl. NUL zap and UTF-8 encoding-error trigger
- recursive -r/-R symlink semantics + gnulib excluded_file_name
  last-match-wins include/exclude handling
- full getopt_long grammar incl. ambiguity disambiguation and
  POSIXLY_CORRECT reordering; glibc regerror wording mapping

Differential: 162 gate rows green (grep 142 / egrep 11 / fgrep 9,
14 rows added during review); 28-case oracle probe set byte-identical
(target/grep-review/). Perf on 64MiB -c literal: ~99-138 ms vs GNU
grep 3.11 ~103 ms warm; C++ release ~370-554 ms.

Documented deviations (mod.rs header): --color=auto never colors
(injected handles), -T stdin width, -i folds ASCII only (engine-level
gap, tracked separately), -X awk approximation, no stdin rewinding.

I18N prerequisite for the wire PR: regerror/walk messages need stable
tr! keys per the 2.0 continuation checkpoint.
@caomengxuan666

Copy link
Copy Markdown
Member Author

Superseded by the wire PR — the library commits ride inside it (rust2/grep-wire). Will be linked from there; this one stays as the library-landing record.

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.

1 participant