[git commit] bc: simplify bc_num_parseDecimal() further

Denys Vlasenko vda.linux at googlemail.com
Mon Dec 10 14:38:52 UTC 2018


commit: https://git.busybox.net/busybox/commit/?id=dafbc2cdb8825ed36a25f9a6275d5226f35d3bd3
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
bc_program_num                                       925     912     -13

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 miscutils/bc.c | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

diff --git a/miscutils/bc.c b/miscutils/bc.c
index 7ed6dd91d..26ab94cbd 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -2395,19 +2395,11 @@ static void bc_num_parseDecimal(BcNum *n, const char *val)
 {
 	size_t len, i;
 	const char *ptr;
-	bool zero;
 
 	len = strlen(val);
 	if (len == 0)
 		return;
 
-	zero = true;
-	for (i = 0; val[i]; ++i) {
-		if (val[i] != '0' && val[i] != '.') {
-			zero = false;
-			break;
-		}
-	}
 	bc_num_expand(n, len);
 
 	ptr = strchr(val, '.');
@@ -2416,16 +2408,21 @@ static void bc_num_parseDecimal(BcNum *n, const char *val)
 	if (ptr != NULL)
 		n->rdx = (size_t)((val + len) - (ptr + 1));
 
-	if (!zero) {
-		i = len - 1;
-		for (;;) {
-			n->num[n->len] = val[i] - '0';
-			++n->len;
+	for (i = 0; val[i]; ++i) {
+		if (val[i] != '0' && val[i] != '.') {
+			// Not entirely zero value - convert it, and exit
+			i = len - 1;
+			for (;;) {
+				n->num[n->len] = val[i] - '0';
+				++n->len;
  skip_dot:
-			if ((ssize_t)--i == (ssize_t)-1) break;
-			if (val[i] == '.') goto skip_dot;
+				if ((ssize_t)--i == (ssize_t)-1) break;
+				if (val[i] == '.') goto skip_dot;
+			}
+			break;
 		}
 	}
+	// if this is reached, the value is entirely zero
 }
 
 // Note: n is already "bc_num_zero()"ed,


More information about the busybox-cvs mailing list