gbranden pushed a commit to branch master
in repository groff.

commit 01b5c4e3c2cdf1c01c4d38167fdd13168d82f55b
Author: G. Branden Robinson <[email protected]>
AuthorDate: Sat Apr 26 00:18:26 2025 -0500

    src/libs/libgroff/nametoindex.cpp: Refactor.
    
    * src/libs/libgroff/nametoindex.cpp: Fix code style nits.
    
      (character_indexer::ascii_char_glyph)
      (character_indexer::numbered_char_glyph)
      (name_to_glyph): Reorder equality comparisons to avoid inadvertent
      lvalue assignment.
    
      (character_indexer::named_char_glyph)
      (character_indexer::numbered_char_glyph) (name_to_glyph): Parenthesize
      formally complex expressions.
    
      (character_indexer::named_char_glyph): Slightly refactor; replace
      series of individual character comparisons with a `strncmp()` call.
---
 ChangeLog                         | 14 +++++++++
 src/libs/libgroff/nametoindex.cpp | 63 +++++++++++++++++++++++----------------
 src/roff/troff/charinfo.h         |  2 ++
 src/roff/troff/input.cpp          | 12 ++++++--
 4 files changed, 62 insertions(+), 29 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 25de3fb99..85b2e3833 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2025-04-26  G. Branden Robinson <[email protected]>
+
+       * src/libs/libgroff/nametoindex.cpp: Fix code style nits.
+       (character_indexer::ascii_char_glyph)
+       (character_indexer::numbered_char_glyph)
+       (name_to_glyph): Reorder equality comparisons to avoid
+       inadvertent lvalue assignment.
+       (character_indexer::named_char_glyph)
+       (character_indexer::numbered_char_glyph)
+       (name_to_glyph): Parenthesize formally complex expressions.
+       (character_indexer::named_char_glyph): Slightly refactor;
+       replace series of individual character comparisons with a
+       `strncmp()` call.
+
 2025-04-26  G. Branden Robinson <[email protected]>
 
        * src/roff/troff/input.cpp (glyph_to_name): Parenthesize
diff --git a/src/libs/libgroff/nametoindex.cpp 
b/src/libs/libgroff/nametoindex.cpp
index bf97ab981..a4dbc211f 100644
--- a/src/libs/libgroff/nametoindex.cpp
+++ b/src/libs/libgroff/nametoindex.cpp
@@ -1,4 +1,4 @@
-/* Copyright (C) 1989-2024 Free Software Foundation, Inc.
+/* Copyright (C) 1989-2025 Free Software Foundation, Inc.
      Written by James Clark ([email protected])
 
 This file is part of groff.
@@ -22,9 +22,10 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
 
 #include <assert.h>
 #include <ctype.h>
-#include <stdlib.h>
+#include <stdlib.h> // strotol()
+#include <string.h> // memcpy(), strcpy(), strncmp()
 
-#include "lib.h"
+#include "lib.h" // strsave()
 
 #include "errarg.h"
 #include "error.h"
@@ -32,23 +33,26 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
 #include "ptable.h"
 #include "itable.h"
 
-// Every glyphinfo is actually a charinfo.
+// troff has a more elaborate `charinfo` class that stores much more
+// information.  All libgroff and output drivers need is a way to
+// retrieve the object's name as a C string.
 class charinfo : glyph {
 public:
   const char *name;    // The glyph name, or a null pointer.
   friend class character_indexer;
 };
 
-// PTABLE(charinfo) is a hash table mapping 'const char *' to 'charinfo *'.
+// PTABLE(charinfo) is a hash table mapping `const char *` to
+// `charinfo *`.
 declare_ptable(charinfo)
 implement_ptable(charinfo)
 
-// ITABLE(charinfo) is a hash table mapping 'int >= 0' to 'charinfo *'.
+// ITABLE(charinfo) is a hash table mapping `int >= 0` to `charinfo *`.
 declare_itable(charinfo)
 implement_itable(charinfo)
 
-// This class is as a registry storing all named and numbered glyphs known
-// so far, and assigns a unique index to each glyph.
+// This class is a registry storing all named and numbered glyphs known
+// so far, assigning a unique index to each glyph.
 class character_indexer {
 public:
   character_indexer();
@@ -60,8 +64,8 @@ public:
 private:
   int next_index;              // Number of glyphs already allocated.
   PTABLE(charinfo) table;      // Table mapping name to glyph.
-  glyph *ascii_glyph[256];     // Shorthand table for looking up "charNNN"
-                               // glyphs.
+  glyph *ascii_glyph[256];     // Shorthand table for looking up
+                               // "charNNN" glyphs.
   ITABLE(charinfo) ntable;     // Table mapping number to glyph.
   enum { NSMALL = 256 };
   glyph *small_number_glyph[NSMALL]; // Shorthand table for looking up
@@ -82,12 +86,18 @@ character_indexer::~character_indexer()
 {
 }
 
+// Keep this in sync with "src/roff/troff/input.cpp".
+// constexpr // C++11
+static const char char_prefix[] = { 'c', 'h', 'a', 'r' };
+// constexpr // C++11
+static const size_t char_prefix_len = sizeof char_prefix;
+
 glyph *character_indexer::ascii_char_glyph(unsigned char c)
 {
-  if (ascii_glyph[c] == UNDEFINED_GLYPH) {
-    char buf[4+3+1];
-    memcpy(buf, "char", 4);
-    strcpy(buf + 4, i_to_a(c));
+  if (UNDEFINED_GLYPH == ascii_glyph[c]) {
+    char buf[sizeof char_prefix + 3 + 1]; // "char" + nnn + '\0'
+    (void) memcpy(buf, char_prefix, char_prefix_len);
+    (void) strcpy(buf + char_prefix_len, i_to_a(c));
     charinfo *ci = new charinfo;
     ci->index = next_index++;
     ci->number = -1;
@@ -99,16 +109,17 @@ glyph *character_indexer::ascii_char_glyph(unsigned char c)
 
 inline glyph *character_indexer::named_char_glyph(const char *s)
 {
-  // Glyphs with name 'charNNN' are only stored in ascii_glyph[], not
+  // Glyphs with name 'charNNN' are stored only in `ascii_glyph[]`, not
   // in the table.  Therefore treat them specially here.
-  if (s[0] == 'c' && s[1] == 'h' && s[2] == 'a' && s[3] == 'r') {
+  if (strncmp(s, char_prefix, char_prefix_len) == 0) {
     char *val;
-    long n = strtol(s + 4, &val, 10);
-    if (val != s + 4 && *val == '\0' && n >= 0 && n < 256)
+    long n = strtol((s + char_prefix_len), &val, 10);
+    if ((val != (s + char_prefix_len)) && ('\0' == *val)
+       && (n >= 0) && (n < 256))
       return ascii_char_glyph((unsigned char)n);
   }
   charinfo *ci = table.lookupassoc(&s);
-  if (0 == ci) {
+  if (0 /* nullptr */ == ci) {
     ci = new charinfo[1];
     ci->index = next_index++;
     ci->number = -1;
@@ -119,22 +130,22 @@ inline glyph *character_indexer::named_char_glyph(const 
char *s)
 
 inline glyph *character_indexer::numbered_char_glyph(int n)
 {
-  if (n >= 0 && n < NSMALL) {
-    if (small_number_glyph[n] == UNDEFINED_GLYPH) {
+  if ((n >= 0) && (n < NSMALL)) {
+    if (UNDEFINED_GLYPH == small_number_glyph[n]) {
       charinfo *ci = new charinfo;
       ci->index = next_index++;
       ci->number = n;
-      ci->name = 0;
+      ci->name = 0 /* nullptr */;
       small_number_glyph[n] = ci;
     }
     return small_number_glyph[n];
   }
   charinfo *ci = ntable.lookup(n);
-  if (0 == ci) {
+  if (0 /* nullptr */ == ci) {
     ci = new charinfo[1];
     ci->index = next_index++;
     ci->number = n;
-    ci->name = 0;
+    ci->name = 0 /* nullptr */;
     ntable.define(n, ci);
   }
   return ci;
@@ -151,8 +162,8 @@ glyph *number_to_glyph(int n)
 
 glyph *name_to_glyph(const char *s)
 {
-  assert(s != 0 && s[0] != '\0' && s[0] != ' ');
-  if (s[1] == '\0')
+  assert((s != 0 /* nullptr */) && (s[0] != '\0') && (s[0] != ' '));
+  if ('\0' == s[1])
     // \200 and char128 are synonyms
     return indexer.ascii_char_glyph(s[0]);
   return indexer.named_char_glyph(s);
diff --git a/src/roff/troff/charinfo.h b/src/roff/troff/charinfo.h
index 9f507b840..5f2a33362 100644
--- a/src/roff/troff/charinfo.h
+++ b/src/roff/troff/charinfo.h
@@ -24,6 +24,8 @@ extern void get_flags();
 
 class macro;
 
+// libgroff has a simpler `charinfo` class that stores much less
+// information.
 class charinfo : glyph {
   static int next_index;
   charinfo *translation;
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index e5da0b812..88dad3235 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -30,7 +30,7 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>. */
                   // ungetc()
 #include <stdlib.h> // atoi(), exit(), EXIT_FAILURE, EXIT_SUCCESS,
                    // free(), getenv(), putenv(), strtol(), system()
-#include <string.h> // strdup(), strerror()
+#include <string.h> // strcpy(), strdup(), strerror()
 
 #include <getopt.h> // getopt_long()
 
@@ -7919,12 +7919,18 @@ void spreadwarn_request()
   skip_line();
 }
 
+// Keep this in sync with "src/libs/libgroff/nametoindex.cpp".
+// constexpr // C++11
+static const char char_prefix[] = { 'c', 'h', 'a', 'r' };
+// constexpr // C++11
+static const size_t char_prefix_len = sizeof char_prefix;
+
 static void init_charset_table()
 {
   char buf[16];
-  strcpy(buf, "char");
+  (void) strncpy(buf, char_prefix, char_prefix_len);
   for (int i = 0; i < 256; i++) {
-    strcpy(buf + 4, i_to_a(i));
+    (void) strcpy((buf + char_prefix_len), i_to_a(i));
     charset_table[i] = get_charinfo(symbol(buf));
     charset_table[i]->set_ascii_code(i);
     if (csalpha(i))

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

Reply via email to