Skip to content

Commit

Permalink
Merge pull request #661 from chewing/feat-get-bopomofo-alloc
Browse files Browse the repository at this point in the history
feat(capi): add new chewing_bopomofo_String API
  • Loading branch information
kanru authored Dec 7, 2024
2 parents bab0250 + abda404 commit 1a30890
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 3 deletions.
18 changes: 18 additions & 0 deletions capi/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,24 @@ pub unsafe extern "C" fn chewing_bopomofo_String_static(
copy_cstr(&mut ctx.bopomofo_buf, &ctx.editor.syllable_buffer_display())
}

/// # Safety
///
/// This function should be called with valid pointers.
#[no_mangle]
pub unsafe extern "C" fn chewing_bopomofo_String(ctx: *const ChewingContext) -> *mut c_char {
let ctx = as_ref_or_return!(
ctx,
owned_into_raw(Owned::CString, CString::default().into_raw())
);

let buffer = ctx.editor.syllable_buffer_display();
let cstr = match CString::new(buffer) {
Ok(cstr) => cstr,
Err(_) => return null_mut(),
};
owned_into_raw(Owned::CString, cstr.into_raw())
}

/// # Safety
///
/// This function should be called with valid pointers.
Expand Down
11 changes: 11 additions & 0 deletions capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,17 @@ pub mod output {
/// </p>
pub use super::io::chewing_zuin_Check;

/// Returns the phonetic characters in the pre-edit buffer.
///
/// The returned value is a pointer to a character string. The memory must
/// be freed by the caller using function
/// [chewing_free][super::setup::chewing_free].
///
/// # Failures
///
/// This function returns NULL when memory allocation fails.
pub use super::io::chewing_bopomofo_String;

/// Returns the phonetic characters in the pre-edit buffer.
///
/// The return value is a const pointer to a character string. The pointer
Expand Down
7 changes: 6 additions & 1 deletion capi/src/symbols-elf.map
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,9 @@ CHEWING_0.9 {
chewing_version_minor;
chewing_version_patch;
chewing_version_extra;
} CHEWING_0.5;
} CHEWING_0.5;

CHEWING_0.10 {
global:
chewing_bopomofo_String;
} CHEWING_0.9;
1 change: 1 addition & 0 deletions capi/src/symbols-mach_o.map
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ _chewing_aux_Length
_chewing_aux_String
_chewing_aux_String_static
_chewing_bopomofo_Check
_chewing_bopomofo_String
_chewing_bopomofo_String_static
_chewing_buffer_Check
_chewing_buffer_Len
Expand Down
1 change: 1 addition & 0 deletions capi/src/symbols-msvc.def
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ EXPORTS
chewing_aux_String;
chewing_aux_String_static;
chewing_bopomofo_Check;
chewing_bopomofo_String;
chewing_bopomofo_String_static;
chewing_buffer_Check;
chewing_buffer_Len;
Expand Down
9 changes: 8 additions & 1 deletion include/chewing.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ typedef struct ChewingContext ChewingContext;

#define CHEWING_VERSION_MINOR 9

#define CHEWING_VERSION_PATCH 0
#define CHEWING_VERSION_PATCH 1

/**
* Keyboard layout index.
Expand Down Expand Up @@ -850,6 +850,13 @@ int chewing_buffer_Len(const struct ChewingContext *ctx);
*/
const char *chewing_bopomofo_String_static(const struct ChewingContext *ctx);

/**
* # Safety
*
* This function should be called with valid pointers.
*/
char *chewing_bopomofo_String(const struct ChewingContext *ctx);

/**
* # Safety
*
Expand Down
4 changes: 4 additions & 0 deletions tests/test-error-handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ void test_null()
const_buf = chewing_buffer_String_static(NULL);
ok(strcmp(const_buf, "") == 0, "chewing_buffer_String_static() returns `%s' shall be `%s'", const_buf, "");

buf = chewing_bopomofo_String(NULL);
ok(strcmp(buf, "") == 0, "chewing_bopomofo_String() returns `%s' shall be `%s'", buf, "");
chewing_free(buf);

const_buf = chewing_bopomofo_String_static(NULL);
ok(strcmp(const_buf, "") == 0, "chewing_bopomofo_String_static() returns `%s' shall be `%s'", const_buf, "");

Expand Down
2 changes: 1 addition & 1 deletion tests/testhelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ BufferType BOPOMOFO_BUFFER = {
chewing_bopomofo_Check,
chewing_zuin_Check,
0,
0,
chewing_bopomofo_String,
chewing_zuin_String,
chewing_bopomofo_String_static
};
Expand Down

0 comments on commit 1a30890

Please sign in to comment.