Skip to content

Fix -n LIMIT restarting on every line - #351

Open
VXNCXNX wants to merge 1 commit into
chmln:masterfrom
VXNCXNX:fix/limit-per-file
Open

Fix -n LIMIT restarting on every line#351
VXNCXNX wants to merge 1 commit into
chmln:masterfrom
VXNCXNX:fix/limit-per-file

Conversation

@VXNCXNX

@VXNCXNX VXNCXNX commented Aug 17, 2026

Copy link
Copy Markdown

-n LIMIT is documented as "Limit the number of replacements that can occur per file", but in the default line-by-line mode the limit restarts on every line, so there is no overall cap. Only -A/--across honours the documented behaviour. On a file of three a a lines:

$ sd -n 2 --preview a X file.txt      $ sd -A -n 2 --preview a X file.txt
X X                                   X X
X X                                   a a
X X   <- 6 replacements for -n 2      a a   <- 2, as documented

process_reader_line_by_line called replacer.replace once per line and each call restarted from self.replacements. replacen now takes a &mut budget it decrements, and the reader creates one budget per input instead of one per line. Replacer::replace keeps its signature, so the across path and the library API are unchanged.

-n 2 now matches -A -n 2 exactly and unlimited output is untouched. Two tests added in sd-cli/tests/cli.rs. Suite passes, clippy clean. cargo fmt --check flags one pre-existing diff in that file, not in code I touched, so I left it.

AI disclosure: written with Claude Code. I ran the binary before and after and checked the mutation myself.

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