fold: add -p/--punctuation flag for tiered break-point folding#13327
fold: add -p/--punctuation flag for tiered break-point folding#13327abdorah wants to merge 1 commit into
Conversation
Merging this PR will not alter performance
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ❌ | Simulation | shuf_lines[100000] |
27.4 ms | 32.2 ms | -14.89% |
| ❌ | Simulation | fold_many_lines[100000] |
57.3 ms | 63.6 ms | -9.85% |
| ❌ | Simulation | shuf_repeat_sampling[50000] |
4.5 ms | 5 ms | -9.83% |
| ❌ | Simulation | fold_custom_width[50000] |
22.9 ms | 25.2 ms | -9.09% |
| ❌ | Memory | fold_many_lines[100000] |
26.7 KB | 27.6 KB | -3.38% |
| ⚡ | Simulation | false_consecutive_calls |
199.4 ns | 170.3 ns | +17.13% |
| ⚡ | Simulation | true_consecutive_calls |
199.4 ns | 170.3 ns | +17.13% |
| ⚡ | Simulation | unexpand_large_file[10] |
291.9 ms | 282.3 ms | +3.41% |
| ⚡ | Simulation | unexpand_many_lines[100000] |
139.3 ms | 134.7 ms | +3.4% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing abdorah:main (e1eece4) with main (b42827c)
Footnotes
-
46 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
| .arg( | ||
| Arg::new(options::PUNCTUATION) | ||
| .long(options::PUNCTUATION) | ||
| .short('p') |
There was a problem hiding this comment.
| .short('p') |
Please don't add short option which potentially conflicts with GNU's new options.
As I said in an earlier comment in the issue 13316, I've implemented -p / --punctuation as a standalone flag (no argument). When the line hits the width limit, it searches backwards through
the buffered content for the best break point using this priority: sentence-ending punctuation (. ! ? :) -> clause punctuation (, ;) -> whitespace -> any non-alphanumeric character -> hard cut at width.
The rightmost match of the highest-priority tier wins, which maximizes line usage while still preferring semantically meaningful breaks. This is mutually exclusive with -s (enforced by clap). The tiered fallback means a single -p invocation replaces what would otherwise require piping through fold four times with different break characters.
The character sets are hardcoded to ASCII for now but the structure makes it straightforward to extend with locale-aware or CJK punctuation (e.g., 。 、 ?) in a follow-up if needed.
This has changed many things in the way that fold used to work. Since the only attribute that requires 'back tracking' of spaces is space itself, we only had a true or false switch for that, which is perfectly fine, but for an intelligent fold like the one suggested by Dmole, we need more than that, hence the big changes to the fold context.