diff options
Diffstat (limited to 'ext/bcmath/libbcmath/src/num2str.c')
-rw-r--r-- | ext/bcmath/libbcmath/src/num2str.c | 58 |
1 files changed, 30 insertions, 28 deletions
diff --git a/ext/bcmath/libbcmath/src/num2str.c b/ext/bcmath/libbcmath/src/num2str.c index 14c57726fe..e38bf87797 100644 --- a/ext/bcmath/libbcmath/src/num2str.c +++ b/ext/bcmath/libbcmath/src/num2str.c @@ -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 = zend_string_alloc(num->n_len + num->n_scale + signch + 1, 0); + else + str = zend_string_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; } |