Merge pull request #2626 from rhysd/encoding_rs

Replace unmaintained `encoding` dependency with `encoding_rs`
This commit is contained in:
David Peter
2023-09-01 21:12:07 +02:00
committed by GitHub
3 changed files with 21 additions and 79 deletions

View File

@@ -15,8 +15,7 @@ use syntect::parsing::SyntaxSet;
use content_inspector::ContentType;
use encoding::all::{UTF_16BE, UTF_16LE};
use encoding::{DecoderTrap, Encoding};
use encoding_rs::{UTF_16BE, UTF_16LE};
use unicode_width::UnicodeWidthChar;
@@ -431,27 +430,25 @@ impl<'a> Printer for InteractivePrinter<'a> {
self.config.tab_width,
self.config.nonprintable_notation,
)
.into()
} else {
let line = match self.content_type {
match self.content_type {
Some(ContentType::BINARY) | None => {
return Ok(());
}
Some(ContentType::UTF_16LE) => UTF_16LE
.decode(line_buffer, DecoderTrap::Replace)
.map_err(|_| "Invalid UTF-16LE")?,
Some(ContentType::UTF_16BE) => UTF_16BE
.decode(line_buffer, DecoderTrap::Replace)
.map_err(|_| "Invalid UTF-16BE")?,
_ => String::from_utf8_lossy(line_buffer).to_string(),
};
// Remove byte order mark from the first line if it exists
if line_number == 1 {
match line.strip_prefix('\u{feff}') {
Some(stripped) => stripped.to_string(),
None => line,
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
}
}
} else {
line
}
};