gbranden pushed a commit to branch master
in repository groff.

commit 604fbee5045a91bd4bdbc1dc9e2e9ef943ed54cc
Author: G. Branden Robinson <[email protected]>
AuthorDate: Fri Mar 20 18:05:38 2026 -0500

    [troff]: Refactor.
    
    Make the "guts" of the `chop_macro()` function a member function of
    class `macro`.
    
    * src/roff/troff/request.h (class macro): Declare member function
      `chop()`, taking and returning `void`.  Drop declaration of `friend`
      function `chop_macro()`.
    
    * src/roff/troff/input.cpp: Drop forward declaration of `chop_macro()`.
    
      (macro::chop): Member function takes responsibility for updating a
      `macro` object, reducing internally stored length of its contents if
      possible.  (Exception: token values used internally to manage the
      state of compatibility mode do not contribute to the length count.)
    
      (chop_macro): Rename this...  (chop_request): ...to this, following a
      developing convention of naming request handlers to end with
      "_request".  Declare it `static`.  Replace lengthy `else` branch with
      a call of the resolved macro's `chop()` member function.
    
      (init_input_requests): Update wire-uppery of "chop" request name.
---
 ChangeLog                | 23 +++++++++++++++++++
 src/roff/troff/input.cpp | 57 +++++++++++++++++++++++++-----------------------
 src/roff/troff/request.h |  2 +-
 3 files changed, 54 insertions(+), 28 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 83517af9c..7018e8de8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,26 @@
+2026-03-20  G. Branden Robinson <[email protected]>
+
+       [troff]: Refactor.  Make the "guts" of the `chop_macro()`
+       function a member function of class `macro`.
+
+       * src/roff/troff/request.h (class macro): Declare member
+       function `chop()`, taking and returning `void`.  Drop
+       declaration of `friend` function `chop_macro()`.
+       * src/roff/troff/input.cpp: Drop forward declaration of
+       `chop_macro()`.
+       (macro::chop): Member function takes responsibility for updating
+       a `macro` object, reducing internally stored length of its
+       contents if possible.  (Exception: token values used internally
+       to manage the state of compatibility mode do not contribute to
+       the length count.)
+       (chop_macro): Rename this...
+       (chop_request): ...to this, following a developing convention of
+       naming request handlers to end with "_request".  Declare it
+       `static`.  Replace lengthy `else` branch with a call of the
+       resolved macro's `chop()` member function.
+       (init_input_requests): Update wire-uppery of "chop" request
+       name.
+
 2026-03-20  G. Branden Robinson <[email protected]>
 
        * src/roff/troff/input.cpp (chop_macro): Arrange equality
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 80b12390a..d2a47edb4 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -192,7 +192,6 @@ input_iterator *make_temp_iterator(const char *);
 const char *input_char_description(int);
 
 void process_input_stack();
-void chop_macro();     // declare to avoid friend name injection
 
 static const unsigned char default_escape_char = (unsigned char)('\\');
 static unsigned char escape_char = default_escape_char;
@@ -4133,6 +4132,32 @@ void macro::append_int(int i)
   append_unsigned((unsigned int) i);
 }
 
+void macro::chop()
+{
+  bool contains_mode_tokens = false;
+  // We have to check for save/restore pairs which could be present due
+  // to as1, ds1, de1, am1 requests.
+  for (;;) {
+    if (get(len - 1) != POP_GROFFCOMP_MODE)
+      break;
+    contains_mode_tokens = true;
+    len -= 1;
+    if (get(len - 1) != PUSH_GROFF_MODE
+       && get(len - 1) != PUSH_COMP_MODE)
+      break;
+    contains_mode_tokens = false;
+    len -= 1;
+    if (0 == len)
+      break;
+  }
+  assert(len != 0);
+  // TODO: If it's empty, do nothing, quietly?
+  if (contains_mode_tokens)
+    set(POP_GROFFCOMP_MODE, len - 1);
+  else
+    len -= 1;
+}
+
 void macro::print_size()
 {
   errprint("%1", len);
@@ -5683,7 +5708,7 @@ void alias_macro()
   skip_line();
 }
 
-void chop_macro()
+static void chop_request()
 {
   if (!has_arg()) {
     warning(WARN_MISSING, "chop request expects an argument");
@@ -5702,30 +5727,8 @@ void chop_macro()
       error("cannot chop empty %1 '%2'",
            (m->is_diversion() ? "diversion" : "macro or string"),
            s.contents());
-    else {
-      bool contains_mode_tokens = false;
-      // We have to check for save/restore pairs which could
-      // be present due to as1, ds1, de1, am1 requests.
-      for (;;) {
-       if (m->get(m->len - 1) != POP_GROFFCOMP_MODE)
-         break;
-       contains_mode_tokens = true;
-       m->len -= 1;
-       if (m->get(m->len - 1) != PUSH_GROFF_MODE
-           && m->get(m->len - 1) != PUSH_COMP_MODE)
-         break;
-       contains_mode_tokens = false;
-       m->len -= 1;
-       if (0 == m->len)
-         break;
-      }
-      assert(m->len != 0);
-      // TODO: If it's empty, do nothing, quietly?
-      if (contains_mode_tokens)
-       m->set(POP_GROFFCOMP_MODE, m->len - 1);
-      else
-       m->len -= 1;
-    }
+    else
+      m->chop();
   }
   skip_line();
 }
@@ -10228,7 +10231,7 @@ void init_input_requests()
   init_request("cf", unsafe_transparent_throughput_file_request);
   init_request("cflags", set_character_flags_request);
   init_request("char", define_character_request);
-  init_request("chop", chop_macro);
+  init_request("chop", chop_request);
   init_request("class", define_class_request);
   init_request("close", close_request);
   init_request("color", activate_color);
diff --git a/src/roff/troff/request.h b/src/roff/troff/request.h
index deec2838d..5a4d007d5 100644
--- a/src/roff/troff/request.h
+++ b/src/roff/troff/request.h
@@ -62,6 +62,7 @@ public:
   void append_int(int);
   void append_str(const char *);
   void set(unsigned char, int);
+  void chop();
   unsigned char get(int);
   int length();
   void invoke(symbol, bool);
@@ -74,7 +75,6 @@ public:
   void dump();
   void json_dump();
   friend class string_iterator;
-  friend void chop_macro();
   friend void substring_request();
   friend bool operator==(const macro &, const macro &);
 };

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

Reply via email to