Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace unmaintained encoding dependency with encoding_rs #2626

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 5 additions & 60 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
33 changes: 15 additions & 18 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
}
};

Expand Down