chore: minor refactor

This commit is contained in:
CismonX 2023-02-20 13:44:27 +08:00
parent d748d8972e
commit 787f487b19
Signed by: cismonx
GPG Key ID: 3094873E29A482FB
3 changed files with 25 additions and 28 deletions

View File

@ -104,9 +104,7 @@ arif_rime_finalize (
free_candidates(ctx); free_candidates(ctx);
if (rime_api != NULL) { if (rime_api != NULL) {
if (ctx->session != 0) { rime_api->destroy_session(ctx->session);
rime_api->destroy_session(ctx->session);
}
} }
free(ctx); free(ctx);
@ -140,7 +138,7 @@ arif_rime_init (
assert(ctx != NULL); assert(ctx != NULL);
*ctx = (struct engine_ctx) { *ctx = (struct engine_ctx) {
.session = rime_api->create_session(), .session = 0,
}; };
*engine_data_ptr = ctx; *engine_data_ptr = ctx;
return 0; return 0;

View File

@ -60,7 +60,6 @@ struct arif_ctx {
// Forward declaration start // Forward declaration start
static struct cand_page * static struct cand_page *
choose_candidate (struct cand_page *, int); choose_candidate (struct cand_page *, int);
static void clear_old_line (struct arif_ctx *);
static int compare_text (char const *, int, char const *, int); static int compare_text (char const *, int, char const *, int);
static void copy_candidate (struct arif_cand *, struct arif_cand const *, static void copy_candidate (struct arif_cand *, struct arif_cand const *,
int, arif_cand_disp_func *); int, arif_cand_disp_func *);
@ -77,6 +76,7 @@ static struct cand_page *
static void new_pages (struct arif_cand const *, int, int, static void new_pages (struct arif_cand const *, int, int,
arif_cand_disp_func *, struct cand_page **, arif_cand_disp_func *, struct cand_page **,
struct cand_page **); struct cand_page **);
static void set_old_line (struct arif_ctx *, char const *, int, int);
// Forward declaration end // Forward declaration end
static struct cand_page * static struct cand_page *
@ -105,16 +105,6 @@ choose_candidate (
return new_page; return new_page;
} }
static inline void
clear_old_line (
struct arif_ctx *ctx
) {
free((char *) ctx->old_line);
ctx->old_line = NULL;
ctx->old_offset = 0;
ctx->old_len = 0;
}
static inline int static inline int
compare_text ( compare_text (
char const *text1, char const *text1,
@ -213,14 +203,14 @@ free_page_list (
if (head == NULL) { if (head == NULL) {
return; return;
} }
struct cand_page *prev = head->prev; struct cand_page *next, *prev = head->prev;
// free forward // free forward
for (struct cand_page *next, *page = head; page != NULL; page = next) { for (struct cand_page *page = head; page != NULL; page = next) {
next = page->next; next = page->next;
free_page(page); free_page(page);
} }
// free backward // free backward
for (struct cand_page *next, *page = prev; page != NULL; page = next) { for (struct cand_page *page = prev; page != NULL; page = next) {
next = page->prev; next = page->prev;
free_page(page); free_page(page);
} }
@ -398,11 +388,7 @@ arif_query (
char *saved_line = malloc(sizeof(char) * (offset + len)); char *saved_line = malloc(sizeof(char) * (offset + len));
assert(saved_line != NULL); assert(saved_line != NULL);
free((char *) ctx->old_line); set_old_line(ctx, memcpy(saved_line, line, offset + len), offset, len);
ctx->old_line = memcpy(saved_line, line, offset + len);
ctx->old_offset = offset;
ctx->old_len = len;
first_candidates(ctx, saved_line, offset, len); first_candidates(ctx, saved_line, offset, len);
} else { } else {
// same text as old query // same text as old query
@ -426,7 +412,7 @@ arif_query (
ctx->page_num = 1; ctx->page_num = 1;
ctx->no_more_pages = false; ctx->no_more_pages = false;
clear_old_line(ctx); set_old_line(ctx, NULL, 0, 0);
} }
finish: finish:
@ -482,8 +468,21 @@ arif_set_engine (
ctx->page_num = 0; ctx->page_num = 0;
ctx->no_more_pages = false; ctx->no_more_pages = false;
clear_old_line(ctx); set_old_line(ctx, NULL, 0, 0);
ctx->engine = engine; ctx->engine = engine;
ctx->engine_data = engine_data; ctx->engine_data = engine_data;
} }
static inline void
set_old_line (
struct arif_ctx *ctx,
char const *old_line,
int old_offset,
int old_len
) {
free((char *) ctx->old_line);
ctx->old_line = old_line;
ctx->old_offset = old_offset;
ctx->old_len = old_len;
}

View File

@ -20,9 +20,9 @@ arif_test_SOURCES = arif_test.c arif_dummy_engine.c
libarifdummy_la_CPPFLAGS = -I$(top_srcdir)/include libarifdummy_la_CPPFLAGS = -I$(top_srcdir)/include
libarifdummy_la_SOURCES = arif_dummy_engine.c libarifdummy_la_SOURCES = arif_dummy_engine.c
EXTRA_DIST = lib/arif.exp \ EXTRA_DIST = lib/arif.exp \
arif/000-query.exp \ arif/000-query.exp \
arif/001-select-page.exp \ arif/001-select-page.exp \
arif/002-fetch.exp arif/002-fetch.exp
RUNTESTFLAGS = ARIF_TEST_BIN=$(builddir)/arif-test RUNTESTFLAGS = ARIF_TEST_BIN=$(builddir)/arif-test