gbranden pushed a commit to branch master
in repository groff.
commit 3c1fa16104e747a249fc6d8360a84f8667058a4b
Author: G. Branden Robinson <[email protected]>
AuthorDate: Tue Apr 14 05:16:43 2026 -0500
[troff]: Slightly refactor.
Migrate GNU troff's `dictionary` class to use `ssize_t` for measurement
of its capacity and occupancy (and for integer variables iterating
through it on that basis).
* src/roff/troff/dictionary.h (class dictionary_iterator): Migrate
member variable `i` from `int` to `ssize_t`.
(class dictionary): Migrate member variables `capacity` and
`occupancy` from `int` to `ssize_t`.
(class dictionary, class object_dictionary): Migrate
constructors to take an argument of `ssize_t` instead of `int` type.
* src/roff/troff/dictionary.cpp: Preprocessor-include "<sys/types.h">
header defining `ssize_t` type.
* bootstrap.conf: Add "ssize_t" to `gnulib_modules`, since neither ISO
C99 nor ISO C++98 guarantee its existence. (It's a POSIX 2001
feature).
---
ChangeLog | 21 +++++++++++++++++++++
bootstrap.conf | 1 +
src/roff/troff/dictionary.cpp | 31 +++++++++++++++++--------------
src/roff/troff/dictionary.h | 10 +++++-----
4 files changed, 44 insertions(+), 19 deletions(-)
diff --git a/ChangeLog b/ChangeLog
index 07919e3a5..1e3e725df 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,24 @@
+2026-04-14 G. Branden Robinson <[email protected]>
+
+ [troff]: Slightly refactor. Migrate GNU troff's `dictionary`
+ class to use `ssize_t` for measurement of its capacity and
+ occupancy (and for integer variables iterating through it on
+ that basis).
+
+ * src/roff/troff/dictionary.h (class dictionary_iterator):
+ Migrate member variable `i` from `int` to `ssize_t`.
+ (class dictionary): Migrate member variables `capacity` and
+ `occupancy` from `int` to `ssize_t`.
+ (class dictionary, class object_dictionary): Migrate
+ constructors to take an argument of `ssize_t` instead of `int`
+ type.
+ * src/roff/troff/dictionary.cpp: Preprocessor-include
+ "<sys/types.h"> header defining `ssize_t` type.
+
+ * bootstrap.conf: Add "ssize_t" to `gnulib_modules`, since
+ neither ISO C99 nor ISO C++98 guarantee its existence. (It's a
+ POSIX 2001 feature).
+
2026-04-14 G. Branden Robinson <[email protected]>
[troff]: Trivially refactor.
diff --git a/bootstrap.conf b/bootstrap.conf
index b70b2f5d7..19b5f9881 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -48,6 +48,7 @@ gnulib_modules="
fprintf-posix
snprintf
vsnprintf
+ ssize_t
stat
stdbool-h-c99
stdckdint-h
diff --git a/src/roff/troff/dictionary.cpp b/src/roff/troff/dictionary.cpp
index 760a994cf..8cdcc62b2 100644
--- a/src/roff/troff/dictionary.cpp
+++ b/src/roff/troff/dictionary.cpp
@@ -25,6 +25,7 @@ along with this program. If not, see
<http://www.gnu.org/licenses/>. */
#endif
#include <stdio.h> // prerequisite of searchpath.h
+#include <sys/types.h> // ssize_t
// libgroff
#include "symbol.h" // prerequisite of dictionary.h
@@ -32,10 +33,10 @@ along with this program. If not, see
<http://www.gnu.org/licenses/>. */
// is 'p' a good size for a hash table
-static bool is_good_size(unsigned int p)
+static bool is_good_size(ssize_t p)
{
- const unsigned int SMALL = 10;
- unsigned int i;
+ const ssize_t SMALL = 10;
+ ssize_t i;
for (i = 2; i <= (p / 2); i++)
if ((p % i) == 0)
return false;
@@ -45,7 +46,7 @@ static bool is_good_size(unsigned int p)
return true;
}
-dictionary::dictionary(int n)
+dictionary::dictionary(ssize_t n)
: capacity(n), occupancy(0), threshold(0.5), factor(1.5)
{
table = new association[n];
@@ -56,8 +57,8 @@ dictionary::dictionary(int n)
void *dictionary::lookup(symbol s, void *v)
{
- int i;
- for (i = size_t(s.hash() % capacity);
+ ssize_t i;
+ for (i = ssize_t(s.hash() % capacity);
table[i].v != 0 /* nullptr */;
i == 0 ? i = (capacity - 1) : --i)
if (s == table[i].s) {
@@ -76,8 +77,8 @@ void *dictionary::lookup(symbol s, void *v)
table[i].s = s;
if (((static_cast<double>(occupancy) / static_cast<double>(capacity))
>= threshold) || ((occupancy + 1) >= capacity)) {
- int old_capacity = capacity;
- capacity = int(capacity * factor);
+ ssize_t old_capacity = capacity;
+ capacity = ssize_t(capacity * factor);
while (!is_good_size(capacity))
++capacity;
association *old_table = table;
@@ -105,23 +106,25 @@ void *dictionary::lookup(const char *p)
void *dictionary::remove(symbol s)
{
// this relies on the fact that we are using linear probing
- int i;
- for (i = int(s.hash() % capacity);
+ // XXX: This method requires us to use a signed type for `i` and thus
+ // for container capacity and occupancy. -- GBR, 2026
+ ssize_t i;
+ for (i = ssize_t(s.hash() % capacity);
table[i].v != 0 /* nullptr */ && s != table[i].s;
i == 0 ? i = (capacity - 1) : --i)
;
void *p = table[i].v;
while (table[i].v != 0 /* nullptr */) {
table[i].v = 0 /* nullptr */;
- int j = i;
- int r;
+ ssize_t j = i;
+ ssize_t r;
do {
--i;
if (i < 0)
i = capacity - 1;
if (table[i].v == 0 /* nullptr */)
break;
- r = int(table[i].s.hash() % capacity);
+ r = ssize_t(table[i].s.hash() % capacity);
} while ((i <= r && r < j) || (r < j && j < i) || (j < i && i <= r));
table[j] = table[i];
}
@@ -171,7 +174,7 @@ void object::remove_reference()
delete this;
}
-object_dictionary::object_dictionary(int n) : d(n)
+object_dictionary::object_dictionary(ssize_t n) : d(n)
{
}
diff --git a/src/roff/troff/dictionary.h b/src/roff/troff/dictionary.h
index 43e6dc9a2..aa1c0fa1f 100644
--- a/src/roff/troff/dictionary.h
+++ b/src/roff/troff/dictionary.h
@@ -33,20 +33,20 @@ class dictionary;
class dictionary_iterator {
dictionary *dict;
- int i;
+ ssize_t i;
public:
dictionary_iterator(dictionary &);
bool get(symbol *, void **);
};
class dictionary {
- int capacity;
- int occupancy;
+ ssize_t capacity;
+ ssize_t occupancy;
double threshold;
double factor;
association *table;
public:
- dictionary(int);
+ dictionary(ssize_t);
void *lookup(symbol, void * = 0 /* nullptr */);
void *lookup(const char *);
void *remove(symbol);
@@ -74,7 +74,7 @@ public:
class object_dictionary {
dictionary d;
public:
- object_dictionary(int);
+ object_dictionary(ssize_t);
object *lookup(symbol);
void define(symbol, object *);
void rename(symbol, symbol);
_______________________________________________
groff-commit mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/groff-commit