aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tag.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2020-01-20 10:37:15 +0000
committerIngo Schwarze <schwarze@openbsd.org>2020-01-20 10:37:15 +0000
commit3127746e1c5864e767914a2fbeaf4564be79e2c8 (patch)
tree7dbabb7d3a52079ddbb213926efae06a13934afa /tag.c
parent23f80e14e78282088dabbf1cfe358c07030dc9e7 (diff)
downloadmandoc-3127746e1c5864e767914a2fbeaf4564be79e2c8.tar.gz
mandoc-3127746e1c5864e767914a2fbeaf4564be79e2c8.tar.zst
mandoc-3127746e1c5864e767914a2fbeaf4564be79e2c8.zip
Make the code more readable by introducing
symbolic constants for tagging priorities. This review also made me find a minor bug: do not upgrade TAG_FALLBACK to TAG_WEAK when there is trailing whitespace.
Diffstat (limited to 'tag.c')
-rw-r--r--tag.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/tag.c b/tag.c
index c98c7b84..6df91c8b 100644
--- a/tag.c
+++ b/tag.c
@@ -1,4 +1,4 @@
-/* $Id: tag.c,v 1.26 2020/01/19 18:02:00 schwarze Exp $ */
+/* $Id: tag.c,v 1.27 2020/01/20 10:37:15 schwarze Exp $ */
/*
* Copyright (c) 2015,2016,2018,2019,2020 Ingo Schwarze <schwarze@openbsd.org>
*
@@ -18,6 +18,7 @@
#include <sys/types.h>
+#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
@@ -146,6 +147,7 @@ tag_put(const char *s, int prio, size_t line)
size_t len;
unsigned int slot;
+ assert(prio <= TAG_FALLBACK);
if (tag_files.tfd <= 0)
return;
@@ -162,8 +164,8 @@ tag_put(const char *s, int prio, size_t line)
return;
se = s + len;
- if (*se != '\0')
- prio = INT_MAX;
+ if (*se != '\0' && prio < TAG_WEAK)
+ prio = TAG_WEAK;
slot = ohash_qlookupi(&tag_data, s, &se);
entry = ohash_find(&tag_data, slot);
@@ -183,26 +185,25 @@ tag_put(const char *s, int prio, size_t line)
/*
* Lower priority numbers take precedence,
- * but 0 is special.
- * A tag with priority 0 is only used
+ * but TAG_FALLBACK is special.
+ * A tag with priority TAG_FALLBACK is only used
* if the tag occurs exactly once.
*/
- if (prio == 0) {
- if (entry->prio == 0)
- entry->prio = -1;
+ if (prio == TAG_FALLBACK) {
+ if (entry->prio == TAG_FALLBACK)
+ entry->prio = TAG_DELETE;
return;
}
/* A better entry is already present, ignore the new one. */
- if (entry->prio != -1 && entry->prio < prio)
+ if (entry->prio < prio)
return;
/* The existing entry is worse, clear it. */
- if (entry->prio == -1 || entry->prio == 0 ||
- entry->prio > prio)
+ if (entry->prio > prio)
entry->nlines = 0;
}
@@ -242,7 +243,7 @@ tag_write(void)
empty = 1;
entry = ohash_first(&tag_data, &slot);
while (entry != NULL) {
- if (stream != NULL && entry->prio != -1) {
+ if (stream != NULL && entry->prio < TAG_DELETE) {
for (i = 0; i < entry->nlines; i++) {
fprintf(stream, "%s %s %zu\n",
entry->s, tag_files.ofn, entry->lines[i]);