Skip to content

Commit

Permalink
Merge pull request #2626 from rhysd/encoding_rs
Browse files Browse the repository at this point in the history
Replace unmaintained `encoding` dependency with `encoding_rs`
  • Loading branch information
sharkdp authored Sep 1, 2023
2 parents 9ba3b6e + d33b2e1 commit 1e52785
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 79 deletions.
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

0 comments on commit 1e52785

Please sign in to comment.