1
0
mirror of https://git.savannah.gnu.org/git/coreutils.git synced 2026-10-24 02:08:57 +03:00

maint: prefer countof over ARRAY_CARDINALITY

* bootstrap.conf (gnulib_modules): Add stdcountof-h.
* src/system.h: Include stdcountof.h.
(ARRAY_CARDINALITY): Remove definition.
* .gitignore (/lib/stdcountof.h): Ignore Gnulib generated file.
* src/csplit.c: Use countof instead of ARRAY_CARDINALITY.
* src/df.c: Likewise.
* src/digest.c: Likewise.
* src/dircolors.c: Likewise.
* src/factor.c: Likewise.
* src/join.c: Likewise.
* src/ls.c: Likewise.
* src/od.c: Likewise.
* src/sort.c: Likewise.
* src/stdbuf.c: Likewise.
* src/tr.c: Likewise.
This commit is contained in:
Collin Funk
2025-09-23 19:17:14 -07:00
parent 914972e80d
commit 1bc5ec61d8
14 changed files with 23 additions and 28 deletions

1
.gitignore vendored
View File

@@ -109,6 +109,7 @@
/lib/stdarg.h
/lib/stdbool.h
/lib/stdbit.h
/lib/stdcountof.h
/lib/stdckdint.h
/lib/stddef.h
/lib/stdint.h

View File

@@ -255,6 +255,7 @@ gnulib_modules="
stdc_leading_zeros
stdc_trailing_zeros
stdckdint-h
stdcountof-h
stddef-h
stdlib-safer
stpcpy

View File

@@ -1392,7 +1392,7 @@ main (int argc, char **argv)
SIGXFSZ,
#endif
};
enum { nsigs = ARRAY_CARDINALITY (sig) };
enum { nsigs = countof (sig) };
struct sigaction act;

View File

@@ -441,7 +441,7 @@ decode_output_arg (char const *arg)
/* process S. */
display_field_t field = INVALID_FIELD;
for (idx_t i = 0; i < ARRAY_CARDINALITY (field_data); i++)
for (idx_t i = 0; i < countof (field_data); i++)
{
if (streq (field_data[i].arg, s))
{

View File

@@ -376,8 +376,7 @@ static int const algorithm_bits[] =
512, 512, 512, 256, 0
};
static_assert (ARRAY_CARDINALITY (algorithm_bits)
== ARRAY_CARDINALITY (algorithm_args));
static_assert (countof (algorithm_bits) == countof (algorithm_args));
static bool algorithm_specified = false;
static enum Algorithm cksum_algorithm = crc;

View File

@@ -67,7 +67,7 @@ static char const *const ls_codes[] =
"su", "su", "sg", "sg", "st", "ow", "ow", "tw", "tw", "ca", "mh", "cl",
nullptr
};
static_assert (ARRAY_CARDINALITY (slack_codes) == ARRAY_CARDINALITY (ls_codes));
static_assert (countof (slack_codes) == countof (ls_codes));
/* Whether to output escaped ls color codes for display. */
static bool print_ls_colors;

View File

@@ -720,7 +720,7 @@ static int_least16_t const primes_ptab[] = {
};
#undef P
enum { PRIMES_PTAB_ENTRIES = ARRAY_CARDINALITY (primes_ptab) - 8 + 1 };
enum { PRIMES_PTAB_ENTRIES = countof (primes_ptab) - 8 + 1 };
struct primes_dtab
{

View File

@@ -488,7 +488,7 @@ get_line (FILE *fp, struct line **linep, int which)
static void
free_spareline (void)
{
for (idx_t i = 0; i < ARRAY_CARDINALITY (spareline); i++)
for (idx_t i = 0; i < countof (spareline); i++)
{
if (spareline[i])
{

View File

@@ -180,7 +180,7 @@ enum { filetype_cardinality = arg_directory + 1 };
Keep these in sync with enum filetype. */
static char const filetype_letter[] =
{'?', 'p', 'c', 'd', 'b', '-', 'l', 's', 'w', 'd'};
static_assert (ARRAY_CARDINALITY (filetype_letter) == filetype_cardinality);
static_assert (countof (filetype_letter) == filetype_cardinality);
/* Map enum filetype to <dirent.h> d_type values. */
static unsigned char const filetype_d_type[] =
@@ -188,7 +188,7 @@ static unsigned char const filetype_d_type[] =
DT_UNKNOWN, DT_FIFO, DT_CHR, DT_DIR, DT_BLK, DT_REG, DT_LNK, DT_SOCK,
DT_WHT, DT_DIR
};
static_assert (ARRAY_CARDINALITY (filetype_d_type) == filetype_cardinality);
static_assert (countof (filetype_d_type) == filetype_cardinality);
/* Map d_type values to enum filetype. */
static char const d_type_filetype[UCHAR_MAX + 1] =
@@ -1621,7 +1621,7 @@ signal_setup (bool init)
SIGXFSZ,
#endif
};
enum { nsigs = ARRAY_CARDINALITY (sig) };
enum { nsigs = countof (sig) };
#if ! SA_NOCLDSTOP
static bool caught_sig[nsigs];
@@ -1705,8 +1705,7 @@ main (int argc, char **argv)
initialize_exit_failure (LS_FAILURE);
atexit (close_stdout);
static_assert (ARRAY_CARDINALITY (color_indicator)
== ARRAY_CARDINALITY (indicator_name));
static_assert (countof (color_indicator) == countof (indicator_name));
exit_status = EXIT_SUCCESS;
print_dir_name = true;
@@ -2823,7 +2822,7 @@ parse_ls_color (void)
state = PS_FAIL; /* Assume failure... */
if (*(p++) == '=')/* It *should* be... */
{
for (int i = 0; i < ARRAY_CARDINALITY (indicator_name); i++)
for (int i = 0; i < countof (indicator_name); i++)
{
if ((label0 == indicator_name[i][0])
&& (label1 == indicator_name[i][1]))
@@ -4036,8 +4035,7 @@ static qsortFunc const sort_functions[][2][2][2] =
This line verifies at compile-time that the array of sort functions has been
initialized for all possible sort keys. */
static_assert (ARRAY_CARDINALITY (sort_functions)
== sort_numtypes - 2 + time_numtypes);
static_assert (countof (sort_functions) == sort_numtypes - 2 + time_numtypes);
/* Set up SORTED_FILE to point to the in-use entries in CWD_FILE, in order. */
@@ -4986,8 +4984,7 @@ get_color_indicator (const struct fileinfo *f, bool symlink_target)
C_ORPHAN, C_FIFO, C_CHR, C_DIR, C_BLK, C_FILE,
C_LINK, C_SOCK, C_FILE, C_DIR
};
static_assert (ARRAY_CARDINALITY (filetype_indicator)
== filetype_cardinality);
static_assert (countof (filetype_indicator) == filetype_cardinality);
type = filetype_indicator[f->filetype];
}
else

View File

@@ -167,7 +167,7 @@ static const int width_bytes[] =
/* Ensure that for each member of 'enum size_spec' there is an
initializer in the width_bytes array. */
static_assert (ARRAY_CARDINALITY (width_bytes) == N_SIZE_SPECS);
static_assert (countof (width_bytes) == N_SIZE_SPECS);
/* Names for some non-printing characters. */
static char const charname[33][4] =
@@ -761,7 +761,7 @@ decode_one_format (char const *s_orig, char const *s, char const **next,
size = sizeof (unsigned int);
else
{
if (ARRAY_CARDINALITY (integral_type_size) <= size
if (countof (integral_type_size) <= size
|| integral_type_size[size] == NO_SIZE)
{
error (0, 0, _("invalid type string %s;\nthis system"
@@ -873,7 +873,7 @@ decode_one_format (char const *s_orig, char const *s, char const **next,
size = sizeof (double);
else
{
if (ARRAY_CARDINALITY (fp_type_size) <= size
if (countof (fp_type_size) <= size
|| fp_type_size[size] == NO_SIZE
|| (! FLOAT16_SUPPORTED && BF16_SUPPORTED
&& size == sizeof (bfloat16)))

View File

@@ -4416,7 +4416,7 @@ main (int argc, char **argv)
SIGXFSZ,
#endif
};
enum { nsigs = ARRAY_CARDINALITY (sig) };
enum { nsigs = countof (sig) };
#if SA_NOCLDSTOP
struct sigaction act;

View File

@@ -279,7 +279,7 @@ set_libstdbuf_options (void)
{
bool env_set = false;
for (size_t i = 0; i < ARRAY_CARDINALITY (stdbuf); i++)
for (size_t i = 0; i < countof (stdbuf); i++)
{
if (stdbuf[i].optarg)
{
@@ -333,7 +333,7 @@ main (int argc, char **argv)
case 'i':
case 'o':
opt_fileno = optc_to_fileno (c);
affirm (0 <= opt_fileno && opt_fileno < ARRAY_CARDINALITY (stdbuf));
affirm (0 <= opt_fileno && opt_fileno < countof (stdbuf));
stdbuf[opt_fileno].optc = c;
while (c_isspace (*optarg))
optarg++;

View File

@@ -68,6 +68,7 @@
#endif
#include <stdckdint.h>
#include <stdcountof.h>
#include <stddef.h>
#include <string.h>
#include <uchar.h>
@@ -770,10 +771,6 @@ stzncpy (char *restrict dest, char const *restrict src, size_t len)
return dest;
}
#ifndef ARRAY_CARDINALITY
# define ARRAY_CARDINALITY(Array) (sizeof (Array) / sizeof *(Array))
#endif
/* Return true if ERR is ENOTSUP or EOPNOTSUPP, otherwise false.
This wrapper function avoids the redundant 'or'd comparison on
systems like Linux for which they have the same value. It also

View File

@@ -545,7 +545,7 @@ look_up_char_class (char const *class_str, size_t len)
{
enum Char_class i;
for (i = 0; i < ARRAY_CARDINALITY (char_class_name); i++)
for (i = 0; i < countof (char_class_name); i++)
if (STREQ_LEN (class_str, char_class_name[i], len)
&& strlen (char_class_name[i]) == len)
return i;