Refactor bcmath

This commit is contained in:
Xinchen Hui 2014-05-05 14:26:23 +08:00
parent 7e3989f301
commit fee9d4cafd
3 changed files with 40 additions and 55 deletions

View file

@ -40,40 +40,42 @@
/* Convert a numbers to a string. Base 10 only.*/
char
zend_string
*bc_num2str (num)
bc_num num;
{
char *str, *sptr;
char *nptr;
int index, signch;
zend_string *str;
char *sptr;
char *nptr;
int index, signch;
/* Allocate the string memory. */
signch = ( num->n_sign == PLUS ? 0 : 1 ); /* Number of sign chars. */
if (num->n_scale > 0)
str = (char *) safe_emalloc (1, num->n_len + num->n_scale, 2 + signch);
else
str = (char *) safe_emalloc (1, num->n_len, 1 + signch);
if (str == NULL) bc_out_of_memory();
/* Allocate the string memory. */
signch = ( num->n_sign == PLUS ? 0 : 1 ); /* Number of sign chars. */
if (num->n_scale > 0)
str = STR_ALLOC(num->n_len + num->n_scale + signch + 1, 0);
else
str = STR_ALLOC(num->n_len + signch, 0);
if (str == NULL) bc_out_of_memory();
/* The negative sign if needed. */
sptr = str;
if (signch) *sptr++ = '-';
/* The negative sign if needed. */
sptr = str->val;
if (signch) *sptr++ = '-';
/* Load the whole number. */
nptr = num->n_value;
for (index=num->n_len; index>0; index--)
*sptr++ = BCD_CHAR(*nptr++);
/* Load the whole number. */
nptr = num->n_value;
for (index=num->n_len; index>0; index--)
*sptr++ = BCD_CHAR(*nptr++);
/* Now the fraction. */
if (num->n_scale > 0)
{
*sptr++ = '.';
for (index=0; index<num->n_scale; index++)
*sptr++ = BCD_CHAR(*nptr++);
}
/* Now the fraction. */
if (num->n_scale > 0)
{
*sptr++ = '.';
for (index=0; index<num->n_scale; index++)
*sptr++ = BCD_CHAR(*nptr++);
}
/* Terminate the string and return it! */
*sptr = '\0';
return (str);
/* Terminate the string and return it! */
*sptr = '\0';
str->len = sptr - (char *)str->val;
return str;
}