summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2016-09-25 21:26:49 -0400
committerAndrew Gallant <jamslam@gmail.com>2016-09-25 21:26:49 -0400
commitb034b777984ab0eeeafd82119e75c60a80254b5d (patch)
tree26f011e574aba3f791018f67082f0f2a9de0de5c /src
parent278e1168bf11c62faff18f0857b151dcef65dee4 (diff)
Don't replace NUL bytes when searching binary files as text.
This was a result of misinterpreting a feature in grep where NUL bytes are replaced with \n. The primary reason for doing this is to avoid excessive memory usage on truly binary data. However, grep only does this when searching binary files as if they were binary, and which only reports whether the file matched or not. When grep is told to search binary data as text (the -a/--text flag), then it doesn't do any replacement so we shouldn't either. In general, this makes sense, because the user is essentially asserting that a particular file that looks like binary is actually text. In that case, we shouldn't try to replace any NUL bytes. ripgrep doesn't actually support searching binary data for whether it matches or not, so we don't actually need the replace_buf function. However, it does seem like a potentially useful feature.
Diffstat (limited to 'src')
-rw-r--r--src/search_stream.rs7
1 files changed, 2 insertions, 5 deletions
diff --git a/src/search_stream.rs b/src/search_stream.rs
index 0b5bfaf6..3407366e 100644
--- a/src/search_stream.rs
+++ b/src/search_stream.rs
@@ -532,10 +532,6 @@ impl InputBuffer {
if self.first && is_binary(&self.buf[self.end..self.end + n]) {
self.is_binary = true;
}
- if self.is_binary {
- replace_buf(
- &mut self.buf[self.end..self.end + n], b'\x00', self.eol);
- }
self.first = false;
// We assume that reading 0 bytes means we've hit EOF.
if n == 0 {
@@ -658,6 +654,7 @@ pub fn count_lines(buf: &[u8], eol: u8) -> u64 {
}
/// Replaces a with b in buf.
+#[allow(dead_code)]
fn replace_buf(buf: &mut [u8], a: u8, b: u8) {
if a == b {
return;
@@ -999,7 +996,7 @@ fn main() {
let text = "Sherlock\n\x00Holmes\n";
let (count, out) = search("Sherlock|Holmes", text, |s| s.text(true));
assert_eq!(2, count);
- assert_eq!(out, "/baz.rs:Sherlock\n/baz.rs:Holmes\n");
+ assert_eq!(out, "/baz.rs:Sherlock\n/baz.rs:\x00Holmes\n");
}
#[test]