gbranden pushed a commit to branch master
in repository groff.

commit 59bd04008581a73da9e030ee15a0edcaaa18e0e9
Author: Alejandro Colomar <[email protected]>
AuthorDate: Sun Dec 7 16:56:18 2025 +0100

    Use `countof()` instead of `sizeof` and division.
    
    * src/libs/libgroff/uniglyph.cpp:
    * src/preproc/eqn/delim.cpp:
    * src/preproc/eqn/text.cpp:
    * src/preproc/pic/lex.cpp:
    * src/preproc/refer/command.cpp:
    * src/preproc/refer/ref.cpp:
    * src/utils/hpftodit/hpuni.cpp:
    * src/utils/tfmtodit/tfmtodit.cpp: Include "stdcountof.h" header file.
    
    * src/preproc/eqn/delim.cpp: Initialize global constant
      `DELIM_TABLE_SIZE` using `countof()` instead of `sizeof` and division.
    
    * src/libs/libgroff/uniglyph.cpp
      (unicode_to_glyph_init::unicode_decompose_init):
    * src/preproc/eqn/text.cpp (special_to_entity):
    * src/preproc/pic/lex.cpp (lookup_keyword):
    * src/preproc/refer/command.cpp (execute_command):
    * src/preproc/refer/ref.cpp (find_month):
    * src/utils/hpftodit/hpuni.cpp
      (hp_msl_to_unicode_init::hp_msl_to_unicode_init):
    * src/utils/tfmtodit/tfmtodit.cpp (main): Use `countof()` instead of
      `sizeof` and division.
    
    countof() is [expected to be --GBR] standard (ISO C2y).
    
    Continues the long process of fixing Savannah #66518.
    
    Signed-off-by: Alejandro Colomar <[email protected]>
---
 ChangeLog                       | 31 +++++++++++++++++++++++++++++++
 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 ++++---
 9 files changed, 55 insertions(+), 17 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 8f295669c..f8e137783 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,34 @@
+2025-12-07  Alejandro Colomar <[email protected]>
+
+       * src/libs/libgroff/uniglyph.cpp:
+       * src/preproc/eqn/delim.cpp:
+       * src/preproc/eqn/text.cpp:
+       * src/preproc/pic/lex.cpp:
+       * src/preproc/refer/command.cpp:
+       * src/preproc/refer/ref.cpp:
+       * src/utils/hpftodit/hpuni.cpp:
+       * src/utils/tfmtodit/tfmtodit.cpp: Include "stdcountof.h" header
+       file.
+
+       * src/preproc/eqn/delim.cpp: Initialize global constant
+       `DELIM_TABLE_SIZE` using `countof()` instead of `sizeof` and
+       division.
+
+       * src/libs/libgroff/uniglyph.cpp
+       (unicode_to_glyph_init::unicode_decompose_init):
+       * src/preproc/eqn/text.cpp (special_to_entity):
+       * src/preproc/pic/lex.cpp (lookup_keyword):
+       * src/preproc/refer/command.cpp (execute_command):
+       * src/preproc/refer/ref.cpp (find_month):
+       * src/utils/hpftodit/hpuni.cpp
+       (hp_msl_to_unicode_init::hp_msl_to_unicode_init):
+       * src/utils/tfmtodit/tfmtodit.cpp (main): Use `countof()`
+       instead of `sizeof` and division.
+
+       countof() is [expected to be --GBR] standard (ISO C2y).
+
+       Continues the long process of fixing Savannah #66518.
+
 2025-12-07  Alejandro Colomar <[email protected]>
 
        * src/utils/hpftodit/hpftodit.cpp: Include "stdcountof.h" header
diff --git a/src/libs/libgroff/uniglyph.cpp b/src/libs/libgroff/uniglyph.cpp
index 0dbb8232b..b39ffd67c 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 6d27d427e..547995d98 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 f76d6727b..4d6a33513 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 fc6398116..ede78c8b6 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 fcadf3fb8..4ae2901bd 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 09b407090..778af6f22 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 58fce5db4..f0dcf5200 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 580fbfc5a..25aa5d3e6 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()
@@ -812,8 +813,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)
@@ -874,7 +875,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++)

_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit

Reply via email to