Skip to content

Commit cbe7584

Browse files
committed
Refactor string handling in OfflineRecognizerResult and TTS modules
1 parent 28b6259 commit cbe7584

File tree

3 files changed

+14
-47
lines changed

3 files changed

+14
-47
lines changed

crates/sherpa-rs/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ pub struct OfflineRecognizerResult {
9898

9999
impl OfflineRecognizerResult {
100100
fn new(result: &sherpa_rs_sys::SherpaOnnxOfflineRecognizerResult) -> Self {
101-
let lang = cstr_to_string(result.lang);
102-
let text = cstr_to_string(result.text);
101+
let lang = unsafe { cstr_to_string(result.lang) };
102+
let text = unsafe { cstr_to_string(result.text) };
103103
let count = result.count.try_into().unwrap();
104104
let timestamps = if result.timestamps.is_null() {
105105
Vec::new()

crates/sherpa-rs/src/tts/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ mod kokoro;
22
mod matcha;
33
mod vits;
44

5+
use std::ffi::CString;
6+
57
use eyre::{bail, Result};
68

79
pub use kokoro::{KokoroTts, KokoroTtsConfig};
810
pub use matcha::{MatchaTts, MatchaTtsConfig};
911
pub use vits::{VitsTts, VitsTtsConfig};
1012

11-
use crate::utils::{cstring_from_str, RawCStr};
13+
use crate::utils::cstring_from_str;
1214

1315
#[derive(Debug)]
1416
pub struct TtsAudio {
@@ -26,8 +28,8 @@ pub struct CommonTtsConfig {
2628
}
2729

2830
pub struct CommonTtsRaw {
29-
pub rule_fars: Option<RawCStr>,
30-
pub rule_fsts: Option<RawCStr>,
31+
pub rule_fars: Option<CString>,
32+
pub rule_fsts: Option<CString>,
3133
pub max_num_sentences: i32,
3234
}
3335

crates/sherpa-rs/src/utils.rs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,13 @@
11
use std::ffi::{c_char, CString};
22

3-
pub fn cstring_from_str(s: &str) -> RawCStr {
4-
RawCStr::new(s)
3+
pub fn cstring_from_str(s: &str) -> CString {
4+
CString::new(s).expect("CString::new failed")
55
}
66

7-
pub fn cstr_to_string(ptr: *const c_char) -> String {
8-
unsafe {
9-
if ptr.is_null() {
10-
String::new()
11-
} else {
12-
std::ffi::CStr::from_ptr(ptr).to_string_lossy().into_owned()
13-
}
14-
}
15-
}
16-
17-
pub struct RawCStr {
18-
ptr: *mut std::ffi::c_char,
19-
}
20-
21-
impl RawCStr {
22-
/// Creates a new `CStr` from a given Rust string slice.
23-
pub fn new(s: &str) -> Self {
24-
let cstr = CString::new(s).expect("CString::new failed");
25-
let ptr = cstr.into_raw();
26-
Self { ptr }
27-
}
28-
29-
/// Returns the raw pointer to the internal C string.
30-
///
31-
/// # Safety
32-
/// This function only returns the raw pointer and does not transfer ownership.
33-
/// The pointer remains valid as long as the `CStr` instance exists.
34-
/// Be cautious not to deallocate or modify the pointer after using `CStr::new`.
35-
pub fn as_ptr(&self) -> *const c_char {
36-
self.ptr
37-
}
38-
}
39-
40-
impl Drop for RawCStr {
41-
fn drop(&mut self) {
42-
if !self.ptr.is_null() {
43-
unsafe {
44-
let _ = CString::from_raw(self.ptr);
45-
}
46-
}
7+
pub unsafe fn cstr_to_string(ptr: *const c_char) -> String {
8+
if ptr.is_null() {
9+
String::new()
10+
} else {
11+
std::ffi::CStr::from_ptr(ptr).to_string_lossy().into_owned()
4712
}
4813
}

0 commit comments

Comments
 (0)