groff-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[groff] 25/28: src/roff/troff/input.cpp: Refactor.


From: G. Branden Robinson
Subject: [groff] 25/28: src/roff/troff/input.cpp: Refactor.
Date: Sun, 25 Aug 2024 23:03:32 -0400 (EDT)

gbranden pushed a commit to branch master
in repository groff.

commit e068c518d0d0abeb93be4ab98fe48d40c4d1a5f2
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Sat Aug 24 23:45:45 2024 -0500

    src/roff/troff/input.cpp: Refactor.
    
    * src/roff/troff/input.cpp (encode_special_character_for_device_output):
      New helper function takes over much of the tedium of
      `encode_character_for_device_output()`, for maintainability.
    
      (encode_char_for_device_output): Rename this...
      (encode_character_for_device_output): ...to this.  Use the new helper
      function when necessary.
---
 ChangeLog                | 10 +++++
 src/roff/troff/input.cpp | 96 +++++++++++++++++++++++++-----------------------
 2 files changed, 61 insertions(+), 45 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index dfb382fbe..cddb7fa96 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2024-08-25  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/roff/troff/input.cpp
+       (encode_special_character_for_device_output): New helper
+       function takes over much of the tedium of
+       `encode_character_for_device_output()`, for maintainability.
+       (encode_char_for_device_output): Rename this...
+       (encode_character_for_device_output): ...to this.  Use the new
+       helper function when necessary.
+
 2024-08-25  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/roff/troff/input.cpp (read_long_escape_parameters)
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 337021d36..cf0d17acf 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -5676,12 +5676,59 @@ static node *do_non_interpreted()
   return new non_interpreted_node(mac);
 }
 
+// This is a helper function for `encode_character_for_device_output()`.
+static void encode_special_character_for_device_output(macro *mac)
+{
+  const char *sc;
+  if (font::use_charnames_in_special) {
+    charinfo *ci = tok.get_char(true /* required */);
+    sc = ci->get_symbol()->contents();
+  }
+  else
+    sc = tok.get_char()->get_symbol()->contents();
+  if (strcmp("-", sc) == 0)
+    mac->append('-');
+  else if (strcmp("aq", sc) == 0)
+    mac->append('\'');
+  else if (strcmp("dq", sc) == 0)
+    mac->append('"');
+  else if (strcmp("ga", sc) == 0)
+    mac->append('`');
+  else if (strcmp("ha", sc) == 0)
+    mac->append('^');
+  else if (strcmp("rs", sc) == 0)
+    mac->append('\\');
+  else if (strcmp("ti", sc) == 0)
+    mac->append('~');
+  else {
+    if (font::use_charnames_in_special) {
+      if (sc[0] != '\0') {
+       mac->append('\\');
+       mac->append('[');
+       int i = 0;
+       while (sc[i] != '\0') {
+         mac->append(sc[i]);
+         i++;
+       }
+       mac->append(']');
+      }
+      else
+       error("special character '%1' is unusable within a device"
+             " control escape sequence", sc);
+    }
+    else
+      error("special character '%1' cannot be used within a device"
+           " control escape sequence", sc);
+  }
+}
+
 // In troff output, we translate the escape character to '\', but it is
 // up to the postprocessor to interpret it as such.  (This mostly
 // matters for device control commands.)
-static void encode_char_for_device_output(macro *mac, const char c)
+static void encode_character_for_device_output(macro *mac, const char c)
 {
   if ('\0' == c) {
+    // It's a special token, not a character we can write as-is.
     if (tok.is_stretchable_space()
             || tok.is_unstretchable_space())
       mac->append(' ');
@@ -5690,49 +5737,8 @@ static void encode_char_for_device_output(macro *mac, 
const char c)
             || tok.is_dummy()
             || tok.is_transparent_dummy())
       /* do nothing */;
-    else if (tok.is_special()) {
-      const char *sc;
-      if (font::use_charnames_in_special) {
-       charinfo *ci = tok.get_char(true /* required */);
-       sc = ci->get_symbol()->contents();
-      }
-      else
-       sc = tok.get_char()->get_symbol()->contents();
-      if (strcmp("-", sc) == 0)
-       mac->append('-');
-      else if (strcmp("aq", sc) == 0)
-       mac->append('\'');
-      else if (strcmp("dq", sc) == 0)
-       mac->append('"');
-      else if (strcmp("ga", sc) == 0)
-       mac->append('`');
-      else if (strcmp("ha", sc) == 0)
-       mac->append('^');
-      else if (strcmp("rs", sc) == 0)
-       mac->append('\\');
-      else if (strcmp("ti", sc) == 0)
-       mac->append('~');
-      else {
-       if (font::use_charnames_in_special) {
-         if (sc[0] != '\0') {
-           mac->append('\\');
-           mac->append('[');
-           int i = 0;
-           while (sc[i] != '\0') {
-             mac->append(sc[i]);
-             i++;
-           }
-           mac->append(']');
-         }
-         else
-           error("special character '%1' is unusable within a device"
-                 " control escape sequence", sc);
-       }
-       else
-         error("special character '%1' cannot be used within a device"
-               " control escape sequence", sc);
-      }
-    }
+    else if (tok.is_special())
+      encode_special_character_for_device_output(mac);
     else
       error("%1 is invalid within device control escape sequence",
            tok.description());
@@ -5780,7 +5786,7 @@ static node *do_device_control() // \X
       c = '\b';
     else
       c = tok.ch();
-    encode_char_for_device_output(&mac, c);
+    encode_character_for_device_output(&mac, c);
   }
   return new special_node(mac);
 }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]