Skip to content

Commit

Permalink
Fix reiteration of the first found match with --only-mathing flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp authored and BurntSushi committed Apr 21, 2017
1 parent c50b8b4 commit 362abed
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,23 +242,13 @@ impl<W: WriteColor> Printer<W> {
line_number: Option<u64>,
) {
if !self.line_per_match && !self.only_matching {
let column =
if self.column {
Some(re.find(&buf[start..end])
.map(|m| m.start()).unwrap_or(0) as u64)
} else {
None
};
let column = re.find(&buf[start..end])
.map(|m| m.start()).unwrap_or(0);
return self.write_match(
re, path, buf, start, end, line_number, column);
}
for m in re.find_iter(&buf[start..end]) {
let column =
if self.column {
Some(m.start() as u64)
} else {
None
};
let column = m.start();
self.write_match(
re, path.as_ref(), buf, start, end, line_number, column);
}
Expand All @@ -272,7 +262,7 @@ impl<W: WriteColor> Printer<W> {
start: usize,
end: usize,
line_number: Option<u64>,
column: Option<u64>,
column: usize,
) {
if self.heading && self.with_filename && !self.has_printed {
self.write_file_sep();
Expand All @@ -285,8 +275,8 @@ impl<W: WriteColor> Printer<W> {
if let Some(line_number) = line_number {
self.line_number(line_number, b':');
}
if let Some(c) = column {
self.column_number(c + 1, b':');
if self.column {
self.column_number(column as u64 + 1, b':');
}
if self.replace.is_some() {
let mut count = 0;
Expand All @@ -308,8 +298,9 @@ impl<W: WriteColor> Printer<W> {
}
} else {
let line_buf = if self.only_matching {
let m = re.find(&buf[start..end]).unwrap();
&buf[start + m.start()..start + m.end()]
let start_offset = start + column;
let m = re.find(&buf[start_offset..end]).unwrap();
&buf[start_offset + m.start()..start_offset + m.end()]
} else {
&buf[start..end]
};
Expand Down
45 changes: 45 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,51 @@ fn regression_391() {
assert_eq!(lines, "bar.py\n");
}

// See: https://github.com/BurntSushi/ripgrep/issues/451
#[test]
fn regression_451_only_matching_as_in_issue() {
let wd = WorkDir::new("regression_451_only_matching");
let path = "digits.txt";
wd.create(path, "1 2 3\n");

let mut cmd = wd.command();
cmd.arg("[0-9]+").arg(path).arg("--only-matching");
let lines: String = wd.stdout(&mut cmd);

let expected = "\
1
2
3
";

assert_eq!(lines, expected);
}

// See: https://github.com/BurntSushi/ripgrep/issues/451
#[test]
fn regression_451_only_matching() {
let wd = WorkDir::new("regression_451_only_matching");
let path = "digits.txt";
wd.create(path, "1 2 3\n123\n");

let mut cmd = wd.command();
cmd.arg("[0-9]").arg(path)
.arg("--only-matching")
.arg("--column");
let lines: String = wd.stdout(&mut cmd);

let expected = "\
1:1:1
1:3:2
1:5:3
2:1:1
2:2:2
2:3:3
";

assert_eq!(lines, expected);
}

#[test]
fn type_list() {
let wd = WorkDir::new("type_list");
Expand Down

0 comments on commit 362abed

Please sign in to comment.