From d33b2e158ea96fefd3be7d30916d137fed5e3e88 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 21 Jul 2023 23:41:32 +0900 Subject: [PATCH] Replace unmaintained `encoding` with `encoding_rs` --- Cargo.lock | 65 ++++---------------------------------------------- Cargo.toml | 2 +- src/printer.rs | 33 ++++++++++++------------- 3 files changed, 21 insertions(+), 79 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0d5264be..fa5b7e44 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,7 +130,7 @@ dependencies = [ "console", "content_inspector", "dirs", - "encoding", + "encoding_rs", "expect-test", "flate2", "git2", @@ -383,69 +383,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" [[package]] -name = "encoding" -version = "0.2.33" +name = "encoding_rs" +version = "0.8.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" +checksum = "071a31f4ee85403370b58aca746f01041ede6f0da2730960ad001edc2b71b394" dependencies = [ - "encoding-index-japanese", - "encoding-index-korean", - "encoding-index-simpchinese", - "encoding-index-singlebyte", - "encoding-index-tradchinese", + "cfg-if", ] -[[package]] -name = "encoding-index-japanese" -version = "1.20141219.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91" -dependencies = [ - "encoding_index_tests", -] - -[[package]] -name = "encoding-index-korean" -version = "1.20141219.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81" -dependencies = [ - "encoding_index_tests", -] - -[[package]] -name = "encoding-index-simpchinese" -version = "1.20141219.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7" -dependencies = [ - "encoding_index_tests", -] - -[[package]] -name = "encoding-index-singlebyte" -version = "1.20141219.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a" -dependencies = [ - "encoding_index_tests", -] - -[[package]] -name = "encoding-index-tradchinese" -version = "1.20141219.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" -dependencies = [ - "encoding_index_tests", -] - -[[package]] -name = "encoding_index_tests" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" - [[package]] name = "errno" version = "0.3.3" diff --git a/Cargo.toml b/Cargo.toml index 2164f14d..1f8bc755 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,6 @@ once_cell = "1.17" thiserror = "1.0" wild = { version = "2.1", optional = true } content_inspector = "0.2.4" -encoding = "0.2" shell-words = { version = "1.1.0", optional = true } unicode-width = "0.1.10" globset = "0.4" @@ -64,6 +63,7 @@ grep-cli = { version = "0.1.9", optional = true } regex = { version = "1.8.3", optional = true } walkdir = { version = "2.3", optional = true } bytesize = { version = "1.2.0" } +encoding_rs = "0.8.32" [dependencies.git2] version = "0.18" diff --git a/src/printer.rs b/src/printer.rs index d46b23d1..28230f34 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -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 } };