source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: mdocml: Simplify and make tag_put() more efficient by integrating
Date: Sat, 25 Jul 2015 09:29:29 -0500 (EST)	[thread overview]
Message-ID: <10579492136443303552.enqueue@fantadrom.bsd.lv> (raw)

Log Message:
-----------
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.

Modified Files:
--------------
    mdocml:
        mdoc_term.c
        tag.c
        tag.h

Revision Data
-------------
Index: mdoc_term.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mdoc_term.c,v
retrieving revision 1.323
retrieving revision 1.324
diff -Lmdoc_term.c -Lmdoc_term.c -u -p -r1.323 -r1.324
--- mdoc_term.c
+++ mdoc_term.c
@@ -1481,10 +1481,8 @@ termp_fn_pre(DECL_ARGS)
 	term_word(p, n->string);
 	term_fontpop(p);
 
-	if (n->sec == SEC_DESCRIPTION) {
-		if ( ! tag_get(n->string, 0, ++fn_prio))
-			tag_put(n->string, 0, fn_prio, p->line);
-	}
+	if (n->sec == SEC_DESCRIPTION)
+		tag_put(n->string, ++fn_prio, p->line);
 
 	if (pretty) {
 		term_flushln(p);
@@ -2280,9 +2278,8 @@ termp_er_pre(DECL_ARGS)
 	if (n->sec == SEC_ERRORS &&
 	    (n->parent->tok == MDOC_It ||
 	     (n->parent->tok == MDOC_Bq &&
-	      n->parent->parent->parent->tok == MDOC_It)) &&
-	    ! tag_get(n->child->string, 0, 1))
-		tag_put(n->child->string, 0, 1, p->line);
+	      n->parent->parent->parent->tok == MDOC_It)))
+		tag_put(n->child->string, 1, p->line);
 	return(1);
 }
 
@@ -2296,8 +2293,7 @@ termp_tag_pre(DECL_ARGS)
 	    (n->parent->tok == MDOC_It ||
 	     (n->parent->tok == MDOC_Xo &&
 	      n->parent->parent->prev == NULL &&
-	      n->parent->parent->parent->tok == MDOC_It)) &&
-	    ! tag_get(n->child->string, 0, 1))
-		tag_put(n->child->string, 0, 1, p->line);
+	      n->parent->parent->parent->tok == MDOC_It)))
+		tag_put(n->child->string, 1, p->line);
 	return(1);
 }
Index: tag.h
===================================================================
RCS file: /home/cvs/mdocml/mdocml/tag.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -Ltag.h -Ltag.h -u -p -r1.3 -r1.4
--- tag.h
+++ tag.h
@@ -18,8 +18,7 @@
 __BEGIN_DECLS
 
 char	*tag_init(void);
-size_t	 tag_get(const char *, size_t, int);
-void	 tag_put(const char *, size_t, int, size_t);
+void	 tag_put(const char *, int, size_t);
 void	 tag_write(void);
 void	 tag_unlink(void);
 
Index: tag.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/tag.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -Ltag.c -Ltag.c -u -p -r1.4 -r1.5
--- tag.c
+++ tag.c
@@ -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;
 }
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

                 reply	other threads:[~2015-07-25 14:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=10579492136443303552.enqueue@fantadrom.bsd.lv \
    --to=schwarze@mdocml.bsd.lv \
    --cc=source@mdocml.bsd.lv \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).