From c8cd14bf48741bd35653ccab09aff2ade7bb66d7 Mon Sep 17 00:00:00 2001 From: Daniel Berg Date: Thu, 22 Apr 2021 18:53:49 +0200 Subject: [PATCH] Fix #21 WM_NAME decoding issue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue where from_utf8 would error when presented with a latin-1 encoded string, in the case of "Actualités" --- Cargo.lock | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 15 +++++++++++-- 3 files changed, 79 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c26ab19..7eed88c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,6 +90,70 @@ version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3" +[[package]] +name = "encoding" +version = "0.2.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" +dependencies = [ + "encoding-index-japanese", + "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]] name = "exitfailure" version = "0.5.1" @@ -153,6 +217,7 @@ name = "i3wsr" version = "2.0.0" dependencies = [ "clap", + "encoding", "exitfailure", "failure", "failure_derive", diff --git a/Cargo.toml b/Cargo.toml index 1ae14ee..3aa1f73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ toml = "0.5.6" serde = { version = "1.0.111", features = ["derive"] } itertools = "0.9.0" regex = "1" +encoding = "0.2" [dependencies.i3ipc] version = "0.10.1" diff --git a/src/lib.rs b/src/lib.rs index 90e2000..6d676fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,6 +27,10 @@ extern crate lazy_static; extern crate toml; +extern crate encoding; +use encoding::{Encoding, DecoderTrap}; +use encoding::all::ISO_8859_1; + pub mod config; pub mod icons; pub mod regex; @@ -70,8 +74,15 @@ fn get_property( ); let reply = cookie.get_reply()?; - let reply = std::str::from_utf8(reply.value())?.to_string(); - Ok(reply) + if let Ok(s) = std::str::from_utf8(reply.value()) { + Ok(s.to_string()) + } else { + let decoded = ISO_8859_1.decode(reply.value(), DecoderTrap::Strict); + match decoded { + Ok(s) => Ok(s), + Err(_) => Ok(String::new()), + } + } } /// Gets a window title, depends on wm_property config opt