groff-commit
[Top][All Lists]
Advanced

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

[groff] 11/12: [troff]: Refactor (is_char_usable_as_delimiter).


From: G. Branden Robinson
Subject: [groff] 11/12: [troff]: Refactor (is_char_usable_as_delimiter).
Date: Thu, 7 Mar 2024 18:37:44 -0500 (EST)

gbranden pushed a commit to branch master
in repository groff.

commit 61141803ffbb9cc7ed8c27744bc8689c43a953d2
Author: G. Branden Robinson <g.branden.robinson@gmail.com>
AuthorDate: Thu Mar 7 07:08:38 2024 -0600

    [troff]: Refactor (is_char_usable_as_delimiter).
    
    * src/roff/troff/input.cpp: Refactor.  Pull delimiter character
      validator into its own function operating on a character, rather than
      on an object of the token class.
    
      (is_char_usable_as_delimiter): New function compares `char` parameter
      to list of valid delimiters.
    
      (token::is_usable_as_delimiter): Refactor to call the foregoing.
---
 ChangeLog                |  9 ++++++
 src/roff/troff/input.cpp | 74 +++++++++++++++++++++++++++---------------------
 2 files changed, 51 insertions(+), 32 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 2a9be3b86..05ee4fedc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-03-07  G. Branden Robinson <g.branden.robinson@gmail.com>
+
+       * src/roff/troff/input.cpp: Refactor.  Pull delimiter character
+       validator into its own function operating on a character, rather
+       than on an object of the token class.
+       (is_char_usable_as_delimiter): New function compares `char`
+       parameter to list of valid delimiters.
+       (token::is_usable_as_delimiter): Refactor to call the foregoing.
+
 2024-03-07  G. Branden Robinson <g.branden.robinson@gmail.com>
 
        * src/roff/troff/input.cpp (is_usable_as_delimiter): Fix code
diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 7fa0e08d4..8bf5d1017 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -2473,44 +2473,54 @@ int token::operator!=(const token &t)
   return !(*this == t);
 }
 
-// is token a suitable delimiter (like ')?
+// Is the character usable as a delimiter?
+//
+// This is used directly only by `do_device_control()`, because it is
+// the only escape sequence that reads its argument in copy mode (so it
+// doesn't tokenize it) and accepts a user-specified delimiter.
+static bool is_char_usable_as_delimiter(int c)
+{
+  switch(c) {
+  case '0':
+  case '1':
+  case '2':
+  case '3':
+  case '4':
+  case '5':
+  case '6':
+  case '7':
+  case '8':
+  case '9':
+  case '+':
+  case '-':
+  case '/':
+  case '*':
+  case '%':
+  case '<':
+  case '>':
+  case '=':
+  case '&':
+  case ':':
+  case '(':
+  case ')':
+  case '.':
+    return false;
+  default:
+    return true;
+  }
+}
 
 // Is the current token a suitable delimiter (like `'`)?
 bool token::is_usable_as_delimiter(bool report_error)
 {
+  bool is_valid = false;
   switch(type) {
   case TOKEN_CHAR:
-    switch(c) {
-    case '0':
-    case '1':
-    case '2':
-    case '3':
-    case '4':
-    case '5':
-    case '6':
-    case '7':
-    case '8':
-    case '9':
-    case '+':
-    case '-':
-    case '/':
-    case '*':
-    case '%':
-    case '<':
-    case '>':
-    case '=':
-    case '&':
-    case ':':
-    case '(':
-    case ')':
-    case '.':
-      if (report_error)
-        error("character '%1' is not allowed as a starting delimiter",
-             static_cast<char>(c));
-      return false;
-    default:
-      return true;
-    }
+    is_valid = is_char_usable_as_delimiter(c);
+    if (!is_valid && report_error)
+      error("character '%1' is not allowed as a starting delimiter",
+           static_cast<char>(c));
+    return is_valid;
   case TOKEN_NODE:
     // the user doesn't know what a node is
     if (report_error)



reply via email to

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