Repository: BurntSushi/ripgrep
File: src/printer.rs (Rust)

Code before:
        } else {
            if self.only_matching {
                let buf = &buf[start + match_start..start + match_end];
                self.write_matched_line(re, buf, true);
            } else {
                self.write_matched_line(re, &buf[start..end], false);
            }
        }
    }

    fn write_matched_line(
        &mut self,
        re: &Regex,
        buf: &[u8],
        only_match: bool,
    ) {
        if self.max_columns.map_or(false, |m| buf.len() > m) {
            let count = re.find_iter(buf).count();
            let msg = format!("[Omitted long line with {} matches]", count);
            self.write_colored(msg.as_bytes(), |colors| colors.matched());
            self.write_eol();
            return;
        }
        if !self.wtr.supports_color() || self.colors.matched().is_none() {
            self.write(buf);
        } else if only_match {
            self.write_colored(buf, |colors| colors.matched());
        } else {
            let mut last_written = 0;
            for m in re.find_iter(buf) {
                self.write(&buf[last_written..m.start()]);
                self.write_colored(
                    &buf[m.start()..m.end()], |colors| colors.matched());
                last_written = m.end();
            }
            self.write(&buf[last_written..]);
        }
        if buf.last() != Some(&self.eol) {
            self.write_eol();
        }
    }

    pub fn context<P: AsRef<Path>>(
        &mut self,
        path: P,
        buf: &[u8],
        start: usize,
        end: usize,
        line_number: Option<u64>,
    ) {
        if self.heading && self.with_filename && !self.has_printed {

Review comment (suggestion):
I think `&line` should be sufficient here? I guess you might need `&*line` because `line` is a cow I think.

Code after:
            self.write_path_eol();
        } else if !self.heading && self.with_filename {
            self.write_path(path);
            self.write_path_sep(b':');
        }
        if let Some(line_number) = line_number {
            self.line_number(line_number, b':');
        }
        if self.column {
            self.column_number(match_start as u64 + 1, b':');
        }
        if self.replace.is_some() {
            let mut count = 0;
            let mut offsets = Vec::new();
            let line = {
                let replacer = CountingReplacer::new(
                    self.replace.as_ref().unwrap(), &mut count, &mut offsets);
                re.replace_all(&buf[start..end], replacer)
            };
            if self.max_columns.map_or(false, |m| line.len() > m) {
                let msg = format!(
                    "[Omitted long line with {} replacements]", count);
                self.write_colored(msg.as_bytes(), |colors| colors.matched());
                self.write_eol();
                return;
            }
            self.write_matched_line(offsets, &*line, false);
        } else {
            let buf = if self.only_matching {
                &buf[start + match_start..start + match_end]
            } else {
                &buf[start..end]
            };
            if self.max_columns.map_or(false, |m| buf.len() > m) {
                let count = re.find_iter(buf).count();
                let msg = format!("[Omitted long line with {} matches]", count);
                self.write_colored(msg.as_bytes(), |colors| colors.matched());
                self.write_eol();
                return;
            }
            let only_match = self.only_matching;
            self.write_matched_line(
                re.find_iter(buf).map(|x| Offset::from(&x)), buf, only_match);
        }
    }

    fn write_matched_line<I>(&mut self, offsets: I, buf: &[u8], only_match: bool)
        where I: IntoIterator<Item=Offset>,
    {
        if !self.wtr.supports_color() || self.colors.matched().is_none() {
            self.write(buf);