diff options
author | Andrew Gallant <jamslam@gmail.com> | 2016-09-25 21:23:26 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2016-09-25 21:23:26 -0400 |
commit | 278e1168bf11c62faff18f0857b151dcef65dee4 (patch) | |
tree | fd197fca1ccb5fd0b1748c809d2847903633d00f /src | |
parent | 6a8051b258408343c4cf164acdc4cc2cd7928129 (diff) |
Make printing paths a bit faster.
It seems silly, but on *nix, we can just dump the bytes of the path
straight to the terminal. There's no need to do a UTF-8 check, which
can be costly when printing lots of matches.
Diffstat (limited to 'src')
-rw-r--r-- | src/printer.rs | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/src/printer.rs b/src/printer.rs index a690eaa0..07b6e31c 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -145,14 +145,14 @@ impl<W: Terminal + Send> Printer<W> { /// Prints the given path. pub fn path<P: AsRef<Path>>(&mut self, path: P) { let path = strip_prefix("./", path.as_ref()).unwrap_or(path.as_ref()); - self.write(path.to_string_lossy().as_bytes()); + self.write_path(path); self.write_eol(); } /// Prints the given path and a count of the number of matches found. pub fn path_count<P: AsRef<Path>>(&mut self, path: P, count: u64) { if self.with_filename { - self.write(path.as_ref().to_string_lossy().as_bytes()); + self.write_path(path); self.write(b":"); } self.write(count.to_string().as_bytes()); @@ -186,7 +186,7 @@ impl<W: Terminal + Send> Printer<W> { let column = if self.column { Some(re.find(&buf[start..end]) - .map(|(s, _)| s + 1).unwrap_or(0) as u64) + .map(|(s, _)| s + 1).unwrap_or(0) as u64) } else { None }; @@ -213,7 +213,7 @@ impl<W: Terminal + Send> Printer<W> { if self.heading && self.with_filename && !self.has_printed { self.write_heading(path.as_ref()); } else if !self.heading && self.with_filename { - self.write(path.as_ref().to_string_lossy().as_bytes()); + self.write_path(path.as_ref()); self.write(b":"); } if let Some(line_number) = line_number { @@ -263,7 +263,7 @@ impl<W: Terminal + Send> Printer<W> { if self.heading && self.with_filename && !self.has_printed { self.write_heading(path.as_ref()); } else if !self.heading && self.with_filename { - self.write(path.as_ref().to_string_lossy().as_bytes()); + self.write_path(path.as_ref()); self.write(b"-"); } if let Some(line_number) = line_number { @@ -280,7 +280,7 @@ impl<W: Terminal + Send> Printer<W> { let _ = self.wtr.fg(color::BRIGHT_GREEN); let _ = self.wtr.attr(Attr::Bold); } - self.write(path.as_ref().to_string_lossy().as_bytes()); + self.write_path(path.as_ref()); self.write_eol(); if self.wtr.supports_color() { let _ = self.wtr.reset(); @@ -299,6 +299,19 @@ impl<W: Terminal + Send> Printer<W> { 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(); + self.write(path); + } + + #[cfg(not(unix))] + fn write_path<P: AsRef<Path>>(&mut self, p: P) { + self.write(path.as_ref().to_string_lossy().as_bytes()); + } + fn write(&mut self, buf: &[u8]) { if self.quiet { return; |