countof() is safer, simpler, and is standard (ISO C2y).

Signed-off-by: Alejandro Colomar <[email protected]>
---
 src/libs/libgroff/uniglyph.cpp  | 6 +++---
 src/preproc/eqn/delim.cpp       | 4 +++-
 src/preproc/eqn/text.cpp        | 6 +++---
 src/preproc/pic/lex.cpp         | 3 ++-
 src/preproc/refer/command.cpp   | 5 +++--
 src/preproc/refer/ref.cpp       | 4 +++-
 src/utils/hpftodit/hpuni.cpp    | 6 +++---
 src/utils/tfmtodit/tfmtodit.cpp | 7 ++++---
 8 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/src/libs/libgroff/uniglyph.cpp b/src/libs/libgroff/uniglyph.cpp
index c6b54ed24..3790c3568 100644
--- a/src/libs/libgroff/uniglyph.cpp
+++ b/src/libs/libgroff/uniglyph.cpp
@@ -20,6 +20,8 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
 #include <config.h>
 #endif
 
+#include <stdcountof.h>
+
 #include "lib.h"
 
 #include "stringclass.h"
@@ -431,9 +433,7 @@ static struct unicode_to_glyph_init {
 
 unicode_to_glyph_init::unicode_to_glyph_init()
 {
-  for (unsigned int i = 0;
-       i < sizeof(unicode_to_glyph_list)/sizeof(unicode_to_glyph_list[0]);
-       i++) {
+  for (unsigned int i = 0; i < countof(unicode_to_glyph_list); i++) {
     unicode_to_glyph *utg = new unicode_to_glyph[1];
     utg->value = (char *)unicode_to_glyph_list[i].value;
     unicode_to_glyph_table.define(unicode_to_glyph_list[i].key, utg);
diff --git a/src/preproc/eqn/delim.cpp b/src/preproc/eqn/delim.cpp
index 452826c13..cfbcb2782 100644
--- a/src/preproc/eqn/delim.cpp
+++ b/src/preproc/eqn/delim.cpp
@@ -20,6 +20,8 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
 #include <config.h>
 #endif
 
+#include <stdcountof.h>
+
 #include <assert.h>
 
 #include "eqn.h"
@@ -162,7 +164,7 @@ struct delimiter {
   },
 };
 
-const int DELIM_TABLE_SIZE = int(sizeof(delim_table)/sizeof(delim_table[0]));
+const int DELIM_TABLE_SIZE = int(countof(delim_table));
 
 class delim_box : public box {
 private:
diff --git a/src/preproc/eqn/text.cpp b/src/preproc/eqn/text.cpp
index 59e39e698..5e024f551 100644
--- a/src/preproc/eqn/text.cpp
+++ b/src/preproc/eqn/text.cpp
@@ -20,6 +20,8 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
 #include <config.h>
 #endif
 
+#include <stdcountof.h>
+
 #include <ctype.h>
 #include <stdlib.h>
 #include "eqn.h"
@@ -396,9 +398,7 @@ struct map entity_table[] = {
 const char *special_to_entity(const char *sp)
 {
   struct map *mp;
-  for (mp = entity_table; 
-       mp < entity_table + sizeof(entity_table)/sizeof(entity_table[0]); 
-       mp++) {
+  for (mp = entity_table; mp < entity_table + countof(entity_table); mp++) {
     if (strcmp(mp->from, sp) == 0)
       return mp->to;
   }
diff --git a/src/preproc/pic/lex.cpp b/src/preproc/pic/lex.cpp
index d81781031..50efa7cbd 100644
--- a/src/preproc/pic/lex.cpp
+++ b/src/preproc/pic/lex.cpp
@@ -23,6 +23,7 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
 #include <assert.h>
 #include <errno.h>
 #include <math.h> // pow()
+#include <stdcountof.h>
 #include <stdio.h> // EOF, FILE, fclose(), fopen(), getc(), ungetc()
 #include <string.h> // strerror()
 
@@ -571,7 +572,7 @@ int lookup_keyword(const char *str, int len)
   };
   
   const keyword *start = table;
-  const keyword *end = table + sizeof(table)/sizeof(table[0]);
+  const keyword *end = table + countof(table);
   while (start < end) {
     // start <= target < end
     const keyword *mid = start + (end - start)/2;
diff --git a/src/preproc/refer/command.cpp b/src/preproc/refer/command.cpp
index f24a08111..eef7ca3af 100644
--- a/src/preproc/refer/command.cpp
+++ b/src/preproc/refer/command.cpp
@@ -20,6 +20,8 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
 #include <config.h>
 #endif
 
+#include <stdcountof.h>
+
 #include "refer.h"
 #include "refid.h"
 #include "search.h"
@@ -758,8 +760,7 @@ static int check_args(const char *types, const char *name,
 
 static void execute_command(const char *name, int argc, argument *argv)
 {
-  for (unsigned int i = 0;
-       i < sizeof(command_table)/sizeof(command_table[0]); i++)
+  for (unsigned int i = 0; i < countof(command_table); i++)
     if (strcmp(name, command_table[i].name) == 0) {
       if (check_args(command_table[i].arg_types, name, argc, argv))
        (*command_table[i].func)(argc, argv);
diff --git a/src/preproc/refer/ref.cpp b/src/preproc/refer/ref.cpp
index 0cb9f632e..64e9817a4 100644
--- a/src/preproc/refer/ref.cpp
+++ b/src/preproc/refer/ref.cpp
@@ -20,6 +20,8 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
 #include <config.h>
 #endif
 
+#include <stdcountof.h>
+
 #include "refer.h"
 #include "refid.h"
 #include "ref.h"
@@ -955,7 +957,7 @@ static int find_month(const char *start, const char *end)
     while (ptr < end && csalpha(*ptr))
       ptr++;
     if (ptr - start >= 3) {
-      for (unsigned int i = 0; i < sizeof(months)/sizeof(months[0]); i++) {
+      for (unsigned int i = 0; i < countof(months); i++) {
        const char *q = months[i];
        const char *p = start;
        for (; p < ptr; p++, q++)
diff --git a/src/utils/hpftodit/hpuni.cpp b/src/utils/hpftodit/hpuni.cpp
index b1b17d650..7fc5ad9d0 100644
--- a/src/utils/hpftodit/hpuni.cpp
+++ b/src/utils/hpftodit/hpuni.cpp
@@ -20,6 +20,8 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
 #include <config.h>
 #endif
 
+#include <stdcountof.h>
+
 #include "lib.h"
 
 #include "stringclass.h"
@@ -685,9 +687,7 @@ static struct hp_msl_to_unicode_init {
 } _hp_msl_to_unicode_init;
 
 hp_msl_to_unicode_init::hp_msl_to_unicode_init() {
-  for (unsigned int i = 0;
-       i < sizeof(hp_msl_to_unicode_list)/sizeof(hp_msl_to_unicode_list[0]);
-       i++) {
+  for (unsigned int i = 0; i < countof(hp_msl_to_unicode_list); i++) {
     hp_msl_to_unicode *ptu = new hp_msl_to_unicode[1];
     ptu->value = (char *)hp_msl_to_unicode_list[i].value;
     hp_msl_to_unicode_table.define(hp_msl_to_unicode_list[i].key, ptu);
diff --git a/src/utils/tfmtodit/tfmtodit.cpp b/src/utils/tfmtodit/tfmtodit.cpp
index b8183f635..a86d6e52c 100644
--- a/src/utils/tfmtodit/tfmtodit.cpp
+++ b/src/utils/tfmtodit/tfmtodit.cpp
@@ -53,6 +53,7 @@ both be zero. */
 #include <assert.h>
 #include <errno.h>
 #include <math.h> // atan2()
+#include <stdcountof.h>
 #include <stdlib.h> // exit(), EXIT_SUCCESS, strtol()
 
 #include <getopt.h> // getopt_long()
@@ -807,8 +808,8 @@ int main(int argc, char **argv)
   // Print the list of ligatures.
   // First find the indices of each character that can participate in
   // a ligature.
-  size_t lig_char_entries = sizeof(lig_chars)/sizeof(lig_chars[0]);
-  size_t lig_table_entries = sizeof(lig_table)/sizeof(lig_table[0]);
+  size_t lig_char_entries = countof(lig_chars);
+  size_t lig_table_entries = countof(lig_table);
   for (i = 0; i < 256; i++)
     for (unsigned int j = 0; j < lig_char_entries; j++)
       for (char_list *p = table[i]; p; p = p->next)
@@ -869,7 +870,7 @@ int main(int argc, char **argv)
       m[5] = g.get_right_adjustment(i);
       printf("%s\t%d", p->ch, m[0]*MULTIPLIER);
       int j;
-      for (j = int(sizeof(m)/sizeof(m[0])) - 1; j > 0; j--)
+      for (j = int(countof(m)) - 1; j > 0; j--)
        if (m[j] != 0)
          break;
       for (k = 1; k <= j; k++)
-- 
2.51.0


Reply via email to