Introduce GA_APPEND()

This macro is used to append an element to a growable array. It replaces this
common idiom:

   ga_grow(&ga, 1);
   ((item_type *)ga.ga_data)[ga.ga_len] = item;
   ++ga.ga_len;
This commit is contained in:
Felipe Oliveira Carvalho
2014-06-25 22:03:58 -03:00
committed by Justin M. Keyes
parent be3a4b6ca8
commit 45e7814e6a
12 changed files with 25 additions and 53 deletions

View File

@@ -14420,8 +14420,7 @@ error:
} }
} }
/* add a terminating NUL */ // add a terminating NUL
ga_grow(&ga, 1);
ga_append(&ga, NUL); ga_append(&ga, NUL);
rettv->vval.v_string = ga.ga_data; rettv->vval.v_string = ga.ga_data;
@@ -17640,8 +17639,7 @@ script_autoload (
else { else {
/* Remember the name if it wasn't loaded already. */ /* Remember the name if it wasn't loaded already. */
if (i == ga_loaded.ga_len) { if (i == ga_loaded.ga_len) {
ga_grow(&ga_loaded, 1); GA_APPEND(char_u *, &ga_loaded, scriptname);
((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
tofree = NULL; tofree = NULL;
} }

View File

@@ -5446,11 +5446,9 @@ helptags_one (
ga_init(&ga, (int)sizeof(char_u *), 100); ga_init(&ga, (int)sizeof(char_u *), 100);
if (add_help_tags || path_full_compare((char_u *)"$VIMRUNTIME/doc", if (add_help_tags || path_full_compare((char_u *)"$VIMRUNTIME/doc",
dir, FALSE) == kEqualFiles) { dir, FALSE) == kEqualFiles) {
ga_grow(&ga, 1);
s = xmalloc(18 + STRLEN(tagfname)); s = xmalloc(18 + STRLEN(tagfname));
sprintf((char *)s, "help-tags\t%s\t1\n", tagfname); sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
((char_u **)ga.ga_data)[ga.ga_len] = s; GA_APPEND(char_u *, &ga, s);
++ga.ga_len;
} }
/* /*
@@ -5517,10 +5515,8 @@ helptags_one (
|| s[1] == '\0')) { || s[1] == '\0')) {
*p2 = '\0'; *p2 = '\0';
++p1; ++p1;
ga_grow(&ga, 1);
s = xmalloc((p2 - p1) + STRLEN(fname) + 2); s = xmalloc((p2 - p1) + STRLEN(fname) + 2);
((char_u **)ga.ga_data)[ga.ga_len] = s; GA_APPEND(char_u *, &ga, s);
++ga.ga_len;
sprintf((char *)s, "%s\t%s", p1, fname); sprintf((char *)s, "%s\t%s", p1, fname);
/* find next '*' */ /* find next '*' */

View File

@@ -1525,8 +1525,7 @@ void get_arglist(garray_T *gap, char_u *str)
{ {
ga_init(gap, (int)sizeof(char_u *), 20); ga_init(gap, (int)sizeof(char_u *), 20);
while (*str != NUL) { while (*str != NUL) {
ga_grow(gap, 1); GA_APPEND(char_u *, gap, str);
((char_u **)gap->ga_data)[gap->ga_len++] = str;
/* Isolate one argument, change it in-place, put a NUL after it. */ /* Isolate one argument, change it in-place, put a NUL after it. */
str = do_one_arg(str); str = do_one_arg(str);
@@ -3332,13 +3331,12 @@ static char_u **find_locales(void)
loc = (char_u *)strtok((char *)locale_a, "\n"); loc = (char_u *)strtok((char *)locale_a, "\n");
while (loc != NULL) { while (loc != NULL) {
ga_grow(&locales_ga, 1);
loc = vim_strsave(loc); loc = vim_strsave(loc);
GA_APPEND(char_u *, &locales_ga, loc);
((char_u **)locales_ga.ga_data)[locales_ga.ga_len++] = loc;
loc = (char_u *)strtok(NULL, "\n"); loc = (char_u *)strtok(NULL, "\n");
} }
free(locale_a); free(locale_a);
// Guarantee that .ga_data is NULL terminated
ga_grow(&locales_ga, 1); ga_grow(&locales_ga, 1);
((char_u **)locales_ga.ga_data)[locales_ga.ga_len] = NULL; ((char_u **)locales_ga.ga_data)[locales_ga.ga_len] = NULL;
return (char_u **)locales_ga.ga_data; return (char_u **)locales_ga.ga_data;

View File

@@ -4000,10 +4000,7 @@ static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file,
continue; continue;
} }
ga_grow(&ga, 1); GA_APPEND(char_u *, &ga, vim_strnsave(s, (int)(e - s)));
((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
++ga.ga_len;
*e = keep; *e = keep;
if (*e != NUL) if (*e != NUL)
@@ -4034,11 +4031,7 @@ static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file)
if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
continue; /* Skip non-string items and empty strings */ continue; /* Skip non-string items and empty strings */
ga_grow(&ga, 1); GA_APPEND(char_u *, &ga, vim_strsave(li->li_tv.vval.v_string));
((char_u **)ga.ga_data)[ga.ga_len] =
vim_strsave(li->li_tv.vval.v_string);
++ga.ga_len;
} }
list_unref(retlist); list_unref(retlist);

View File

@@ -197,10 +197,7 @@ void ga_concat(garray_T *gap, const char_u *restrict s)
/// @param c /// @param c
void ga_append(garray_T *gap, char c) void ga_append(garray_T *gap, char c)
{ {
ga_grow(gap, 1); GA_APPEND(char, gap, c);
char *str = gap->ga_data;
str[gap->ga_len] = c;
gap->ga_len++;
} }
#if defined(UNIX) || defined(WIN3264) || defined(PROTO) #if defined(UNIX) || defined(WIN3264) || defined(PROTO)

View File

@@ -16,6 +16,12 @@ typedef struct growarray {
#define GA_EMPTY(ga_ptr) ((ga_ptr)->ga_len <= 0) #define GA_EMPTY(ga_ptr) ((ga_ptr)->ga_len <= 0)
#define GA_APPEND(item_type, gap, item) \
do { \
ga_grow(gap, 1); \
((item_type *)(gap)->ga_data)[(gap)->ga_len++] = (item); \
} while (0)
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "garray.h.generated.h" # include "garray.h.generated.h"
#endif #endif

View File

@@ -21,16 +21,13 @@ int os_get_usernames(garray_T *users)
ga_init(users, sizeof(char *), 20); ga_init(users, sizeof(char *), 20);
# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H) # if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
char *user;
struct passwd *pw; struct passwd *pw;
setpwent(); setpwent();
while ((pw = getpwent()) != NULL) { while ((pw = getpwent()) != NULL) {
// pw->pw_name shouldn't be NULL but just in case... // pw->pw_name shouldn't be NULL but just in case...
if (pw->pw_name != NULL) { if (pw->pw_name != NULL) {
ga_grow(users, 1); GA_APPEND(char *, users, xstrdup(pw->pw_name));
user = xstrdup(pw->pw_name);
((char **)(users->ga_data))[users->ga_len++] = user;
} }
} }
endpwent(); endpwent();

View File

@@ -625,7 +625,6 @@ static void expand_path_option(char_u *curdir, garray_T *gap)
char_u *path_option = *curbuf->b_p_path == NUL char_u *path_option = *curbuf->b_p_path == NUL
? p_path : curbuf->b_p_path; ? p_path : curbuf->b_p_path;
char_u *buf; char_u *buf;
char_u *p;
int len; int len;
buf = xmalloc(MAXPATHL); buf = xmalloc(MAXPATHL);
@@ -639,7 +638,7 @@ static void expand_path_option(char_u *curdir, garray_T *gap)
* "/path/file" + "./subdir" -> "/path/subdir" */ * "/path/file" + "./subdir" -> "/path/subdir" */
if (curbuf->b_ffname == NULL) if (curbuf->b_ffname == NULL)
continue; continue;
p = path_tail(curbuf->b_ffname); char_u *p = path_tail(curbuf->b_ffname);
len = (int)(p - curbuf->b_ffname); len = (int)(p - curbuf->b_ffname);
if (len + (int)STRLEN(buf) >= MAXPATHL) if (len + (int)STRLEN(buf) >= MAXPATHL)
continue; continue;
@@ -666,10 +665,7 @@ static void expand_path_option(char_u *curdir, garray_T *gap)
simplify_filename(buf); simplify_filename(buf);
} }
ga_grow(gap, 1); GA_APPEND(char_u *, gap, vim_strsave(buf));
p = vim_strsave(buf);
((char_u **)gap->ga_data)[gap->ga_len++] = p;
} }
free(buf); free(buf);
@@ -1194,7 +1190,6 @@ addfile (
int flags int flags
) )
{ {
char_u *p;
bool isdir; bool isdir;
/* if the file/dir doesn't exist, may not add it */ /* if the file/dir doesn't exist, may not add it */
@@ -1215,10 +1210,7 @@ addfile (
if (!isdir && (flags & EW_EXEC) && !os_can_exe(f)) if (!isdir && (flags & EW_EXEC) && !os_can_exe(f))
return; return;
/* Make room for another item in the file list. */ char_u *p = xmalloc(STRLEN(f) + 1 + isdir);
ga_grow(gap, 1);
p = xmalloc(STRLEN(f) + 1 + isdir);
STRCPY(p, f); STRCPY(p, f);
#ifdef BACKSLASH_IN_FILENAME #ifdef BACKSLASH_IN_FILENAME
@@ -1231,7 +1223,7 @@ addfile (
if (isdir && (flags & EW_ADDSLASH)) if (isdir && (flags & EW_ADDSLASH))
add_pathsep(p); add_pathsep(p);
#endif #endif
((char_u **)gap->ga_data)[gap->ga_len++] = p; GA_APPEND(char_u *, gap, p);
} }
#endif /* !NO_EXPANDPATH */ #endif /* !NO_EXPANDPATH */

View File

@@ -9138,8 +9138,7 @@ someerror:
if (c < 0) { if (c < 0) {
goto someerror; goto someerror;
} }
ga_grow(&ga, 1); GA_APPEND(char_u, &ga, c);
((char_u *)ga.ga_data)[ga.ga_len++] = c;
if (c == NUL) if (c == NUL)
break; break;
} }

View File

@@ -1963,9 +1963,7 @@ syn_current_attr (
/* Add the index to a list, so that we can check /* Add the index to a list, so that we can check
* later that we don't match it again (and cause an * later that we don't match it again (and cause an
* endless loop). */ * endless loop). */
ga_grow(&zero_width_next_ga, 1); GA_APPEND(int, &zero_width_next_ga, next_match_idx);
((int *)(zero_width_next_ga.ga_data))
[zero_width_next_ga.ga_len++] = next_match_idx;
next_match_idx = -1; next_match_idx = -1;
} else } else
cur_si = push_next_match(cur_si); cur_si = push_next_match(cur_si);

View File

@@ -1995,8 +1995,7 @@ static garray_T tag_fnames = GA_EMPTY_INIT_VALUE;
*/ */
static void found_tagfile_cb(char_u *fname, void *cookie) static void found_tagfile_cb(char_u *fname, void *cookie)
{ {
ga_grow(&tag_fnames, 1); GA_APPEND(char_u *, &tag_fnames, vim_strsave(fname));
((char_u **)(tag_fnames.ga_data))[tag_fnames.ga_len++] = vim_strsave(fname);
} }
#if defined(EXITFREE) || defined(PROTO) #if defined(EXITFREE) || defined(PROTO)

View File

@@ -2249,7 +2249,6 @@ void ex_undolist(exarg_T *eap)
while (uhp != NULL) { while (uhp != NULL) {
if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
&& uhp->uh_walk != mark) { && uhp->uh_walk != mark) {
ga_grow(&ga, 1);
vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ", vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
uhp->uh_seq, changes); uhp->uh_seq, changes);
u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff), u_add_time(IObuff + STRLEN(IObuff), IOSIZE - STRLEN(IObuff),
@@ -2260,7 +2259,7 @@ void ex_undolist(exarg_T *eap)
vim_snprintf_add((char *)IObuff, IOSIZE, vim_snprintf_add((char *)IObuff, IOSIZE,
" %3ld", uhp->uh_save_nr); " %3ld", uhp->uh_save_nr);
} }
((char_u **)(ga.ga_data))[ga.ga_len++] = vim_strsave(IObuff); GA_APPEND(char_u *, &ga, vim_strsave(IObuff));
} }
uhp->uh_walk = mark; uhp->uh_walk = mark;