From c2372dbae9400e272bdd706ae78cd228a508b4b4 Mon Sep 17 00:00:00 2001 From: Jonathan Soo Date: Thu, 4 May 2023 19:35:37 -0400 Subject: [PATCH] Add token_to_cstr --- src/whisper_ctx.rs | 20 +++++++++++++++++--- sys/whisper.cpp | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/whisper_ctx.rs b/src/whisper_ctx.rs index 98afa9940a6..f5a0108b474 100644 --- a/src/whisper_ctx.rs +++ b/src/whisper_ctx.rs @@ -308,13 +308,27 @@ impl WhisperContext { /// # C++ equivalent /// `const char * whisper_token_to_str(struct whisper_context * ctx, whisper_token token)` pub fn token_to_str(&self, token_id: WhisperToken) -> Result { + let c_str = self.token_to_cstr(token_id)?; + let r_str = c_str.to_str()?; + Ok(r_str.to_string()) + } + + /// Convert a token ID to a &CStr. + /// + /// # Arguments + /// * token_id: ID of the token. + /// + /// # Returns + /// Ok(String) on success, Err(WhisperError) on failure. + /// + /// # C++ equivalent + /// `const char * whisper_token_to_str(struct whisper_context * ctx, whisper_token token)` + pub fn token_to_cstr(&self, token_id: WhisperToken) -> Result<&CStr, WhisperError> { let ret = unsafe { whisper_rs_sys::whisper_token_to_str(self.ctx, token_id) }; if ret.is_null() { return Err(WhisperError::NullPointer); } - let c_str = unsafe { CStr::from_ptr(ret) }; - let r_str = c_str.to_str()?; - Ok(r_str.to_string()) + Ok(unsafe { CStr::from_ptr(ret) }) } /// Undocumented but exposed function in the C++ API. diff --git a/sys/whisper.cpp b/sys/whisper.cpp index c23588cc4b2..5108b30e6da 160000 --- a/sys/whisper.cpp +++ b/sys/whisper.cpp @@ -1 +1 @@ -Subproject commit c23588cc4b2c4be4ba3634a21c7f2b302c5f370e +Subproject commit 5108b30e6daf361c856abb6b86e5038500bdbeb1