Repository: sharkdp/bat
File: src/printer.rs (Rust)

Code before:
                self.config.nonprintable_notation,
            )
            .into()
        } else {
            let mut line = match self.content_type {
                Some(ContentType::BINARY) | None
                    if !matches!(self.config.binary, BinaryBehavior::AsText) =>
                {
                    return Ok(());
                }
                Some(ContentType::UTF_16LE) => UTF_16LE.decode_with_bom_removal(line_buffer).0,
                Some(ContentType::UTF_16BE) => UTF_16BE.decode_with_bom_removal(line_buffer).0,
                _ => {
                    let line = String::from_utf8_lossy(line_buffer);
                    if line_number == 1 {
                        match line.strip_prefix('\u{feff}') {
                            Some(stripped) => stripped.to_string().into(),
                            None => line,
                        }
                    } else {
                        line
                    }
                }
            };

            if self.strip_overstrike && line.contains('\x08') {
                line = Cow::Owned(strip_overstrike(&line).into_owned());
            }

            // If ANSI escape sequences are supposed to be stripped, do it before syntax highlighting.
            if self.strip_ansi {
                line = strip_ansi(&line).into()
            }

            line
        };

        let regions = self.highlight_regions_for_line(&line)?;
        if out_of_range {
            return Ok(());
        }

        // Skip squeezed lines.
        if let Some(squeeze_limit) = self.config.squeeze_lines {
            if line.trim_end_matches(['\r', '\n']).is_empty() {
                self.consecutive_empty_lines += 1;
                if self.consecutive_empty_lines > squeeze_limit {
                    return Ok(());
                }
            } else {
                self.consecutive_empty_lines = 0;

Review comment (performance):
I wonder how necessary the `line.contains` check is, as the first thing `strip_overstrike` does is perform a find - essentially double work? Would it be worth benchmarking with vs without this check to see which is really better for performance? Or to consider doing a find here and passing the position of it into strip_overstrike? Maybe an unnecessary micro-optimization, but I'm a bit worried because this will run on every line of input passing through bat...

Code after:
                    if !matches!(self.config.binary, BinaryBehavior::AsText) =>
                {
                    return Ok(());
                }
                Some(ContentType::UTF_16LE) => UTF_16LE.decode_with_bom_removal(line_buffer).0,
                Some(ContentType::UTF_16BE) => UTF_16BE.decode_with_bom_removal(line_buffer).0,
                _ => {
                    let line = String::from_utf8_lossy(line_buffer);
                    if line_number == 1 {
                        match line.strip_prefix('\u{feff}') {
                            Some(stripped) => stripped.to_string().into(),
                            None => line,
                        }
                    } else {
                        line
                    }
                }
            };

            if self.strip_overstrike {
                if let Some(pos) = line.find('\x08') {
                    line = strip_overstrike(&line, pos).into();
                }
            }

            // If ANSI escape sequences are supposed to be stripped, do it before syntax highlighting.
            if self.strip_ansi {
                line = strip_ansi(&line).into()
            }

            line
        };

        let regions = self.highlight_regions_for_line(&line)?;
        if out_of_range {
            return Ok(());
        }

        // Skip squeezed lines.
        if let Some(squeeze_limit) = self.config.squeeze_lines {
            if line.trim_end_matches(['\r', '\n']).is_empty() {
                self.consecutive_empty_lines += 1;
                if self.consecutive_empty_lines > squeeze_limit {
                    return Ok(());
                }
            } else {
                self.consecutive_empty_lines = 0;
            }
        }

        let mut cursor: usize = 0;