summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2010-08-29 15:15:21 +0000
committerarseny.kapoulkine <arseny.kapoulkine@99668b35-9821-0410-8761-19e4c4f06640>2010-08-29 15:15:21 +0000
commit3284cc2f1b9508ae4708c4e95e7b92c836682b79 (patch)
tree8fed9d5915cf1600ce71b1412ca1b6a75f418cb0 /src
parent7aadde422950e7787ed13c56f3ca1c6504cb3634 (diff)
XPath: Number to string conversion uses ecvt_s under MSVC8+ (major speedup), minor conversion refactoring
git-svn-id: http://pugixml.googlecode.com/svn/trunk@649 99668b35-9821-0410-8761-19e4c4f06640
Diffstat (limited to 'src')
-rw-r--r--src/pugixpath.cpp37
1 files changed, 29 insertions, 8 deletions
diff --git a/src/pugixpath.cpp b/src/pugixpath.cpp
index a30ec55..e866c4f 100644
--- a/src/pugixpath.cpp
+++ b/src/pugixpath.cpp
@@ -337,11 +337,35 @@ namespace
return (value != 0 && !is_nan(value));
}
+ void truncate_zeros(char* begin, char* end)
+ {
+ while (begin != end && end[-1] == '0') end--;
+
+ *end = 0;
+ }
+
// gets mantissa digits in the form of 0.xxxxx with 0. implied and the exponent
- void convert_number_to_mantissa_exponent(double value, char* buffer, char** out_mantissa, int* out_exponent)
+#if defined(_MSC_VER) && _MSC_VER >= 1400
+ void convert_number_to_mantissa_exponent(double value, char* buffer, size_t buffer_size, char** out_mantissa, int* out_exponent)
+ {
+ // get base values
+ int sign, exponent;
+ _ecvt_s(buffer, buffer_size, value, DBL_DIG + 1, &exponent, &sign);
+
+ // truncate redundant zeros
+ truncate_zeros(buffer, buffer + strlen(buffer));
+
+ // fill results
+ *out_mantissa = buffer;
+ *out_exponent = exponent;
+ }
+#else
+ void convert_number_to_mantissa_exponent(double value, char* buffer, size_t buffer_size, char** out_mantissa, int* out_exponent)
{
// get a scientific notation value with IEEE DBL_DIG decimals
- sprintf(buffer, "%.15e", value);
+ sprintf(buffer, "%.*e", DBL_DIG, value);
+ assert(strlen(buffer) < buffer_size);
+ (void)!buffer_size;
// get the exponent (possibly negative)
char* exponent_string = strchr(buffer, 'e');
@@ -359,16 +383,13 @@ namespace
exponent++;
// remove extra mantissa digits and zero-terminate mantissa
- char* mantissa_end = exponent_string;
-
- while (mantissa != mantissa_end && *(mantissa_end - 1) == '0') --mantissa_end;
-
- *mantissa_end = 0;
+ truncate_zeros(mantissa, exponent_string);
// fill results
*out_mantissa = mantissa;
*out_exponent = exponent;
}
+#endif
string_t convert_number_to_string(double value)
{
@@ -381,7 +402,7 @@ namespace
char* mantissa;
int exponent;
- convert_number_to_mantissa_exponent(value, mantissa_buffer, &mantissa, &exponent);
+ convert_number_to_mantissa_exponent(value, mantissa_buffer, sizeof(mantissa_buffer), &mantissa, &exponent);
// make the number!
char_t result[512];