gbranden pushed a commit to branch master
in repository groff.

commit ca607442033bbe2ff7257c1e19b2cee85c52ce88
Author: G. Branden Robinson <[email protected]>
AuthorDate: Sat Apr 26 19:04:37 2025 -0500

    [libgroff]: Fix code style nits.
    
    * src/libs/libgroff/string.cpp (string::json_length):
    * src/libs/libgroff/symbol.cpp (symbol::json_length): Drop
      unnecessary/tautologous comparisons of integer literals with objects
      of plain `char` type, which are of undefined signedness.
    
    This doesn't harm objects containing characters with the 8th bit set.
    
    Demonstration:
    
    $ locale
    LANG=C
    LANGUAGE=
    LC_CTYPE="en_US" # implicitly ISO 8859-1
    LC_NUMERIC="en_US"
    LC_TIME="en_US"
    LC_COLLATE="en_US"
    LC_MONETARY="en_US"
    LC_MESSAGES="en_US"
    LC_PAPER="en_US"
    LC_NAME="en_US"
    LC_ADDRESS="en_US"
    LC_TELEPHONE="en_US"
    LC_MEASUREMENT="en_US"
    LC_IDENTIFICATION="en_US"
    LC_ALL=en_US
    $ printf '.ds s Queensr\377che\n.pm s\n.' | groff -ww
    {"name": "s", "file name": "<standard input>", "starting line number": 1, 
"length": 11, "contents": "Queensr\u00FFche", "node list": [ ]}
---
 ChangeLog                    |  7 +++++++
 src/libs/libgroff/string.cpp | 35 ++++++++++++++++-------------------
 src/libs/libgroff/symbol.cpp | 35 ++++++++++++++++-------------------
 3 files changed, 39 insertions(+), 38 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 40e5728b4..25c603033 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2025-04-26  G. Branden Robinson <[email protected]>
+
+       * src/libs/libgroff/string.cpp (string::json_length):
+       * src/libs/libgroff/symbol.cpp (symbol::json_length): Drop
+       unnecessary/tautologous comparisons of integer literals with
+       objects of plain `char` type, which are of undefined signedness.
+
 2025-04-26  G. Branden Robinson <[email protected]>
 
        * src/libs/libgroff/nametoindex.cpp (glyph_to_name): Use
diff --git a/src/libs/libgroff/string.cpp b/src/libs/libgroff/string.cpp
index d350d809a..db1103cf3 100644
--- a/src/libs/libgroff/string.cpp
+++ b/src/libs/libgroff/string.cpp
@@ -333,27 +333,24 @@ size_t string::json_length() const
   int nextrachars = 2; // leading and trailing double quotes
   for (size_t i = 0; i < n; i++) {
     ch = p[i];
-    if (ch < 128) {
-      // These printable characters require escaping.
-      if (('"' == ch) || ('\\' == ch) || ('/' == ch))
+    assert ((ch >= 0) && (ch <= 127));
+    // These printable characters require escaping.
+    if (('"' == ch) || ('\\' == ch) || ('/' == ch))
+      nextrachars++;
+    else if (csprint(ch))
+      ;
+    else
+      switch (ch) {
+      case '\b':
+      case '\f':
+      case '\n':
+      case '\r':
+      case '\t':
        nextrachars++;
-      else if (csprint(ch))
-       ;
-      else
-       switch (ch) {
-       case '\b':
-       case '\f':
-       case '\n':
-       case '\r':
-       case '\t':
-         nextrachars++;
-         break;
-       default:
-         nextrachars += 5;
-      }
+       break;
+      default:
+       nextrachars += 5;
     }
-    else
-      nextrachars += 5;
   }
   return (n + nextrachars);
 }
diff --git a/src/libs/libgroff/symbol.cpp b/src/libs/libgroff/symbol.cpp
index be2143ab4..a0082bf7c 100644
--- a/src/libs/libgroff/symbol.cpp
+++ b/src/libs/libgroff/symbol.cpp
@@ -175,27 +175,24 @@ size_t symbol::json_length() const
   int nextrachars = 2; // leading and trailing double quotes
   for (size_t i = 0; p[i] != '\0'; i++, len++) {
     ch = p[i];
-    if (ch < 128) {
-      // These printable characters require escaping.
-      if (('"' == ch) || ('\\' == ch) || ('/' == ch))
+    assert ((ch >= 0) && (ch <= 127));
+    // These printable characters require escaping.
+    if (('"' == ch) || ('\\' == ch) || ('/' == ch))
+      nextrachars++;
+    else if (csprint(ch))
+      ;
+    else
+      switch (ch) {
+      case '\b':
+      case '\f':
+      case '\n':
+      case '\r':
+      case '\t':
        nextrachars++;
-      else if (csprint(ch))
-       ;
-      else
-       switch (ch) {
-       case '\b':
-       case '\f':
-       case '\n':
-       case '\r':
-       case '\t':
-         nextrachars++;
-         break;
-       default:
-         nextrachars += 5;
-      }
+       break;
+      default:
+       nextrachars += 5;
     }
-    else
-      nextrachars += 5;
   }
   return (len + nextrachars);
 }

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

Reply via email to