aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tag.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2015-07-25 14:28:59 +0000
committerIngo Schwarze <schwarze@openbsd.org>2015-07-25 14:28:59 +0000
commitd7a1c33838e142532c49cdda9ea67e18aad92683 (patch)
treecf7e98dba0f9b17d889575bbb52feccb941cf7b4 /tag.c
parent7310229a6bc4ea0f603478894a168e2aa4dff9e6 (diff)
downloadmandoc-d7a1c33838e142532c49cdda9ea67e18aad92683.tar.gz
mandoc-d7a1c33838e142532c49cdda9ea67e18aad92683.tar.zst
mandoc-d7a1c33838e142532c49cdda9ea67e18aad92683.zip
Simplify and make tag_put() more efficient by integrating tag_get()
into it and by only handling NUL-terminated strings. Minus 25 lines of code, no functional change.
Diffstat (limited to 'tag.c')
-rw-r--r--tag.c42
1 files changed, 10 insertions, 32 deletions
diff --git a/tag.c b/tag.c
index 8ce576f4..02020663 100644
--- a/tag.c
+++ b/tag.c
@@ -1,4 +1,4 @@
-/* $Id: tag.c,v 1.4 2015/07/25 14:02:06 schwarze Exp $ */
+/* $Id: tag.c,v 1.5 2015/07/25 14:28:59 schwarze Exp $ */
/*
* Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
*
@@ -78,49 +78,27 @@ tag_init(void)
}
/*
- * Return the line number where a term is defined,
- * or 0 if the term is unknown.
- */
-size_t
-tag_get(const char *s, size_t len, int prio)
-{
- struct tag_entry *entry;
- const char *end;
- unsigned int slot;
-
- if (tag_fd == -1)
- return(0);
- if (len == 0)
- len = strlen(s);
- end = s + len;
- slot = ohash_qlookupi(&tag_data, s, &end);
- entry = ohash_find(&tag_data, slot);
- return((entry == NULL || prio < entry->prio) ? 0 : entry->line);
-}
-
-/*
- * Set the line number where a term is defined.
+ * Set the line number where a term is defined,
+ * unless it is already defined at a higher priority.
*/
void
-tag_put(const char *s, size_t len, int prio, size_t line)
+tag_put(const char *s, int prio, size_t line)
{
struct tag_entry *entry;
- const char *end;
+ size_t len;
unsigned int slot;
if (tag_fd == -1)
return;
- if (len == 0)
- len = strlen(s);
- end = s + len;
- slot = ohash_qlookupi(&tag_data, s, &end);
+ slot = ohash_qlookup(&tag_data, s);
entry = ohash_find(&tag_data, slot);
if (entry == NULL) {
- entry = mandoc_malloc(sizeof(*entry) + len + 1);
+ len = strlen(s) + 1;
+ entry = mandoc_malloc(sizeof(*entry) + len);
memcpy(entry->s, s, len);
- entry->s[len] = '\0';
ohash_insert(&tag_data, slot, entry);
- }
+ } else if (entry->prio <= prio)
+ return;
entry->line = line;
entry->prio = prio;
}