summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@kernel.org>2024-03-13 10:47:58 +0100
committerAlejandro Colomar <alx@kernel.org>2024-03-16 13:30:21 +0100
commit29798c58bde575b73ad94c4ddb5394f6044f9eba (patch)
tree21c05c3ff22ed86030b437f8c486c5fb8395b799
parent9e56cc80e6827dec9b5176289469c03779eacea6 (diff)
src/: ceil_prime(): Add function to get the lowest prime not less than n
And use it where the same logic was being open-coded. While at it, fix the logic, which was incorrect in the open-coded call sites, since for an input of 1, it produced 3, but the first prime is 2. A recent commit started rejecting 1 earlier, so this bug was now impossible to trigger, but remained there. Also, since this is a library function, let's behave well for an input of 0, which is mathematically fine, and return also the first prime, 2. Fixes: 4c7a3396375b ("[libbib, libgroff, indxbib]: Slightly refactor.") Link: <https://savannah.gnu.org/bugs/?65452> Link: <https://lists.gnu.org/archive/html/groff/2024-03/msg00065.html> Cc: "G. Branden Robinson" <branden@debian.org> Cc: Dave Kemper <saint.snit@gmail.com> Cc: "James K. Lowden" <jklowden@schemamania.org> Cc: Colin Watson <cjwatson@debian.org> Cc: Werner LEMBERG <wl@gnu.org> Cc: James Clark <jjc@jclark.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r--src/include/lib.h2
-rw-r--r--src/libs/libbib/index.cpp4
-rw-r--r--src/libs/libgroff/prime.cpp18
-rw-r--r--src/utils/indxbib/indxbib.cpp6
4 files changed, 20 insertions, 10 deletions
diff --git a/src/include/lib.h b/src/include/lib.h
index 0f4ed458e..b6c98caee 100644
--- a/src/include/lib.h
+++ b/src/include/lib.h
@@ -56,7 +56,7 @@ extern "C" {
#include <stdbool.h>
char *strsave(const char *s);
-bool is_prime(unsigned);
+unsigned ceil_prime(unsigned);
double groff_hypot(double, double);
#include <stdio.h>
diff --git a/src/libs/libbib/index.cpp b/src/libs/libbib/index.cpp
index 18701a157..53088397a 100644
--- a/src/libs/libbib/index.cpp
+++ b/src/libs/libbib/index.cpp
@@ -611,9 +611,7 @@ void index_search_item::read_common_words_file()
error("can't open '%1': %2", common_words_file, strerror(errno));
return;
}
- common_words_table_size = 2*header.common + 1;
- while (!is_prime(common_words_table_size))
- common_words_table_size += 2;
+ common_words_table_size = ceil_prime(2 * header.common);
common_words_table = new char *[common_words_table_size];
for (int i = 0; i < common_words_table_size; i++)
common_words_table[i] = 0;
diff --git a/src/libs/libgroff/prime.cpp b/src/libs/libgroff/prime.cpp
index b1e73470a..485f7b95d 100644
--- a/src/libs/libgroff/prime.cpp
+++ b/src/libs/libgroff/prime.cpp
@@ -22,7 +22,23 @@ internet at <http://www.gnu.org/licenses/gpl-2.0.txt>. */
#include <assert.h>
#include <math.h>
-bool is_prime(unsigned n)
+static bool is_prime(unsigned);
+
+unsigned ceil_prime(unsigned n)
+{
+ if (n <= 2)
+ return 2;
+
+ if (n % 2 == 0)
+ n++;
+
+ while (!is_prime(n))
+ n += 2;
+
+ return n;
+}
+
+static bool is_prime(unsigned n)
{
assert(n > 1);
if (n <= 3)
diff --git a/src/utils/indxbib/indxbib.cpp b/src/utils/indxbib/indxbib.cpp
index a3ac8eb6e..b15bd8187 100644
--- a/src/utils/indxbib/indxbib.cpp
+++ b/src/utils/indxbib/indxbib.cpp
@@ -148,11 +148,7 @@ int main(int argc, char **argv)
{
int requested_hash_table_size;
check_integer_arg('h', optarg, 2, &requested_hash_table_size);
- hash_table_size = requested_hash_table_size;
- if ((hash_table_size > 2) && (hash_table_size % 2) == 0)
- hash_table_size++;
- while (!is_prime(hash_table_size))
- hash_table_size += 2;
+ hash_table_size = ceil_prime(requested_hash_table_size);
if (hash_table_size != requested_hash_table_size)
warning("requested hash table size %1 is not prime: using %2"
" instead", optarg, hash_table_size);