From: Collin Funk Date: Wed, 24 Sep 2025 02:17:14 +0000 (-0700) Subject: maint: prefer countof over ARRAY_CARDINALITY X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1bc5ec61d8d960e1679b42f55c8f96992ced34ef;p=thirdparty%2Fcoreutils.git 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. --- diff --git a/.gitignore b/.gitignore index 87eacf621d..756f2d8740 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/bootstrap.conf b/bootstrap.conf index b8feef90a1..adf09910de 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -255,6 +255,7 @@ gnulib_modules=" stdc_leading_zeros stdc_trailing_zeros stdckdint-h + stdcountof-h stddef-h stdlib-safer stpcpy diff --git a/src/csplit.c b/src/csplit.c index de499f166a..d2a928ae69 100644 --- a/src/csplit.c +++ b/src/csplit.c @@ -1392,7 +1392,7 @@ main (int argc, char **argv) SIGXFSZ, #endif }; - enum { nsigs = ARRAY_CARDINALITY (sig) }; + enum { nsigs = countof (sig) }; struct sigaction act; diff --git a/src/df.c b/src/df.c index 857f76cf4a..75e638c5e9 100644 --- a/src/df.c +++ b/src/df.c @@ -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)) { diff --git a/src/digest.c b/src/digest.c index 64c2c49b1d..ce0e222e1b 100644 --- a/src/digest.c +++ b/src/digest.c @@ -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; diff --git a/src/dircolors.c b/src/dircolors.c index cb6714dbfd..19e815953f 100644 --- a/src/dircolors.c +++ b/src/dircolors.c @@ -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; diff --git a/src/factor.c b/src/factor.c index 534490e9b2..02b02a9777 100644 --- a/src/factor.c +++ b/src/factor.c @@ -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 { diff --git a/src/join.c b/src/join.c index 28d9dd3ee3..5cf87d1e8e 100644 --- a/src/join.c +++ b/src/join.c @@ -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]) { diff --git a/src/ls.c b/src/ls.c index d999d5bdf1..4470894220 100644 --- a/src/ls.c +++ b/src/ls.c @@ -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 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 diff --git a/src/od.c b/src/od.c index f8937298fb..946b6517f8 100644 --- a/src/od.c +++ b/src/od.c @@ -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))) diff --git a/src/sort.c b/src/sort.c index 5a6a963f35..0212695b13 100644 --- a/src/sort.c +++ b/src/sort.c @@ -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; diff --git a/src/stdbuf.c b/src/stdbuf.c index 11b9a12f27..c3d72ce5f5 100644 --- a/src/stdbuf.c +++ b/src/stdbuf.c @@ -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++; diff --git a/src/system.h b/src/system.h index a7ae5a5f3b..b8612ff5e3 100644 --- a/src/system.h +++ b/src/system.h @@ -68,6 +68,7 @@ #endif #include +#include #include #include #include @@ -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 diff --git a/src/tr.c b/src/tr.c index 87daa94712..f172ced86d 100644 --- a/src/tr.c +++ b/src/tr.c @@ -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;