diff options
author | G. Branden Robinson <g.branden.robinson@gmail.com> | 2024-03-07 07:08:38 -0600 |
---|---|---|
committer | G. Branden Robinson <g.branden.robinson@gmail.com> | 2024-03-07 17:21:07 -0600 |
commit | 61141803ffbb9cc7ed8c27744bc8689c43a953d2 (patch) | |
tree | ab0d4c35c571217fc0997edb7ed1480e7ddeae64 | |
parent | e6f119c36ba820a13c00842f1dccb799ef59d4cd (diff) |
[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.
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | src/roff/troff/input.cpp | 74 |
2 files changed, 51 insertions, 32 deletions
@@ -1,5 +1,14 @@ 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 style nit, using C++-style type cast instead of C-style cast. 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) |