aboutsummaryrefslogtreecommitdiff
path: root/miscutils/bc.c
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-02-26 14:05:28 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2021-02-26 14:23:13 +0100
commitace81cd46ce53e60fe702cc1ac857989207e7ac4 (patch)
treef9d8bd67c672ad9c586dd85e03f976bd2206fe71 /miscutils/bc.c
parent3d88cc1d371a4a5ae8e1521a1e915479bca52959 (diff)
downloadbusybox-w32-ace81cd46ce53e60fe702cc1ac857989207e7ac4.tar.gz
busybox-w32-ace81cd46ce53e60fe702cc1ac857989207e7ac4.tar.bz2
busybox-w32-ace81cd46ce53e60fe702cc1ac857989207e7ac4.zip
bc/dc: fix length(0) and length(0.000nnn) result
function old new delta zxc_vm_process 6464 6498 +34 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils/bc.c')
-rw-r--r--miscutils/bc.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/miscutils/bc.c b/miscutils/bc.c
index 84bbe7b14..91564099e 100644
--- a/miscutils/bc.c
+++ b/miscutils/bc.c
@@ -6259,13 +6259,20 @@ static unsigned long xc_program_len(BcNum *n)
6259{ 6259{
6260 size_t len = n->len; 6260 size_t len = n->len;
6261 6261
6262 if (n->rdx != len) return len; 6262 if (n->rdx != len)
6263 // length(100): rdx 0 len 3, return 3
6264 // length(0.01-0.01): rdx 2 len 0, return 2
6265 // dc: 0.01 0.01 - Zp: rdx 2 len 0, return 1
6266 return len != 0 ? len : (IS_BC ? n->rdx : 1);
6267
6268 // length(0): return 1
6269 // length(0.000nnn): count nnn
6263 for (;;) { 6270 for (;;) {
6264 if (len == 0) break; 6271 if (len == 0) break;
6265 len--; 6272 len--;
6266 if (n->num[len] != 0) break; 6273 if (n->num[len] != 0) break;
6267 } 6274 }
6268 return len; 6275 return len + 1;
6269} 6276}
6270 6277
6271static BC_STATUS zxc_program_builtin(char inst) 6278static BC_STATUS zxc_program_builtin(char inst)
@@ -6293,12 +6300,12 @@ static BC_STATUS zxc_program_builtin(char inst)
6293 if (inst == XC_INST_SQRT) 6300 if (inst == XC_INST_SQRT)
6294 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale); 6301 s = zbc_num_sqrt(num, &res.d.n, G.prog.scale);
6295#if ENABLE_BC 6302#if ENABLE_BC
6296 else if (len != 0 && opnd->t == XC_RESULT_ARRAY) { 6303 else if (len && opnd->t == XC_RESULT_ARRAY) {
6297 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len); 6304 bc_num_ulong2num(&res.d.n, (unsigned long) ((BcVec *) num)->len);
6298 } 6305 }
6299#endif 6306#endif
6300#if ENABLE_DC 6307#if ENABLE_DC
6301 else if (len != 0 && !BC_PROG_NUM(opnd, num)) { 6308 else if (len && !BC_PROG_NUM(opnd, num)) {
6302 char **str; 6309 char **str;
6303 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx; 6310 size_t idx = opnd->t == XC_RESULT_STR ? opnd->d.id.idx : num->rdx;
6304 6311
@@ -6307,6 +6314,8 @@ static BC_STATUS zxc_program_builtin(char inst)
6307 } 6314 }
6308#endif 6315#endif
6309 else { 6316 else {
6317//TODO: length(.00) and scale(.00) should return 2, they return 1 and 0 now
6318//(don't forget to check that dc Z and X commands do not break)
6310 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num)); 6319 bc_num_ulong2num(&res.d.n, len ? xc_program_len(num) : xc_program_scale(num));
6311 } 6320 }
6312 6321