]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: prefer countof over ARRAY_CARDINALITY
authorCollin Funk <collin.funk1@gmail.com>
Wed, 24 Sep 2025 02:17:14 +0000 (19:17 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Thu, 25 Sep 2025 00:31:57 +0000 (17:31 -0700)
* 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.

14 files changed:
.gitignore
bootstrap.conf
src/csplit.c
src/df.c
src/digest.c
src/dircolors.c
src/factor.c
src/join.c
src/ls.c
src/od.c
src/sort.c
src/stdbuf.c
src/system.h
src/tr.c

index 87eacf621d52dc5cf6198bdf26eddded6afda714..756f2d87409708650885157daf88af6b91b6895d 100644 (file)
 /lib/stdarg.h
 /lib/stdbool.h
 /lib/stdbit.h
+/lib/stdcountof.h
 /lib/stdckdint.h
 /lib/stddef.h
 /lib/stdint.h
index b8feef90a1ed6547ab09df738538cd3d5aeb0ce7..adf09910de8083788bb7868f6d014bdfba28f84c 100644 (file)
@@ -255,6 +255,7 @@ gnulib_modules="
   stdc_leading_zeros
   stdc_trailing_zeros
   stdckdint-h
+  stdcountof-h
   stddef-h
   stdlib-safer
   stpcpy
index de499f166a1991abdb16e0cadfe74cbd2d6416c6..d2a928ae69ea1e4031bbc725018245528c21f5f4 100644 (file)
@@ -1392,7 +1392,7 @@ main (int argc, char **argv)
         SIGXFSZ,
 #endif
       };
-    enum { nsigs = ARRAY_CARDINALITY (sig) };
+    enum { nsigs = countof (sig) };
 
     struct sigaction act;
 
index 857f76cf4ae7c94bf0ed8b80f3ec85ce584cbaa9..75e638c5e902d4a3854a092494b5e9690a0d080a 100644 (file)
--- 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))
             {
index 64c2c49b1da7d772c32b739c805148053a7c0901..ce0e222e1bb746c8bd57e3a240ebd460170aae7d 100644 (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;
index cb6714dbfd3eee899edb76bf1919aeae2bebc2a6..19e815953ff6c361e232b84522de8cc3b18231cb 100644 (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;
index 534490e9b2a32862a07170940de12e66e449e27e..02b02a9777e5db7edffe0d80173a151e30d32d26 100644 (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
 {
index 28d9dd3ee3bd4842f0575024036044f55c65f600..5cf87d1e8e5e9d03ba1128abf34fc3be9dc69adf 100644 (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])
         {
index d999d5bdf1f829fa15cfb3ceb75f9770503fdaf5..44708942206002075b9d2cde267f118b5219f6f1 100644 (file)
--- 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 <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
index f8937298fb523cae911c484305f50843daa51f9e..946b6517f8efec2cfd07578861f1ec0d1653e4c9 100644 (file)
--- 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)))
index 5a6a963f358b6e14cd3ad218c3b1ce021b4c9d93..0212695b130d3677342e800c8fd08c7d3dca3b64 100644 (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;
index 11b9a12f27c5c07b52baeb348a4ad68ddde1c378..c3d72ce5f54d13a218610b491688c2b24b6972a9 100644 (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++;
index a7ae5a5f3b2b674d305f14fa3ecc77c44a0472c8..b8612ff5e3656b1bb087e3771664bc2d1b1ebeb8 100644 (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
index 87daa947123fcd56a0dba9924c9890d0ff9c86fe..f172ced86d41f3fbeafffea9dba574c478d1dc45 100644 (file)
--- 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;