ack3 icon indicating copy to clipboard operation
ack3 copied to clipboard

Catch when regexp matches empty string

Open epa opened this issue 1 year ago • 4 comments

When the regexp passed to ack matches the empty string, this is much more likely to be a mistake than intentional. Rather than print the complete contents of every file, ack should stop with a diagnostic.

% ack ''
The regular expression matches the empty string, so would find all lines.

(If there's really a use for it, then maybe an --allow-empty flag similar to what you have in git to allow empty commits. And of course, having detected that the regexp matches the empty string, ack could go a bit faster by not bothering to match it.)

(For example I mistyped (joe|jim)? when I had meant to search for (joe|jim)@. Just to give a random example.)

epa avatar Oct 23 '24 13:10 epa

This makes a lot of sense. Let me ponder if we need an --allow-empty. I'm trying to imagine a case where you would want it. Maybe someone searches for .*whatever just so they get every line, even those that don't match whatever? Like they're emulating the --passthru option.

petdance avatar Oct 23 '24 15:10 petdance

Yes, I don't think --allow-empty would be needed in practice but I mentioned it because you sometimes get people who furiously defend the current behaviour, whatever it may be.

epa avatar Oct 23 '24 15:10 epa

I don't optimize for complaint avoidance. If people complain and there's a legit need we can add it.

petdance avatar Oct 23 '24 18:10 petdance

--allow-empty would be required not just for --passthru edge-cases, but for some edge uses of recent --not that are perhaps more interesting .? --not A... --not B... --not C... . (Which latter is equivalent to -g A... -or B... -or C ... but most people even programmers are not as comfortable with De Morgan's Law logic inversion as we'd like to think we are, so writing in whichever form is more legible is to be encouraged.)

n1vux avatar Dec 18 '24 19:12 n1vux