gbranden pushed a commit to branch master
in repository groff.

commit f1454c96e50be6bb6ae1057a4c2b5dccf8d63a2f
Author: G. Branden Robinson <[email protected]>
AuthorDate: Sun Apr 12 05:45:56 2026 -0500

    src/roff/troff/env.cpp: Generalize new function.
    
    * src/roff/troff/env.cpp (read_hyphenation_exception_word): Support
      being called with null pointers to store the breakpoint count and
      positions, so that a request that isn't interested in them need not
      reserve storage for them.  Consequently, default these function
      parameters to null pointers.  Dereference and populate the breakpoint
      count and positions only if these data are wanted.  Add assert(3)ion
      that either both pointers are null or neither one is.
---
 ChangeLog              | 11 +++++++++++
 src/roff/troff/env.cpp | 21 +++++++++++++++------
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 891ee75b9..810fa5189 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2026-04-12  G. Branden Robinson <[email protected]>
+
+       * src/roff/troff/env.cpp (read_hyphenation_exception_word):
+       Generalize.  Support being called with null pointers to store
+       the breakpoint count and positions, so that a request that isn't
+       interested in them need not reserve storage for them.
+       Consequently, default these function parameters to null
+       pointers.  Dereference and populate the breakpoint count and
+       positions only if these data are wanted.  Add assert(3)ion that
+       either both pointers are null or neither one is.
+
 2026-04-10  G. Branden Robinson <[email protected]>
 
        * src/roff/troff/env.cpp: Refactor.  Lift logic for parsing a
diff --git a/src/roff/troff/env.cpp b/src/roff/troff/env.cpp
index 2324fb10b..e01bdd48e 100644
--- a/src/roff/troff/env.cpp
+++ b/src/roff/troff/env.cpp
@@ -3934,12 +3934,19 @@ static const size_t bpbuflen = WORD_MAX + 2 /* leading 
'-' + '\0' */;
 // `breakpoint_count`, and `breakpoints` are then not meaningful and
 // should not be used.
 static bool read_hyphenation_exception_word(unsigned char *word,
-                                           int *breakpoint_count,
-                                           unsigned char *breakpoints)
-{
+  int *breakpoint_count = 0 /* nullptr */,
+  unsigned char *breakpoints = 0 /* nullptr */)
+{
+  // Either both pointers must be null, or both non-null.
+  assert(!!(breakpoint_count) == !!(breakpoints));
+  bool want_breakpoints = false;
+  if ((breakpoint_count != 0 /* nullptr */)
+      && (breakpoints != 0 /* nullptr */))
+    want_breakpoints = true;
   int i = 0; // index into hyphenation exception word excluding '-'s
   unsigned char hc = 0U; // hyphenation code of current character
-  *breakpoint_count = 0;
+  if (want_breakpoints)
+    *breakpoint_count = 0;
   // Warn at most once per invalid word, not per request invocation.
   bool is_word_valid = true;
   bool was_warned = false;
@@ -3965,7 +3972,8 @@ static bool read_hyphenation_exception_word(unsigned char 
*word,
     if (is_word_valid) {
       tok.next();
       if (ci->get_ascii_code() == '-') {
-       if ((i > 0)
+       if (want_breakpoints
+           && (i > 0)
            && ((*breakpoint_count == 0)
                || (breakpoints[((*breakpoint_count) - 1)] != i)))
          breakpoints[(*breakpoint_count)++] = i;
@@ -3991,7 +3999,8 @@ static bool read_hyphenation_exception_word(unsigned char 
*word,
   // of maximum size `WORD_MAX` (`UCHAR_MAX`).  That's kind of confusing
   // because `unsigned char` is also GNU troff's internal "ordinary"
   // character type.  Might be simpler just to use vector<int>.  --GBR
-  breakpoints[*breakpoint_count] = 0U;
+  if (want_breakpoints)
+    breakpoints[*breakpoint_count] = 0U;
   return is_word_valid;
 }
 

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

Reply via email to