Replace unmaintained `encoding` with `encoding_rs`

This commit is contained in:
rhysd 2023-07-21 23:41:32 +09:00 committed by David Peter
parent d295b3a394
commit d33b2e158e
3 changed files with 21 additions and 79 deletions

65
Cargo.lock generated
View File

@ -130,7 +130,7 @@ dependencies = [
"console", "console",
"content_inspector", "content_inspector",
"dirs", "dirs",
"encoding", "encoding_rs",
"expect-test", "expect-test",
"flate2", "flate2",
"git2", "git2",
@ -383,69 +383,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]] [[package]]
name = "encoding" name = "encoding_rs"
version = "0.2.33" version = "0.8.32"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" checksum = "071a31f4ee85403370b58aca746f01041ede6f0da2730960ad001edc2b71b394"
dependencies = [ dependencies = [
"encoding-index-japanese", "cfg-if",
"encoding-index-korean",
"encoding-index-simpchinese",
"encoding-index-singlebyte",
"encoding-index-tradchinese",
] ]
[[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]] [[package]]
name = "errno" name = "errno"
version = "0.3.3" version = "0.3.3"

View File

@ -49,7 +49,6 @@ once_cell = "1.17"
thiserror = "1.0" thiserror = "1.0"
wild = { version = "2.1", optional = true } wild = { version = "2.1", optional = true }
content_inspector = "0.2.4" content_inspector = "0.2.4"
encoding = "0.2"
shell-words = { version = "1.1.0", optional = true } shell-words = { version = "1.1.0", optional = true }
unicode-width = "0.1.10" unicode-width = "0.1.10"
globset = "0.4" globset = "0.4"
@ -64,6 +63,7 @@ grep-cli = { version = "0.1.9", optional = true }
regex = { version = "1.8.3", optional = true } regex = { version = "1.8.3", optional = true }
walkdir = { version = "2.3", optional = true } walkdir = { version = "2.3", optional = true }
bytesize = { version = "1.2.0" } bytesize = { version = "1.2.0" }
encoding_rs = "0.8.32"
[dependencies.git2] [dependencies.git2]
version = "0.18" version = "0.18"

View File

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