source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: mdocml: In a2roffsu(), do not parse the number twice.
Date: Mon, 22 Dec 2014 22:28:31 -0500 (EST)	[thread overview]
Message-ID: <3806633831723784142.enqueue@fantadrom.bsd.lv> (raw)

Log Message:
-----------
In a2roffsu(), do not parse the number twice.
Gets rid of 25 lines of code and one static buffer.
No functional change for numbers shorter than BUFSIZ characters.

Modified Files:
--------------
    mdocml:
        out.c

Revision Data
-------------
Index: out.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/out.c,v
retrieving revision 1.54
retrieving revision 1.55
diff -Lout.c -Lout.c -u -p -r1.54 -r1.55
--- out.c
+++ out.c
@@ -20,8 +20,6 @@
 #include <sys/types.h>
 
 #include <assert.h>
-#include <ctype.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
@@ -39,53 +37,23 @@ static	void	tblcalc_number(struct rofftb
 
 
 /*
- * Convert a `scaling unit' to a consistent form, or fail.  Scaling
- * units are documented in groff.7, mdoc.7, man.7.
+ * Parse the *src string and store a scaling unit into *dst.
+ * If the string doesn't specify the unit, use the default.
+ * If no default is specified, fail.
+ * Return 1 on success and 0 on failure.
  */
 int
 a2roffsu(const char *src, struct roffsu *dst, enum roffscale def)
 {
-	char		 buf[BUFSIZ], hasd;
-	int		 i;
+	char		*endptr;
+	double		 scale;
 	enum roffscale	 unit;
 
-	if ('\0' == *src)
+	scale = strtod(src, &endptr);
+	if (endptr == src || (endptr[0] != '\0' && endptr[1] != '\0'))
 		return(0);
 
-	i = hasd = 0;
-
-	switch (*src) {
-	case '+':
-		src++;
-		break;
-	case '-':
-		buf[i++] = *src++;
-		break;
-	default:
-		break;
-	}
-
-	if ('\0' == *src)
-		return(0);
-
-	while (i < BUFSIZ) {
-		if ( ! isdigit((unsigned char)*src)) {
-			if ('.' != *src)
-				break;
-			else if (hasd)
-				break;
-			else
-				hasd = 1;
-		}
-		buf[i++] = *src++;
-	}
-
-	if (BUFSIZ == i || (*src && *(src + 1)))
-		return(0);
-
-	buf[i] = '\0';
-
-	switch (*src) {
+	switch (*endptr) {
 	case 'c':
 		unit = SCALE_CM;
 		break;
@@ -126,8 +94,9 @@ a2roffsu(const char *src, struct roffsu 
 	}
 
 	/* FIXME: do this in the caller. */
-	if ((dst->scale = atof(buf)) < 0.0)
-		dst->scale = 0.0;
+	if (scale < 0.0)
+		scale = 0.0;
+	dst->scale = scale;
 	dst->unit = unit;
 	return(1);
 }
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

                 reply	other threads:[~2014-12-23  3:28 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=3806633831723784142.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).