gbranden pushed a commit to branch master
in repository groff.
commit 3435977999aef227b046cc984f8424e02627e227
Author: G. Branden Robinson <[email protected]>
AuthorDate: Mon Nov 23 11:17:06 2026 -0600
src/roff/troff/env.cpp: Refactor.
* src/roff/troff/env.cpp: Migrate representation of *roff's environment
stack from a hand-rolled linked list `struct` called `env_list_node`
to an STL vector. Why an STL vector and not an STL stack? Because
mutation operations occur only at the distal end--environment pointers
are added (pushed) and removed (popped). But we also want to be able
to iterate through the vector non-destructively to report ("peek at")
its contents and determine whether a given environment is stacked or
not. Delete `env_list_node` structure definition. Retype `env_stack`
from it to `std::vector<environment *>`.
(init_environments): Actually stack the initial environment. `curenv`
is now merely a convenience pointer, and it might be wise to replace
most or all instances of it with `env_stack.back()`, because we might
risk decoherence problems otherwise. But for now, things work.
(environment_switch_request): When pushing an environment onto the
stack, replace creation of new node in linked list with `push_back()`
operation on `env_stack`. Rewrite test for invalid attempt to pop a
single-item environment stack with a test for an `env_stack.size()` of
`1`. When popping the stack, `env_stack.pop_back()` and assign
`env_stack.back()` to `curenv`. Drop linked-list node copying logic.
---
ChangeLog | 25 +++++++++++++++++++++++++
src/roff/troff/env.cpp | 29 ++++++++++++++---------------
2 files changed, 39 insertions(+), 15 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index d24882b98..d4c54df3a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,28 @@
+2026-11-23 G. Branden Robinson <[email protected]>
+
+ * src/roff/troff/env.cpp: Refactor. Migrate representation of
+ *roff's environment stack from a hand-rolled linked list
+ `struct` called `env_list_node` to an STL vector. Why an STL
+ vector and not an STL stack? Because mutation operations occur
+ only at the distal end--environment pointers are added (pushed)
+ and removed (popped). But we also want to be able to iterate
+ through the vector non-destructively to report ("peek at") its
+ contents and determine whether a given environment is stacked or
+ not. Delete `env_list_node` structure definition. Retype
+ `env_stack` from it to `std::vector<environment *>`.
+ (init_environments): Actually stack the initial environment.
+ `curenv` is now merely a convenience pointer, and it might be
+ wise to replace most or all instances of it with
+ `env_stack.back()`, because we might risk decoherence problems
+ otherwise. But for now, things work.
+ (environment_switch_request): When pushing an environment onto
+ the stack, replace creation of new node in linked list with
+ `push_back()` operation on `env_stack`. Rewrite test for
+ invalid attempt to pop a single-item environment stack with a
+ test for an `env_stack.size()` of `1`. When popping the stack,
+ `env_stack.pop_back()` and assign `env_stack.back()` to
+ `curenv`. Drop linked-list node copying logic.
+
2026-11-23 G. Branden Robinson <[email protected]>
* src/roff/troff/env.cpp (environment_switch_request): Fix code
diff --git a/src/roff/troff/env.cpp b/src/roff/troff/env.cpp
index fe71ba0a9..07a2d284c 100644
--- a/src/roff/troff/env.cpp
+++ b/src/roff/troff/env.cpp
@@ -88,13 +88,12 @@ enum {
HYPHEN_MAX = 63
};
-struct env_list_node {
- environment *env;
- env_list_node *next;
- env_list_node(environment *e, env_list_node *p) : env(e), next(p) {}
-};
-
-env_list_node *env_stack;
+// We use an STL vector to simulate a stack because mutation operations
+// occur only at the distal end--environment pointers are added (pushed)
+// and removed (popped). But we also want to be able to iterate through
+// the vector non-destructively to report ("peek at") its contents and
+// determine whether a given environment is stacked or not.
+std::vector<environment *> env_stack;
dictionary env_dictionary(10);
environment *curenv;
static int next_line_number = 0;
@@ -334,8 +333,10 @@ static symbol default_environment_name("0");
void init_environments()
{
- curenv = new environment(default_environment_name);
- (void) env_dictionary.lookup(default_environment_name, curenv);
+ environment *e = new environment(default_environment_name);
+ (void) env_dictionary.lookup(default_environment_name, e);
+ env_stack.push_back(e);
+ curenv = e;
}
// Set tab character, used to fill out the remainder of a tab stop where
@@ -3965,12 +3966,12 @@ static void environment_switch_request() // .ev
e = new environment(nm);
(void) env_dictionary.lookup(nm, e);
}
- env_stack = new env_list_node(curenv, env_stack);
+ env_stack.push_back(e);
curenv = e;
}
else {
// Pop environment stack.
- if (env_stack == 0 /* nullptr */) {
+ if (env_stack.size() == 1) {
error("cannot pop the only environment in the stack");
skip_line();
return;
@@ -3980,13 +3981,11 @@ static void environment_switch_request() // .ev
bool seen_space = curenv->seen_space;
bool seen_eol = curenv->seen_eol;
bool suppress_next_eol = curenv->suppress_next_eol;
- curenv = env_stack->env;
+ env_stack.pop_back();
+ curenv = env_stack.back();
curenv->seen_space = seen_space;
curenv->seen_eol = seen_eol;
curenv->suppress_next_eol = suppress_next_eol;
- env_list_node *tem = env_stack;
- env_stack = env_stack->next;
- delete tem;
}
skip_line();
}
_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit