Why don't patterns that match empty strings match empty files? #2091
-
In trying to use For example:
This is also how But why? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Sorry, I missed this question and it probably dipped too far into my email inbox and I ended up forgetting about it. This is a good question, although ambiguous. On the one hand, maybe you're interested in "why does the implementation behave this way?" Or maybe you're interested in, "why did you choose to implement these semantics?" I suspect you're interested in the latter. But I'll answer the former too. For the former, the answer is that when ripgrep goes to read a file, it has to know when to stop reading and thus stop searching. It knows this when For the latter question... Incidentally, I cannot remember ever explicitly thinking about what the behavior ought to be here. Interestingly, despite not considering it explicitly, ripgrep and grep do indeed behave the same. Looking at the POSIX specification for grep, I don't see anything that specifies the behavior for this particular case. So either they didn't consider it either, or they intentionally left it up to the implementation to decide. So trying to reason about this from first principles, it does kind of seem like you could spin this either way, and it primarily depends on how you interpret "empty file."
Incidentally, if you did choose interpretation (2), does that mean, for example, that So it seems to me like interpretation (1) is the most consistent with other tools. You could say that the empty file is really a special case, but then you'd have to reconcile it with things like |
Beta Was this translation helpful? Give feedback.
Sorry, I missed this question and it probably dipped too far into my email inbox and I ended up forgetting about it.
This is a good question, although ambiguous. On the one hand, maybe you're interested in "why does the implementation behave this way?" Or maybe you're interested in, "why did you choose to implement these semantics?" I suspect you're interested in the latter. But I'll answer the former too.
For the former, the answer is that when ripgrep goes to read a file, it has to know when to stop reading and thus stop searching. It knows this when
read
syscalls return 0 bytes, which conventionally indicates EOF. So in the current implementation, for an empty file, ripgrep issues aread
…