Skip to content

Commit e7829c0

Browse files
committed
cli: fix bug where last byte was stripped
In an effort to strip line terminators, we assumed their existence. But a pattern file may not end with a line terminator, so we shouldn't unconditionally strip them. We fix this by moving to bstr's line handling, which does this for us automatically.
1 parent a622293 commit e7829c0

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
TBD
2+
===
3+
TODO.
4+
5+
Bug fixes:
6+
7+
* [BUG #1259](https://github.com/BurntSushi/ripgrep/issues/1259):
8+
Fix bug where the last byte of a `-f file` was stripped if it wasn't a `\n`.
9+
10+
111
11.0.1 (2019-04-16)
212
===================
313
ripgrep 11.0.1 is a new patch release that fixes a search regression introduced

grep-cli/src/pattern.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ use std::error;
22
use std::ffi::OsStr;
33
use std::fmt;
44
use std::fs::File;
5-
use std::io::{self, BufRead};
5+
use std::io;
66
use std::path::Path;
77
use std::str;
88

9+
use bstr::io::BufReadExt;
10+
911
use escape::{escape, escape_os};
1012

1113
/// An error that occurs when a pattern could not be converted to valid UTF-8.
@@ -156,28 +158,22 @@ pub fn patterns_from_stdin() -> io::Result<Vec<String>> {
156158
/// ```
157159
pub fn patterns_from_reader<R: io::Read>(rdr: R) -> io::Result<Vec<String>> {
158160
let mut patterns = vec![];
159-
let mut bufrdr = io::BufReader::new(rdr);
160-
let mut line = vec![];
161161
let mut line_number = 0;
162-
while {
163-
line.clear();
162+
io::BufReader::new(rdr).for_byte_line(|line| {
164163
line_number += 1;
165-
bufrdr.read_until(b'\n', &mut line)? > 0
166-
} {
167-
line.pop().unwrap(); // remove trailing '\n'
168-
if line.last() == Some(&b'\r') {
169-
line.pop().unwrap();
170-
}
171-
match pattern_from_bytes(&line) {
172-
Ok(pattern) => patterns.push(pattern.to_string()),
164+
match pattern_from_bytes(line.as_bytes()) {
165+
Ok(pattern) => {
166+
patterns.push(pattern.to_string());
167+
Ok(true)
168+
}
173169
Err(err) => {
174-
return Err(io::Error::new(
170+
Err(io::Error::new(
175171
io::ErrorKind::Other,
176172
format!("{}: {}", line_number, err),
177-
));
173+
))
178174
}
179175
}
180-
}
176+
})?;
181177
Ok(patterns)
182178
}
183179

tests/regression.rs

+11
Original file line numberDiff line numberDiff line change
@@ -705,3 +705,14 @@ rgtest!(r1203_reverse_suffix_literal, |dir: Dir, _: TestCommand| {
705705
let mut cmd = dir.command();
706706
eqnice!("153.230000\n", cmd.arg(r"\d\d\d000").arg("test").stdout());
707707
});
708+
709+
// See: https://github.com/BurntSushi/ripgrep/issues/1259
710+
rgtest!(r1259_drop_last_byte_nonl, |dir: Dir, mut cmd: TestCommand| {
711+
dir.create("patterns-nonl", "[foo]");
712+
dir.create("patterns-nl", "[foo]\n");
713+
dir.create("test", "fz");
714+
715+
eqnice!("fz\n", cmd.arg("-f").arg("patterns-nonl").arg("test").stdout());
716+
cmd = dir.command();
717+
eqnice!("fz\n", cmd.arg("-f").arg("patterns-nl").arg("test").stdout());
718+
});

0 commit comments

Comments
 (0)