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

Code before:

    fn write_heading<P: AsRef<Path>>(&mut self, path: P) {
        let _ = self.wtr.set_color(self.colors.path());
        self.write_path(path.as_ref());
        let _ = self.wtr.reset();
        if self.null {
            self.write(b"\x00");
        } else {
            self.write_eol();
        }
    }

    fn write_non_heading_path<P: AsRef<Path>>(&mut self, path: P) {
        let _ = self.wtr.set_color(self.colors.path());
        self.write_path(path.as_ref());
        let _ = self.wtr.reset();
        if self.null {
            self.write(b"\x00");
        } else {
            self.write(b":");
        }
    }

    fn line_number(&mut self, n: u64, sep: u8) {
        let _ = self.wtr.set_color(self.colors.line());
        self.write(n.to_string().as_bytes());
        let _ = self.wtr.reset();
        self.write(&[sep]);
    }

    #[cfg(unix)]
    fn write_path<P: AsRef<Path>>(&mut self, path: P) {
        use std::os::unix::ffi::OsStrExt;

        let path = path.as_ref().as_os_str().as_bytes();
        match self.path_separator {
            None => self.write(path),
            Some(sep) => self.write_path_with_sep(path, sep),
        }
    }

    #[cfg(not(unix))]
    fn write_path<P: AsRef<Path>>(&mut self, path: P) {
        let path = path.as_ref().to_string_lossy();
        match self.path_separator {
            None => self.write(path.as_bytes()),
            Some(sep) => self.write_path_with_sep(path.as_bytes(), sep),
        }
    }

    fn write_path_with_sep(&mut self, path: &[u8], sep: u8) {

Review comment (refactor):
This introduces an additional allocation that wasn't there before, probably as a result of refactoring `path_to_str_native` to return a slice of bytes. Maybe it should return a cow instead?

Code after:
    }

    #[cfg(unix)]
    fn write_path<P: AsRef<Path>>(&mut self, path: P) {
        use std::os::unix::ffi::OsStrExt;
        let path = path.as_ref().as_os_str().as_bytes();
        self.write_path_replace_separator(path);
    }

    #[cfg(not(unix))]
    fn write_path<P: AsRef<Path>>(&mut self, path: P) {
        let path = path.as_ref().to_string_lossy();
        self.write_path_replace_separator(path.as_bytes());
    }

    fn write_path_replace_separator(&mut self, path: &[u8]) {
        match self.path_separator {
            None => self.write_colored(path, |colors| colors.path()),
            Some(sep) => {
                let transformed_path: Vec<_> = path.iter().map(|&b| {
                    if b == b'/' || (cfg!(windows) && b == b'\\') {
                        sep
                    } else {
                        b
                    }
                }).collect();
                self.write_colored(&transformed_path, |colors| colors.path());
            }
        }
    }

    fn line_number(&mut self, n: u64, sep: u8) {
        self.write_colored(n.to_string().as_bytes(), |colors| colors.line());
        self.separator(&[sep]);
    }

    fn column_number(&mut self, n: u64, sep: u8) {
        self.write(n.to_string().as_bytes());
        self.separator(&[sep]);
    }

    fn write(&mut self, buf: &[u8]) {
        self.has_printed = true;
        let _ = self.wtr.write_all(buf);
    }

    fn write_eol(&mut self) {
        let eol = self.eol;
        self.write(&[eol]);
    }