升级
This commit is contained in:
29
src/lib.rs
29
src/lib.rs
@@ -1,9 +1,12 @@
|
||||
use ::log::{debug, error, info, trace, warn};
|
||||
use encoding_rs::Encoding;
|
||||
use tokio::runtime::Runtime;
|
||||
use crate::utils::cxx_string_to_string;
|
||||
|
||||
mod log;
|
||||
mod network;
|
||||
mod config;
|
||||
pub mod utils;
|
||||
|
||||
#[cxx::bridge]
|
||||
mod ffi {
|
||||
@@ -76,30 +79,4 @@ fn http_get(url: &cxx::CxxString) -> Result<String, Box<dyn std::error::Error>>
|
||||
rt.block_on(network::http_get(&url))
|
||||
}
|
||||
|
||||
fn cxx_string_to_string(s: &cxx::CxxString) -> String {
|
||||
match s.to_str() {
|
||||
Ok(s) => return s.to_string(),
|
||||
Err(_) => {}
|
||||
};
|
||||
|
||||
// 不是UTF-8,尝试转换
|
||||
let candidates = [
|
||||
"gb18030", // 覆盖 GBK/GB2312 的常见场景
|
||||
"windows-1252", // 西欧常见
|
||||
"shift_jis", // 日文常见
|
||||
"big5", // 繁体中文常见
|
||||
];
|
||||
|
||||
let bytes = s.as_bytes();
|
||||
|
||||
for label in candidates {
|
||||
let enc = Encoding::for_label(label.as_bytes()).unwrap();
|
||||
let (cow, _actual_used, had_errors) = enc.decode(bytes);
|
||||
if !had_errors {
|
||||
return cow.into_owned();
|
||||
}
|
||||
}
|
||||
|
||||
/// 编码全部没有命中的话,则丢弃无法解析的部分
|
||||
s.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user