Skip to content

Commit

Permalink
Replace unmaintained encoding with encoding_rs
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Jul 21, 2023
1 parent 783d487 commit 0311b7b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 93 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.

18 changes: 3 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,10 @@ rust-version = "1.64"
default = ["application"]
# Feature required for bat the application. Should be disabled when depending on
# bat as a library.
application = [
"bugreport",
"build-assets",
"git",
"minimal-application",
]
application = ["bugreport", "build-assets", "git", "minimal-application"]
# Mainly for developers that want to iterate quickly
# Be aware that the included features might change in the future
minimal-application = [
"is-terminal",
"clap",
"dirs",
"paging",
"regex-onig",
"wild",
]
minimal-application = ["is-terminal", "clap", "dirs", "paging", "regex-onig", "wild"]
git = ["git2"] # Support indicating git modifications
paging = ["shell-words", "grep-cli"] # Support applying a pager on the output
build-assets = ["syntect/yaml-load", "syntect/plist-load", "regex", "walkdir"]
Expand All @@ -51,7 +39,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 @@ -66,6 +53,7 @@ grep-cli = { version = "0.1.7", 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.16"
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 0311b7b

Please sign in to comment.