From 589f4761ee896cea5c8d1b1dad6655bd0b78fc2d Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 4 Dec 2023 15:49:31 +0800 Subject: [PATCH] refactor(completion): use an inline function to free cptext (#26380) --- src/nvim/insexpand.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/nvim/insexpand.c b/src/nvim/insexpand.c index a59ba1b6d9..8791aeb10c 100644 --- a/src/nvim/insexpand.c +++ b/src/nvim/insexpand.c @@ -747,6 +747,16 @@ int ins_compl_add_infercase(char *str_arg, int len, bool icase, char *fname, Dir return res; } +/// free cptext +static inline void free_cptext(char *const *const cptext) +{ + if (cptext != NULL) { + for (size_t i = 0; i < CPT_COUNT; i++) { + xfree(cptext[i]); + } + } +} + /// Add a match to the list of matches /// /// @param[in] str text of the match to add @@ -784,16 +794,10 @@ static int ins_compl_add(char *const str, int len, char *const fname, char *cons } else { os_breakcheck(); } -#define FREE_CPTEXT(cptext, cptext_allocated) \ - do { \ - if ((cptext) != NULL && (cptext_allocated)) { \ - for (size_t i = 0; i < CPT_COUNT; i++) { \ - xfree((cptext)[i]); \ - } \ - } \ - } while (0) if (got_int) { - FREE_CPTEXT(cptext, cptext_allocated); + if (cptext_allocated) { + free_cptext(cptext); + } return FAIL; } if (len < 0) { @@ -807,7 +811,9 @@ static int ins_compl_add(char *const str, int len, char *const fname, char *cons if (!match_at_original_text(match) && strncmp(match->cp_str, str, (size_t)len) == 0 && ((int)strlen(match->cp_str) <= len || match->cp_str[len] == NUL)) { - FREE_CPTEXT(cptext, cptext_allocated); + if (cptext_allocated) { + free_cptext(cptext); + } return NOTDONE; } match = match->cp_next; @@ -1552,9 +1558,7 @@ static void ins_compl_free(void) if (match->cp_flags & CP_FREE_FNAME) { xfree(match->cp_fname); } - for (int i = 0; i < CPT_COUNT; i++) { - xfree(match->cp_text[i]); - } + free_cptext(match->cp_text); tv_clear(&match->cp_user_data); xfree(match); } while (compl_curr_match != NULL && !is_first_match(compl_curr_match)); @@ -2459,9 +2463,7 @@ static int ins_compl_add_tv(typval_T *const tv, const Direction dir, bool fast) CLEAR_FIELD(cptext); } if (word == NULL || (!empty && *word == NUL)) { - for (size_t i = 0; i < CPT_COUNT; i++) { - xfree(cptext[i]); - } + free_cptext(cptext); tv_clear(&user_data); return FAIL; }