🐞 fix: cmd error with gbk (#114)

* 🐞 fix: cmd error with gbk
* 🎈 perf: try gbk only on windows
This commit is contained in:
m1m1sha
2024-05-15 20:50:11 +08:00
committed by GitHub
parent d5bf041834
commit 58d2ec475c
4 changed files with 92 additions and 5 deletions
+16
View File
@@ -225,6 +225,22 @@ pub fn init_logger(
Ok(ret_sender)
}
#[cfg(target_os = "windows")]
pub fn utf8_or_gbk_to_string(s: &[u8]) -> String {
use encoding::{all::GBK, DecoderTrap, Encoding};
if let Ok(utf8_str) = String::from_utf8(s.to_vec()) {
utf8_str
} else {
// 如果解码失败,则尝试使用GBK解码
if let Ok(gbk_str) = GBK.decode(&s, DecoderTrap::Strict) {
gbk_str
} else {
String::from_utf8_lossy(s).to_string()
}
}
}
#[cfg(test)]
mod tests {
use crate::common::config::{self};