From dafbc2cdb8825ed36a25f9a6275d5226f35d3bd3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 10 Dec 2018 15:38:52 +0100 Subject: [PATCH] bc: simplify bc_num_parseDecimal() further function old new delta bc_program_num 925 912 -13 Signed-off-by: Denys Vlasenko --- 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, -- 2.25.1