printer: preserve line terminator when using `--crlf` and `--replace`
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Ref #3097, Closes #3100
authored by Isaac · committed by Andrew Gallant · parent f596a5d · Browse files · 3 files·+59 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+59 added−7 removed
- other:+59 added−7 removed
Provenance
- Commit
64174b8- Tree
7304d0a- Author
Isaac <IsaacOscar@live.com.au>- Committer
Andrew Gallant <jamslam@gmail.com>- Parent
f596a5d
3 files changed+59 added−7 removed
modified:crates/printer/src/standard.rs+37 added
3945 unmodified lines3946 let got = printer_contents(&mut printer);3947 let expected = "4:d\n5-e\n6:d\n";3948 assert_eq_printed!(expected, got);3949 }3950+3951+ #[test]3952+ fn regression_crlf_preserve() {3953+ let haystack = "hello\nworld\r\n";3954+ let matcher =3955+ RegexMatcherBuilder::new().crlf(true).build(r".").unwrap();3956+ let mut printer = StandardBuilder::new().build(NoColor::new(vec![]));3957+ let mut searcher = SearcherBuilder::new()3958+ .line_number(false)3959+ .line_terminator(LineTerminator::crlf())3960+ .build();3961+3962+ searcher3963+ .search_reader(3964+ &matcher,3965+ haystack.as_bytes(),3966+ printer.sink(&matcher),3967+ )3968+ .unwrap();3969+ let got = printer_contents(&mut printer);3970+ let expected = "hello\nworld\r\n";3971+ assert_eq_printed!(expected, got);3972+3973+ let mut printer = StandardBuilder::new()3974+ .replacement(Some(b"$0".to_vec()))3975+ .build(NoColor::new(vec![]));3976+ searcher3977+ .search_reader(3978+ &matcher,3979+ haystack.as_bytes(),3980+ printer.sink(&matcher),3981+ )3982+ .unwrap();3983+ let got = printer_contents(&mut printer);3984+ let expected = "hello\nworld\r\n";3985+ assert_eq_printed!(expected, got);3986+ }3987 }modified:crates/printer/src/util.rs+20 added-7 removed
57 unmodified lines58 ) -> io::Result<()> {59 // See the giant comment in 'find_iter_at_in_context' below for why we60 // do this dance.61 let is_multi_line = searcher.multi_line_with_matcher(&matcher);62- if is_multi_line {62+ // Get the line_terminator that was removed (if any) so we can add it back63+ let line_terminator = if is_multi_line {64 if haystack[range.end..].len() >= MAX_LOOK_AHEAD {65 haystack = &haystack[..range.end + MAX_LOOK_AHEAD];66 }67+ &[]68 } else {69 // When searching a single line, we should remove the line70 // terminator. Otherwise, it's possible for the regex (via71 // look-around) to observe the line terminator and not match72 // because of it.73 let mut m = Match::new(0, range.end);74+ let line_terminator =72- trim_line_terminator(searcher, haystack, &mut m);75+ trim_line_terminator(searcher, haystack, &mut m);76 haystack = &haystack[..m.end()];74- }77+ line_terminator78+ };79 {80 let &mut Space { ref mut dst, ref mut caps, ref mut matches } =81 self.allocate(matcher)?;82 dst.clear();1 unmodified line84 85 replace_with_captures_in_context(86 matcher,87 haystack,88+ line_terminator,89 range.clone(),90 caps,91 dst,92 |caps, dst| {419 unmodified lines512 // When searching a single line, we should remove the line terminator.513 // Otherwise, it's possible for the regex (via look-around) to observe514 // the line terminator and not match because of it.515 let mut m = Match::new(0, range.end);516+ // No need to rember the line terminator as we aren't doing a replace here517 trim_line_terminator(searcher, bytes, &mut m);518 bytes = &bytes[..m.end()];519 }520 matcher7 unmodified lines528 }529 530 /// Given a buf and some bounds, if there is a line terminator at the end of531 /// the given bounds in buf, then the bounds are trimmed to remove the line526-/// terminator.527-pub(crate) fn trim_line_terminator(532+/// terminator, returning the slice of the removed line terminator (if any).533+pub(crate) fn trim_line_terminator<'b>(534 searcher: &Searcher,529- buf: &[u8],535+ buf: &'b [u8],536 line: &mut Match,531-) {537+) -> &'b [u8] {538 let lineterm = searcher.line_terminator();539 if lineterm.is_suffix(&buf[*line]) {540 let mut end = line.end() - 1;541 if lineterm.is_crlf() && end > 0 && buf.get(end - 1) == Some(&b'\r') {542 end -= 1;543 }544+ let orig_end = line.end();545 *line = line.with_end(end);546+ &buf[end..orig_end]547+ } else {548+ &[]549 }550 }551 552 /// Like `Matcher::replace_with_captures_at`, but accepts an end bound.1 unmodified line554 /// See also: `find_iter_at_in_context` for why we need this.555 fn replace_with_captures_in_context<M, F>(556 matcher: M,557 bytes: &[u8],558+ line_terminator: &[u8],559 range: std::ops::Range<usize>,560 caps: &mut M::Captures,561 dst: &mut Vec<u8>,562 mut append: F,13 unmodified lines576 append(caps, dst)577 })?;578 let end = std::cmp::min(bytes.len(), range.end);579 dst.extend(&bytes[last_match..end]);580+ // Add back any line terminator581+ dst.extend(line_terminator);582 Ok(())583 }584 585 #[cfg(test)]17 unmodified linesmodified:CHANGELOG.md+2 added
25 unmodified lines26 * [BUG #2990](https://github.com/BurntSushi/ripgrep/issues/2990):27 Fix a bug where ripgrep would mishandle globs that ended with a `.`.28 * [BUG #3076](https://github.com/BurntSushi/ripgrep/issues/3076):29 Fix bug with `-m/--max-count` and `-U/--multiline` showing too many matches.30+* [BUG #3100](https://github.com/BurntSushi/ripgrep/pull/3100):31+ Preserve line terminators when using `-r/--replace` flag.32 * [BUG #3108](https://github.com/BurntSushi/ripgrep/issues/3108):33 Fix a bug where `-q --files-without-match` inverted the exit code.34 * [BUG #3140](https://github.com/BurntSushi/ripgrep/issues/3140):35 Ensure hyphens in flag names are escaped in the roff text for the man page.1737 unmodified lines