fold: do not fold a bytewise line that is exactly the width - #14189
Open
Socialpranker wants to merge 1 commit into
Open
fold: do not fold a bytewise line that is exactly the width#14189Socialpranker wants to merge 1 commit into
Socialpranker wants to merge 1 commit into
Conversation
In byte mode the newline that terminates a line of exactly `width` bytes sits one byte past the chunk being inspected, so it was never seen as a natural line end. With -s the line was then broken at its last blank, producing an early wrap and a trailing space. Include the lookahead byte in the newline search; the "next byte is a newline" guard on the fold path becomes unreachable and is dropped.
Contributor
|
Please avoid writing such longs comment 0. |
|
GNU testsuite comparison: |
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.
In byte mode (
-b), a line that is exactlywidthbytes long is wrappedanyway when
-sis given:fold_file_bytewisebufferswidth + 1bytes and looks for a natural lineend inside
line[..width]. For a line of exactlywidthbytes theterminating newline sits at index
width— one byte past that slice — so it isnever found, and the code falls through to the fold path. Without
-stheif line[end] != NLguard happens to cover it, becauseend == width; with-sthe break moves back to the last blank and the guard looks at the wrongbyte, so the line is split early and the blank is left dangling at the end of
the output line.
Extending the newline search by that one lookahead byte fixes it. The guard on
the fold path then becomes unreachable —
end <= width, and a newline inline[..=width]has just been ruled out — so it is dropped and the newline iswritten unconditionally.
Real-world instance,
/usr/share/doc/apt/copyrighton a Debian system, wherethis line is exactly 60 bytes:
Testing: three new tests in
tests/by-util/test_fold.rscover a whole lineof exactly the width, the remainder of a folded line landing on exactly the
width, and — as a control — a line one byte longer that must still fold. The
first two fail on current
mainand pass here; the third passes either way.The full
foldsuite is green (94 passed),cargo fmt --all --checkandcargo clippy -p uu_fold --all-targetsare clean.Release builds of
mainand of this branch were also run against the systemfoldinsidedebian:stable-slimover 60 files from/usr/share/docand/etc, with flags-b,-bs,-sand none, at widths 1, 2, 3, 5, 7, 8, 11,20, 40, 60 and 80 — 2640 invocations. 468 of them go from differing to
byte-identical, and none regress. Every remaining difference is in the
character/column path, which this PR does not touch; byte mode (
-b,-bs,1320 invocations) now matches GNU everywhere in that sweep.
Behaviour was established by observing GNU
fold's output on a Debian system —no GNU source was consulted.