Skip to content

interpreter: match a bare regex against $0 - #139

Merged
Alonely0 merged 1 commit into
uutils:mainfrom
jayhemnani9910:bare-regex-matches-record
Oct 23, 2026
Merged

Alonely0 merged 1 commit into
uutils:mainfrom
jayhemnani9910:bare-regex-matches-record

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

A regex on its own, as a pattern or in an expression, never matches. It is lowered as 0 ~ /re/ (the number 0) instead of $0 ~ /re/. gawk 5.3.2 vs main:

$ printf 'a b\nc\nab\n' | awk '/b/'
gawk: a b
      ab
main: (nothing)

$ printf 'a b\nc\nab\n' | awk '!/a/'
gawk: c
main: a b
      c
      ab

awk '/re/' is the most common awk program, and if (/re/), n += /re/ and /a/ || /b/ fail the same way. $0 ~ /re/ already works.

The lowering of Atom::Regex now loads field 0 into the register before the Matches, the way $0 itself is loaded.

New tests bare_regex_pattern_matches_record and bare_regex_in_expression_matches_record fail without the change. cargo test --workspace: 210 passed, clippy and fmt clean. Eleven pattern forms (/x/, !/a/, /a/ || /x/, /^$/, /[[:digit:]]+/, if (/[0-9]/), x = /a/, n += /a/, ...) print the same as gawk.

Copilot AI balanced review requested due to automatic review settings October 22, 2026 05:20

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.

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

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 100 / FAILED: 468 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 97 / FAILED: 471 / SKIPPED: 98

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

New test failures (3):
  - litoct
  - noloop1
  - noloop2

Test improvements (6):
  + datanonl
  + dfacheck1
  + dfacheck2
  + escapebrace
  + gensub3
  + reint2

@Alonely0 Alonely0 self-assigned this Oct 22, 2026
@jayhemnani9910

Copy link
Copy Markdown
Contributor Author

The three new failures come from the bare regex now being matched against $0, not from a regression:

  • noloop1 and noloop2 run /''/ { sub(...) }. Before this change the pattern never matched, so sub never ran and the empty expected output passed. Now the pattern matches and hits the todo!() for sub.
  • litoct runs /a\52b/ on ab with --traditional. gawk treats octal escapes in a regex constant literally in traditional mode, so it prints "no match"; with --posix or no flag it prints "match". Here --traditional maps to the same mode as --posix, so it now matches. Before, it passed only because the regex was never tested against the record.

I can take either of these in a follow-up if you want.

@sylvestre

Copy link
Copy Markdown
Contributor

a bunch of jobs are failing

@Alonely0

Copy link
Copy Markdown
Collaborator

The three new failures come from the bare regex now being matched against $0, not from a regression:

* `noloop1` and `noloop2` run `/''/ { sub(...) }`. Before this change the pattern never matched, so `sub` never ran and the empty expected output passed. Now the pattern matches and hits the `todo!()` for `sub`.

* `litoct` runs `/a\52b/` on `ab` with `--traditional`. gawk treats octal escapes in a regex constant literally in traditional mode, so it prints "no match"; with `--posix` or no flag it prints "match". Here `--traditional` maps to the same mode as `--posix`, so it now matches. Before, it passed only because the regex was never tested against the record.

I can take either of these in a follow-up if you want.

That would be nice, thanks! Let me take a look at the two PRs tomorrow, I've had a long day.

@Alonely0 Alonely0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice! This must have slipped through one of the thousand refactorings of the IR emitter. We could do this a single instruction in the near future, given how common of a pattern this is.

@Alonely0
Alonely0 merged commit 1195b2f into uutils:main Oct 23, 2026
11 of 15 checks passed
@Alonely0

Copy link
Copy Markdown
Collaborator

Merged, thanks for your contribution!

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.

4 participants