Repository navigation
fix(grep): pad line-number/offset fields to GNU width under -T (Refs #1515) - #1539
Merged
Merged
Conversation
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
marked this pull request as ready for review
November 8, 2026 10:47
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #1515
概要
GNU grep 在
-T/--initial-tab下把行号与字节偏移字段填充到统一宽度(grep.cprint_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 跨度(与 GNUprint_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 验证。