Skip to content

Commit

Permalink
patch 8.2.2922: computing array length is done in various ways
Browse files Browse the repository at this point in the history
Problem:    Computing array length is done in various ways.
Solution:   Use ARRAY_LENGTH everywhere. (Ken Takata, closes #8305)
  • Loading branch information
k-takata authored and brammool committed Jun 2, 2021
1 parent b54abee commit eeec254
Show file tree
Hide file tree
Showing 43 changed files with 80 additions and 90 deletions.
4 changes: 1 addition & 3 deletions src/arabic.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,6 @@ static struct achar {

#define a_BYTE_ORDER_MARK 0xfeff

#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

/*
* Find the struct achar pointer to the given Arabic char.
* Returns NULL if not found.
Expand All @@ -175,7 +173,7 @@ find_achar(int c)
int h, m, l;

// using binary search to find c
h = ARRAY_SIZE(achars);
h = ARRAY_LENGTH(achars);
l = 0;
while (l < h)
{
Expand Down
2 changes: 0 additions & 2 deletions src/blowfish.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

#if defined(FEAT_CRYPT) || defined(PROTO)

#define ARRAY_LENGTH(A) (sizeof(A)/sizeof(A[0]))

#define BF_BLOCK 8
#define BF_BLOCK_MASK 7
#define BF_MAX_CFB_LEN (8 * BF_BLOCK)
Expand Down
2 changes: 1 addition & 1 deletion src/cindent.c
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ cin_isinit(void)
{
int i, l;

for (i = 0; i < (int)(sizeof(skip) / sizeof(char *)); ++i)
for (i = 0; i < (int)ARRAY_LENGTH(skip); ++i)
{
l = (int)strlen(skip[i]);
if (cin_starts_with(s, skip[i]))
Expand Down
2 changes: 1 addition & 1 deletion src/cmdexpand.c
Original file line number Diff line number Diff line change
Expand Up @@ -2155,7 +2155,7 @@ ExpandFromContext(
// Find a context in the table and call the ExpandGeneric() with the
// right function to do the expansion.
ret = FAIL;
for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
for (i = 0; i < (int)ARRAY_LENGTH(tab); ++i)
if (xp->xp_context == tab[i].context)
{
if (tab[i].ic)
Expand Down
2 changes: 1 addition & 1 deletion src/cmdhist.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ get_history_arg(expand_T *xp UNUSED, int idx)
static char_u compl[2] = { NUL, NUL };
char *short_names = ":=@>?/";
int short_names_count = (int)STRLEN(short_names);
int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
int history_name_count = ARRAY_LENGTH(history_names) - 1;

if (idx < short_names_count)
{
Expand Down
5 changes: 2 additions & 3 deletions src/dosinst.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct choice
struct choice choices[30]; // choices the user can make
int choice_count = 0; // number of choices available

#define TABLE_SIZE(s) (int)(sizeof(s) / sizeof(*s))
#define TABLE_SIZE(s) (int)ARRAYSIZE(s)

enum
{
Expand Down Expand Up @@ -1527,8 +1527,7 @@ register_openwith(
"*\\OpenWithList\\gvim.exe",
};

for (i = 0; ERROR_SUCCESS == lRet
&& i < sizeof(openwith) / sizeof(openwith[0]); i++)
for (i = 0; ERROR_SUCCESS == lRet && i < ARRAYSIZE(openwith); i++)
lRet = reg_create_key_and_value(hRootKey, openwith[i], NULL, "", flag);
}

Expand Down
2 changes: 1 addition & 1 deletion src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ compare_func_name(const void *s1, const void *s2)
static void
sortFunctions(void)
{
int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
int funcCnt = (int)ARRAY_LENGTH(functions) - 1;

qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
}
Expand Down
4 changes: 2 additions & 2 deletions src/evalfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,7 @@ get_function_name(expand_T *xp, int idx)
return name;
}
}
if (++intidx < (int)(sizeof(global_functions) / sizeof(funcentry_T)))
if (++intidx < (int)ARRAY_LENGTH(global_functions))
{
STRCPY(IObuff, global_functions[intidx].f_name);
STRCAT(IObuff, "(");
Expand Down Expand Up @@ -1923,7 +1923,7 @@ find_internal_func_opt(char_u *name, int implemented)
int cmp;
int x;

last = (int)(sizeof(global_functions) / sizeof(funcentry_T)) - 1;
last = (int)ARRAY_LENGTH(global_functions) - 1;

// Find the function name in the table. Binary search.
while (first <= last)
Expand Down
6 changes: 3 additions & 3 deletions src/ex_docmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3735,7 +3735,7 @@ modifier_len(char_u *cmd)

if (VIM_ISDIGIT(*cmd))
p = skipwhite(skipdigits(cmd + 1));
for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
{
for (j = 0; p[j] != NUL; ++j)
if (p[j] != cmdmods[i].name[j])
Expand All @@ -3762,7 +3762,7 @@ cmd_exists(char_u *name)
char_u *p;

// Check command modifiers.
for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
for (i = 0; i < (int)ARRAY_LENGTH(cmdmods); ++i)
{
for (j = 0; name[j] != NUL; ++j)
if (name[j] != cmdmods[i].name[j])
Expand Down Expand Up @@ -8732,7 +8732,7 @@ find_cmdline_var(char_u *src, int *usedlen)
#endif
};

for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
for (i = 0; i < (int)ARRAY_LENGTH(spec_str); ++i)
{
len = (int)STRLEN(spec_str[i]);
if (STRNCMP(src, spec_str[i], len) == 0)
Expand Down
2 changes: 1 addition & 1 deletion src/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -5073,7 +5073,7 @@ vim_tempname(
/*
* Try the entries in TEMPDIRNAMES to create the temp directory.
*/
for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
for (i = 0; i < (int)ARRAY_LENGTH(tempdirs); ++i)
{
# ifndef HAVE_MKDTEMP
size_t itmplen;
Expand Down
2 changes: 1 addition & 1 deletion src/gui_athena.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ get_toolbar_pixmap(vimmenu_T *menu, Pixmap *sen)
if (menu->icon_builtin || gui_find_bitmap(menu->name, buf, "xpm") == FAIL)
{
if (menu->iconidx >= 0 && menu->iconidx
< (int)(sizeof(built_in_pixmaps) / sizeof(built_in_pixmaps[0])))
< (int)ARRAY_LENGTH(built_in_pixmaps))
xpm = built_in_pixmaps[menu->iconidx];
else
xpm = tb_blank_xpm;
Expand Down
6 changes: 3 additions & 3 deletions src/gui_gtk_x11.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ static const GtkTargetEntry selection_targets[] =
{"TEXT", 0, TARGET_TEXT},
{"STRING", 0, TARGET_STRING}
};
#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
#define N_SELECTION_TARGETS ARRAY_LENGTH(selection_targets)

#ifdef FEAT_DND
/*
Expand All @@ -149,7 +149,7 @@ static const GtkTargetEntry dnd_targets[] =
{"STRING", 0, TARGET_STRING},
{"text/plain", 0, TARGET_TEXT_PLAIN}
};
# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
# define N_DND_TARGETS ARRAY_LENGTH(dnd_targets)
#endif


Expand Down Expand Up @@ -6853,7 +6853,7 @@ mch_set_mouse_shape(int shape)
else
id &= ~1; // they are always even (why?)
}
else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
else if (shape < (int)ARRAY_LENGTH(mshape_ids))
id = mshape_ids[shape];
else
return;
Expand Down
2 changes: 1 addition & 1 deletion src/gui_haiku.cc
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ static struct specialkey
{0, 0, 0}
};

#define NUM_SPECIAL_KEYS (sizeof(special_keys)/sizeof(special_keys[0]))
#define NUM_SPECIAL_KEYS ARRAY_LENGTH(special_keys)

// ---------------- VimApp ----------------

Expand Down
1 change: 0 additions & 1 deletion src/gui_photon.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
# define PhImage_t int
#endif

#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(a[0]))
#define RGB(r, g, b) PgRGB(r, g, b)

#define EVENT_BUFFER_SIZE sizeof(PhEvent_t) + 1000
Expand Down
4 changes: 2 additions & 2 deletions src/gui_w32.c
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ gui_mch_get_color(char_u *name)
/*
* Try to look up a system colour.
*/
for (i = 0; i < sizeof(sys_table) / sizeof(sys_table[0]); i++)
for (i = 0; i < ARRAY_LENGTH(sys_table); i++)
if (STRICMP(name, sys_table[i].name) == 0)
return GetSysColor(sys_table[i].color);

Expand Down Expand Up @@ -5077,7 +5077,7 @@ gui_mch_do_spawn(char_u *arg)
/*
* Parse the GUI related command-line arguments. Any arguments used are
* deleted from argv, and *argc is decremented accordingly. This is called
* when vim is started, whether or not the GUI has been started.
* when Vim is started, whether or not the GUI has been started.
*/
void
gui_mch_prepare(int *argc, char **argv)
Expand Down
2 changes: 1 addition & 1 deletion src/gui_xmebw.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ set_pixmap(XmEnhancedButtonWidget eb)
attr.valuemask = XpmColorSymbols | XpmCloseness | XpmColorKey;
attr.closeness = 65535; // accuracy isn't crucial
attr.colorsymbols = color;
attr.numsymbols = sizeof(color) / sizeof(color[0]);
attr.numsymbols = ARRAY_LENGTH(color);
attr.color_key = XPM_MONO;
status = XpmCreatePixmapFromData(dpy, root, data, &pix, &mask, &attr);

Expand Down
26 changes: 12 additions & 14 deletions src/hardcopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,8 +972,6 @@ hardcopy_line(
* http://www.adobe.com
*/

#define NUM_ELEMENTS(arr) (sizeof(arr)/sizeof((arr)[0]))

#define PRT_PS_DEFAULT_DPI (72) // Default user space resolution
#define PRT_PS_DEFAULT_FONTSIZE (10)
#define PRT_PS_DEFAULT_BUFFER_SIZE (80)
Expand All @@ -985,7 +983,7 @@ struct prt_mediasize_S
float height;
};

#define PRT_MEDIASIZE_LEN (sizeof(prt_mediasize) / sizeof(struct prt_mediasize_S))
#define PRT_MEDIASIZE_LEN ARRAY_LENGTH(prt_mediasize)

static struct prt_mediasize_S prt_mediasize[] =
{
Expand Down Expand Up @@ -1210,33 +1208,33 @@ struct prt_ps_mbfont_S
static struct prt_ps_mbfont_S prt_ps_mbfonts[] =
{
{
NUM_ELEMENTS(j_encodings),
ARRAY_LENGTH(j_encodings),
j_encodings,
NUM_ELEMENTS(j_charsets),
ARRAY_LENGTH(j_charsets),
j_charsets,
"jis_roman",
"JIS_X_1983"
},
{
NUM_ELEMENTS(sc_encodings),
ARRAY_LENGTH(sc_encodings),
sc_encodings,
NUM_ELEMENTS(sc_charsets),
ARRAY_LENGTH(sc_charsets),
sc_charsets,
"gb_roman",
"GB_2312-80"
},
{
NUM_ELEMENTS(tc_encodings),
ARRAY_LENGTH(tc_encodings),
tc_encodings,
NUM_ELEMENTS(tc_charsets),
ARRAY_LENGTH(tc_charsets),
tc_charsets,
"cns_roman",
"BIG5"
},
{
NUM_ELEMENTS(k_encodings),
ARRAY_LENGTH(k_encodings),
k_encodings,
NUM_ELEMENTS(k_charsets),
ARRAY_LENGTH(k_charsets),
k_charsets,
"ks_roman",
"KS_X_1992"
Expand Down Expand Up @@ -1793,12 +1791,12 @@ prt_next_dsc(struct prt_dsc_line_S *p_dsc_line)
return FALSE;

// Find type of DSC comment
for (comment = 0; comment < (int)NUM_ELEMENTS(prt_dsc_table); comment++)
for (comment = 0; comment < (int)ARRAY_LENGTH(prt_dsc_table); comment++)
if (prt_resfile_strncmp(0, prt_dsc_table[comment].string,
prt_dsc_table[comment].len) == 0)
break;

if (comment != NUM_ELEMENTS(prt_dsc_table))
if (comment != ARRAY_LENGTH(prt_dsc_table))
{
// Return type of comment
p_dsc_line->type = prt_dsc_table[comment].type;
Expand Down Expand Up @@ -2385,7 +2383,7 @@ mch_print_init(
int cmap_first = 0;

p_mbenc_first = NULL;
for (cmap = 0; cmap < (int)NUM_ELEMENTS(prt_ps_mbfonts); cmap++)
for (cmap = 0; cmap < (int)ARRAY_LENGTH(prt_ps_mbfonts); cmap++)
if (prt_match_encoding((char *)p_encoding, &prt_ps_mbfonts[cmap],
&p_mbenc))
{
Expand Down
2 changes: 1 addition & 1 deletion src/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ find_help_tags(
// When the string starting with "expr-" and containing '?' and matches
// the table, it is taken literally (but ~ is escaped). Otherwise '?'
// is recognized as a wildcard.
for (i = (int)(sizeof(expr_table) / sizeof(char *)); --i >= 0; )
for (i = (int)ARRAY_LENGTH(expr_table); --i >= 0; )
if (STRCMP(arg + 5, expr_table[i]) == 0)
{
int si = 0, di = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ do_highlight(
off = 0;
while (arg[off] != NUL)
{
for (i = sizeof(hl_attr_table) / sizeof(int); --i >= 0; )
for (i = ARRAY_LENGTH(hl_attr_table); --i >= 0; )
{
len = (int)STRLEN(hl_name_table[i]);
if (STRNICMP(arg + off, hl_name_table[i], len) == 0)
Expand Down Expand Up @@ -1168,7 +1168,7 @@ do_highlight(

// reduce calls to STRICMP a bit, it can be slow
off = TOUPPER_ASC(*arg);
for (i = (sizeof(color_names) / sizeof(char *)); --i >= 0; )
for (i = ARRAY_LENGTH(color_names); --i >= 0; )
if (off == color_names[i][0]
&& STRICMP(arg + 1, color_names[i] + 1) == 0)
break;
Expand Down
2 changes: 1 addition & 1 deletion src/if_mzsch.c
Original file line number Diff line number Diff line change
Expand Up @@ -3799,7 +3799,7 @@ make_modules(void)
mod = scheme_primitive_module(vimext_symbol, environment);
MZ_GC_CHECK();
// all prims made closed so they can access their own names
for (i = 0; i < (int)(sizeof(prims)/sizeof(prims[0])); i++)
for (i = 0; i < (int)ARRAY_LENGTH(prims); i++)
{
Vim_Prim *prim = prims + i;
closed_prim = scheme_make_closed_prim_w_arity(prim->prim, prim, prim->name,
Expand Down
3 changes: 3 additions & 0 deletions src/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,6 @@
#ifndef MAX
# define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif

// Length of the array.
#define ARRAY_LENGTH(a) (sizeof(a) / sizeof((a)[0]))
6 changes: 3 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ vim_main2(void)
#endif

/*
* When done something that is not allowed or error message call
* When done something that is not allowed or given an error message call
* wait_return. This must be done before starttermcap(), because it may
* switch to another screen. It must be done after settmode(TMODE_RAW),
* because we want to react on a single key stroke.
Expand Down Expand Up @@ -1662,7 +1662,7 @@ getout(int exitval)
{
// give the user a chance to read the (error) message
no_wait_return = FALSE;
wait_return(FALSE);
// wait_return(FALSE);
}

// Position the cursor again, the autocommands may have moved it
Expand Down Expand Up @@ -3435,7 +3435,7 @@ usage(void)
{
mch_msg(_(" vim [arguments] "));
mch_msg(_(use[i]));
if (i == (sizeof(use) / sizeof(char_u *)) - 1)
if (i == ARRAY_LENGTH(use) - 1)
break;
mch_msg(_("\n or:"));
}
Expand Down
5 changes: 2 additions & 3 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -2478,13 +2478,12 @@ init_mappings(void)
if (!gui.starting)
# endif
{
for (i = 0;
i < (int)(sizeof(cinitmappings) / sizeof(struct initmap)); ++i)
for (i = 0; i < (int)ARRAY_LENGTH(cinitmappings); ++i)
add_map(cinitmappings[i].arg, cinitmappings[i].mode);
}
# endif
# if defined(FEAT_GUI_MSWIN) || defined(MACOS_X)
for (i = 0; i < (int)(sizeof(initmappings) / sizeof(struct initmap)); ++i)
for (i = 0; i < (int)ARRAY_LENGTH(initmappings); ++i)
add_map(initmappings[i].arg, initmappings[i].mode);
# endif
#endif
Expand Down

0 comments on commit eeec254

Please sign in to comment.