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

View File

@ -5446,11 +5446,9 @@ helptags_one (
ga_init(&ga, (int)sizeof(char_u *), 100);
if (add_help_tags || path_full_compare((char_u *)"$VIMRUNTIME/doc",
dir, FALSE) == kEqualFiles) {
ga_grow(&ga, 1);
s = xmalloc(18 + STRLEN(tagfname));
sprintf((char *)s, "help-tags\t%s\t1\n", tagfname);
((char_u **)ga.ga_data)[ga.ga_len] = s;
++ga.ga_len;
GA_APPEND(char_u *, &ga, s);
}
/*
@ -5517,10 +5515,8 @@ helptags_one (
|| s[1] == '\0')) {
*p2 = '\0';
++p1;
ga_grow(&ga, 1);
s = xmalloc((p2 - p1) + STRLEN(fname) + 2);
((char_u **)ga.ga_data)[ga.ga_len] = s;
++ga.ga_len;
GA_APPEND(char_u *, &ga, s);
sprintf((char *)s, "%s\t%s", p1, fname);
/* 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);
while (*str != NUL) {
ga_grow(gap, 1);
((char_u **)gap->ga_data)[gap->ga_len++] = str;
GA_APPEND(char_u *, gap, str);
/* Isolate one argument, change it in-place, put a NUL after it. */
str = do_one_arg(str);
@ -3332,13 +3331,12 @@ static char_u **find_locales(void)
loc = (char_u *)strtok((char *)locale_a, "\n");
while (loc != NULL) {
ga_grow(&locales_ga, 1);
loc = vim_strsave(loc);
((char_u **)locales_ga.ga_data)[locales_ga.ga_len++] = loc;
GA_APPEND(char_u *, &locales_ga, loc);
loc = (char_u *)strtok(NULL, "\n");
}
free(locale_a);
// Guarantee that .ga_data is NULL terminated
ga_grow(&locales_ga, 1);
((char_u **)locales_ga.ga_data)[locales_ga.ga_len] = NULL;
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;
}
ga_grow(&ga, 1);
((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
++ga.ga_len;
GA_APPEND(char_u *, &ga, vim_strnsave(s, (int)(e - s)));
*e = keep;
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)
continue; /* Skip non-string items and empty strings */
ga_grow(&ga, 1);
((char_u **)ga.ga_data)[ga.ga_len] =
vim_strsave(li->li_tv.vval.v_string);
++ga.ga_len;
GA_APPEND(char_u *, &ga, vim_strsave(li->li_tv.vval.v_string));
}
list_unref(retlist);

View File

@ -197,10 +197,7 @@ void ga_concat(garray_T *gap, const char_u *restrict s)
/// @param c
void ga_append(garray_T *gap, char c)
{
ga_grow(gap, 1);
char *str = gap->ga_data;
str[gap->ga_len] = c;
gap->ga_len++;
GA_APPEND(char, gap, c);
}
#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_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
# include "garray.h.generated.h"
#endif

View File

@ -21,16 +21,13 @@ int os_get_usernames(garray_T *users)
ga_init(users, sizeof(char *), 20);
# if defined(HAVE_GETPWENT) && defined(HAVE_PWD_H)
char *user;
struct passwd *pw;
setpwent();
while ((pw = getpwent()) != NULL) {
// pw->pw_name shouldn't be NULL but just in case...
if (pw->pw_name != NULL) {
ga_grow(users, 1);
user = xstrdup(pw->pw_name);
((char **)(users->ga_data))[users->ga_len++] = user;
GA_APPEND(char *, users, xstrdup(pw->pw_name));
}
}
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
? p_path : curbuf->b_p_path;
char_u *buf;
char_u *p;
int len;
buf = xmalloc(MAXPATHL);
@ -639,7 +638,7 @@ static void expand_path_option(char_u *curdir, garray_T *gap)
* "/path/file" + "./subdir" -> "/path/subdir" */
if (curbuf->b_ffname == NULL)
continue;
p = path_tail(curbuf->b_ffname);
char_u *p = path_tail(curbuf->b_ffname);
len = (int)(p - curbuf->b_ffname);
if (len + (int)STRLEN(buf) >= MAXPATHL)
continue;
@ -666,10 +665,7 @@ static void expand_path_option(char_u *curdir, garray_T *gap)
simplify_filename(buf);
}
ga_grow(gap, 1);
p = vim_strsave(buf);
((char_u **)gap->ga_data)[gap->ga_len++] = p;
GA_APPEND(char_u *, gap, vim_strsave(buf));
}
free(buf);
@ -1194,7 +1190,6 @@ addfile (
int flags
)
{
char_u *p;
bool isdir;
/* 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))
return;
/* Make room for another item in the file list. */
ga_grow(gap, 1);
p = xmalloc(STRLEN(f) + 1 + isdir);
char_u *p = xmalloc(STRLEN(f) + 1 + isdir);
STRCPY(p, f);
#ifdef BACKSLASH_IN_FILENAME
@ -1231,7 +1223,7 @@ addfile (
if (isdir && (flags & EW_ADDSLASH))
add_pathsep(p);
#endif
((char_u **)gap->ga_data)[gap->ga_len++] = p;
GA_APPEND(char_u *, gap, p);
}
#endif /* !NO_EXPANDPATH */

View File

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

View File

@ -1963,9 +1963,7 @@ syn_current_attr (
/* Add the index to a list, so that we can check
* later that we don't match it again (and cause an
* endless loop). */
ga_grow(&zero_width_next_ga, 1);
((int *)(zero_width_next_ga.ga_data))
[zero_width_next_ga.ga_len++] = next_match_idx;
GA_APPEND(int, &zero_width_next_ga, next_match_idx);
next_match_idx = -1;
} else
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)
{
ga_grow(&tag_fnames, 1);
((char_u **)(tag_fnames.ga_data))[tag_fnames.ga_len++] = vim_strsave(fname);
GA_APPEND(char_u *, &tag_fnames, vim_strsave(fname));
}
#if defined(EXITFREE) || defined(PROTO)

View File

@ -2249,7 +2249,6 @@ void ex_undolist(exarg_T *eap)
while (uhp != NULL) {
if (uhp->uh_prev.ptr == NULL && uhp->uh_walk != nomark
&& uhp->uh_walk != mark) {
ga_grow(&ga, 1);
vim_snprintf((char *)IObuff, IOSIZE, "%6ld %7ld ",
uhp->uh_seq, changes);
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,
" %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;