Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reiteration of the first found match with --only-mathing flag #454

Merged
merged 1 commit into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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