Use buffer with a static size in convert_number_to_mantissa_exponent

We use references to arrays elsewhere in the codebase and there's just
one caller for this function so it's easier to fix the size.

This will simplify snprintf refactoring.
This commit is contained in:
Arseny Kapoulkine 2017-06-15 22:58:46 -07:00
parent cd2804d3ee
commit 207bc788e9

View file

@ -7998,11 +7998,11 @@ PUGI__NS_BEGIN
// gets mantissa digits in the form of 0.xxxxx with 0. implied and the exponent
#if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 && !defined(_WIN32_WCE)
PUGI__FN void convert_number_to_mantissa_exponent(double value, char* buffer, size_t buffer_size, char** out_mantissa, int* out_exponent)
PUGI__FN void convert_number_to_mantissa_exponent(double value, char (&buffer)[32], char** out_mantissa, int* out_exponent)
{
// get base values
int sign, exponent;
_ecvt_s(buffer, buffer_size, value, DBL_DIG + 1, &exponent, &sign);
_ecvt_s(buffer, sizeof(buffer), value, DBL_DIG + 1, &exponent, &sign);
// truncate redundant zeros
truncate_zeros(buffer, buffer + strlen(buffer));
@ -8012,20 +8012,18 @@ PUGI__NS_BEGIN
*out_exponent = exponent;
}
#else
PUGI__FN void convert_number_to_mantissa_exponent(double value, char* buffer, size_t buffer_size, char** out_mantissa, int* out_exponent)
PUGI__FN void convert_number_to_mantissa_exponent(double value, char (&buffer)[32], char** out_mantissa, int* out_exponent)
{
// get a scientific notation value with IEEE DBL_DIG decimals
#if defined(PUGI__HAVE_SNPRINTF)
snprintf(buffer, buffer_size, "%.*e", DBL_DIG, value);
snprintf(buffer, 32, "%.*e", DBL_DIG, value);
#elif defined(PUGI__MSVC_CRT_VERSION)
_snprintf(buffer, buffer_size, "%.*e", DBL_DIG, value);
buffer[buffer_size - 1] = '\0';
_snprintf(buffer, 32, "%.*e", DBL_DIG, value);
buffer[31] = '\0';
#else
sprintf(buffer, "%.*e", DBL_DIG, value);
assert(strlen(buffer) < buffer_size);
#endif
// get the exponent (possibly negative)
char* exponent_string = strchr(buffer, 'e');
assert(exponent_string);
@ -8061,7 +8059,7 @@ PUGI__NS_BEGIN
char* mantissa;
int exponent;
convert_number_to_mantissa_exponent(value, mantissa_buffer, sizeof(mantissa_buffer), &mantissa, &exponent);
convert_number_to_mantissa_exponent(value, mantissa_buffer, &mantissa, &exponent);
// allocate a buffer of suitable length for the number
size_t result_size = strlen(mantissa_buffer) + (exponent > 0 ? exponent : -exponent) + 4;