Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows向けのマルチバイト処理を追加した #1

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/open_jtalk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
[package]
name = "open_jtalk"
version = "0.1.24"
version = "0.1.25"
edition = "2021"

[dependencies]
open_jtalk-sys = { path = "../open_jtalk-sys", version = "0.15.111" }
thiserror = "1.0.31"

[target.'cfg(target_os = "windows")'.dependencies]
local-encoding = "0.2.0"
windows = { version = "0.39.0", features = ["Win32_Globalization"] }

[dev-dependencies]
rstest = "0.12.0"
pretty_assertions = "1.2.1"
3 changes: 3 additions & 0 deletions crates/open_jtalk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ mod njd;
mod resource;
mod text2mecab;

#[cfg(target_os = "windows")]
mod win_api_helper;

pub use jpcommon::*;
pub use mecab::*;
pub use njd::*;
Expand Down
12 changes: 11 additions & 1 deletion crates/open_jtalk/src/mecab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ impl Mecab {
}

pub fn load(&mut self, dic_dir: impl AsRef<Path>) -> bool {
let dic_dir = CString::new(dic_dir.as_ref().to_str().unwrap()).unwrap();
let path_str = dic_dir.as_ref().to_str().unwrap();
#[cfg(target_os = "windows")]
let dic_dir = {
if let Ok(s) = win_api_helper::str_to_local_multi_byte_string(path_str) {
s
} else {
return false;
}
};
#[cfg(not(target_os = "windows"))]
let dic_dir = CString::new(path_str).unwrap();
unsafe {
bool_number_to_bool(open_jtalk_sys::Mecab_load(
self.as_raw_ptr(),
Expand Down
8 changes: 8 additions & 0 deletions crates/open_jtalk/src/win_api_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::ffi::CString;

pub fn str_to_local_multi_byte_string(s: &str) -> Result<CString, std::io::Error> {
let cp = unsafe { windows::Win32::Globalization::GetACP() };
Ok(unsafe {
CString::from_vec_unchecked(local_encoding::windows::string_to_multibyte(cp, s, None)?)
})
}