Skip to content

Commit

Permalink
Merge pull request ggerganov#48 from jcsoo/token_to_cstr
Browse files Browse the repository at this point in the history
Add token_to_cstr
  • Loading branch information
tazz4843 authored May 5, 2023
2 parents 3fce9cb + c2372db commit a7caf1b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/whisper_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,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<String, WhisperError> {
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.
Expand Down
2 changes: 1 addition & 1 deletion sys/whisper.cpp

0 comments on commit a7caf1b

Please sign in to comment.