Refactor a function to not use a pointer when it could return a value
Signed-off-by: Gavin Howard <gavin@yzena.com>
diff --git a/include/num.h b/include/num.h
index ae336ce..b532639 100644
--- a/include/num.h
+++ b/include/num.h
@@ -188,7 +188,7 @@
size_t bc_num_scale(const BcNum *restrict n);
size_t bc_num_len(const BcNum *restrict n);
-void bc_num_bigdig(const BcNum *restrict n, BcBigDig *result);
+BcBigDig bc_num_bigdig(const BcNum *restrict n);
BcBigDig bc_num_bigdig2(const BcNum *restrict n);
void bc_num_bigdig2num(BcNum *restrict n, BcBigDig val);
diff --git a/src/library.c b/src/library.c
index 930f2ff..eb5dad6 100644
--- a/src/library.c
+++ b/src/library.c
@@ -513,7 +513,7 @@
assert(num != NULL && num->num != NULL);
- bc_num_bigdig(num, result);
+ *result = bc_num_bigdig(num);
err:
bcl_num_dtor(ctxt, n, num);
diff --git a/src/num.c b/src/num.c
index b9cc896..c7e1f84 100644
--- a/src/num.c
+++ b/src/num.c
@@ -596,15 +596,15 @@
}
#if BC_ENABLE_EXTRA_MATH
-static void bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
- BcBigDig *v)
+static BcBigDig bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c)
{
BcNum temp;
if (BC_ERR(bc_num_nonInt(b, &temp))) bc_err(BC_ERR_MATH_NON_INTEGER);
bc_num_copy(c, a);
- bc_num_bigdig(&temp, v);
+
+ return bc_num_bigdig(&temp);
}
#endif // BC_ENABLE_EXTRA_MATH
@@ -1339,7 +1339,7 @@
neg = BC_NUM_NEG_NP(btemp);
BC_NUM_NEG_CLR_NP(btemp);
- bc_num_bigdig(&btemp, &pow);
+ pow = bc_num_bigdig(&btemp);
BC_SIG_LOCK;
@@ -1395,11 +1395,11 @@
#if BC_ENABLE_EXTRA_MATH
static void bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
- BcBigDig val = 0;
+ BcBigDig val;
BC_UNUSED(scale);
- bc_num_intop(a, b, c, &val);
+ val = bc_num_intop(a, b, c);
if (val < c->scale) bc_num_truncate(c, c->scale - val);
else if (val > c->scale) bc_num_extend(c, val - c->scale);
@@ -1407,22 +1407,22 @@
static void bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
- BcBigDig val = 0;
+ BcBigDig val;
BC_UNUSED(scale);
- bc_num_intop(a, b, c, &val);
+ val = bc_num_intop(a, b, c);
bc_num_shiftLeft(c, (size_t) val);
}
static void bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
- BcBigDig val = 0;
+ BcBigDig val;
BC_UNUSED(scale);
- bc_num_intop(a, b, c, &val);
+ val = bc_num_intop(a, b, c);
if (BC_NUM_ZERO(c)) return;
@@ -2279,14 +2279,14 @@
return r;
}
-void bc_num_bigdig(const BcNum *restrict n, BcBigDig *result) {
+BcBigDig bc_num_bigdig(const BcNum *restrict n) {
- assert(n != NULL && result != NULL);
+ assert(n != NULL);
if (BC_ERR(BC_NUM_NEG(n))) bc_err(BC_ERR_MATH_NEGATIVE);
if (BC_ERR(bc_num_cmp(n, &vm.max) >= 0)) bc_err(BC_ERR_MATH_OVERFLOW);
- *result = bc_num_bigdig2(n);
+ return bc_num_bigdig2(n);
}
void bc_num_bigdig2num(BcNum *restrict n, BcBigDig val) {
diff --git a/src/program.c b/src/program.c
index e6bd7b1..6516a52 100644
--- a/src/program.c
+++ b/src/program.c
@@ -921,7 +921,7 @@
BcBigDig *ptr, *ptr_t, val, max, min;
BcErr e;
- bc_num_bigdig(l, &val);
+ val = bc_num_bigdig(l);
e = left->t - BC_RESULT_IBASE + BC_ERR_EXEC_IBASE;
if (sc) {
@@ -1029,7 +1029,7 @@
#endif // BC_ENABLED
bc_program_prep(p, &operand, &num, 0);
- bc_num_bigdig(num, &temp);
+ temp = bc_num_bigdig(num);
r.t = BC_RESULT_ARRAY_ELEM;
r.d.loc.idx = (size_t) temp;
@@ -1499,7 +1499,7 @@
else {
bc_program_prep(p, &opnd, &num, 0);
- bc_num_bigdig(num, &val);
+ val = bc_num_bigdig(num);
bc_vec_pop(&p->results);
}