Skip to content
34 changes: 31 additions & 3 deletions ext/bcmath/libbcmath/src/convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ static inline BC_VECTOR bc_partial_convert_to_vector(const char *n, size_t len)
}

BC_VECTOR num = 0;
BC_VECTOR base = 1;
BC_VECTOR digit_base_value = 1;

for (size_t i = 0; i < len; i++) {
num += *n * base;
base *= BASE;
num += *n * digit_base_value;
digit_base_value *= BASE;
n--;
}

Expand All @@ -57,4 +57,32 @@ static inline void bc_convert_to_vector(BC_VECTOR *n_vector, const char *nend, s
}
}

static inline void bc_convert_to_vector_with_zero_pad(BC_VECTOR *n_vector, const char *nend, size_t nlen, size_t zeros)
{
while (zeros >= BC_VECTOR_SIZE) {
*n_vector = 0;
n_vector++;
zeros -= BC_VECTOR_SIZE;
}

if (zeros > 0) {
*n_vector = 0;
BC_VECTOR digit_base_value = BC_POW_10_LUT[zeros];
size_t len_to_write = MIN(BC_VECTOR_SIZE - zeros, nlen);
for (size_t i = 0; i < len_to_write; i++) {
*n_vector += *nend * digit_base_value;
digit_base_value *= BASE;
nend--;
}
n_vector++;
nlen -= len_to_write;
}

if (nlen == 0) {
return;
}

bc_convert_to_vector(n_vector, nend, nlen);
}

#endif
Loading
Loading