Skip to content

fix(grep): pad line-number/offset fields to GNU width under -T (Refs #1515) - #1539

Merged
caomengxuan666 merged 3 commits into
mainfrom
fix/WINX1515-wave1
Nov 8, 2026
Merged

caomengxuan666 merged 3 commits into
mainfrom
fix/WINX1515-wave1

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #1515

概要

GNU grep 在 -T/--initial-tab 下把行号与字节偏移字段填充到统一宽度(grep.c print_offset 以 offset_width 打 %*"PRIdMAX;宽度在每输入开扫时按 grep.c:1541-1550 由输入大小算出:st_size 的十进制位数、打印行号时因 origin-1 再 +1、大小不可用(管道/控制台/设备)时为 INTMAX_MAX 的 19 位)。WinuxCmd 的 grep 从不填充——这是 A5-G13 在 PR #1506 修掉 -T 制表位面之后剩下的填充面。本 PR 在 append_prefix 这一唯一的数字字段出口按 GNU 同一机制补齐:process() 每输入按 GNU 同式算 cfg.offset_width(不用 -T 时恒 0,行为不变),行号与字节偏移共用该宽度、空格右对齐补齐,彩色时整个填充字段同处一个 SGR 跨度(与 GNU print_offset 的 pr_sgr 包裹一致)。

本 PR 删除了什么

无删除。纯增量:src/commands/grep.cpp 新增 offset_width 配置字段、宽度计算与输入大小探测两个静态助手、append_prefix 的字段格式化助手,并重写行号/字节偏移两段的输出调用;新增回归钉文件 tests/unit/grep/grep_issue1515_unit_test.cpp。

回归钉

tests/unit/grep/grep_issue1515_unit_test.cpp 六格,期望字节全部取自 GNU grep 3.11 实跑:上下文行号填充、无 -T 对照、字节偏移共用宽度(含 size=999 的 +1 进位边界)、stdin 管道宽度 19、多文件逐文件重置宽度、-o 面。

UNVERIFIED-BY-CAPTAIN: 本地不编译,待本 PR 的 Windows CI 验证。

GNU grep 3.11 pads line numbers and byte offsets to a uniform width
under -T/--initial-tab (grep.c print_offset %*PRIdMAX with a per-input
offset_width). Six cells pin the GNU bytes: context padding, the
no--T control, the shared byte-offset width incl. the origin-1 +1
digit boundary at size 999, the stdin-pipe width 19, the per-file
width reset, and the -o face. All six fail on the current tree, which
never pads.
…efs #1515)

Port grep.c's offset_width mechanism (grep 3.11): print_offset
prints both the line number and the byte offset right-aligned to
offset_width (%*"PRIdMAX). process() now computes the width once
per input, exactly like grep.c:1541-1550: 0 without -T; otherwise
the decimal digits of the input size (digits of st_size, +1 when
line numbers are printed since they are origin-1), or 19 (digits
of INTMAX_MAX) when the input has no usable size -- a pipe or
console stdin (GetFileType != FILE_TYPE_DISK) or a non-regular
operand. append_prefix routes both fields through one formatter;
the padding spaces sit inside the field's color span, matching
pr_sgr_start_if/printf/pr_sgr_end_if.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 8, 2026 10:47
@caomengxuan666
caomengxuan666 merged commit b6ff7f1 into main Nov 8, 2026
16 of 26 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 9, 2026
The baseline job is red on this PR with exactly one gate error:
'new case: corpus/grep/07-initial-tab-width.case=PASS'. The case was
added to the corpus by #1539 (grep -T line-number/offset width, Refs
#1515) but its ledger entry was never recorded — the same error line
appears in #1539's own baseline job log on main, so main carries the
latent gap and any PR touching nothing at all would trip it.

Add the single entry (PASS, grep), verified three ways:
- compare-baseline.py against the CI run's own baseline-results.tsv
  artifact (oracle-anchored Ubuntu 24.04 / coreutils 9.4 shape):
  'baseline OK cases=645'.
- check-baseline-growth.py against the base revision's ledger:
  644->645 cases, 604->605 PASS (pure growth).
- Local full-corpus runner run reproduces PASS for the case; its other
  deltas vs the ledger are this machine's host-PATH shape (Niubash
  allowlisted oracles), not present in the CI artifact.

Refs #1488, #1539
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