Merge pull request #30 from mloy/redundant-strlen

This commit is contained in:
Christopher Dunn 2014-09-03 14:07:40 -07:00
commit 033677cc1a
2 changed files with 11 additions and 12 deletions

View File

@ -105,8 +105,7 @@ static inline char *duplicateStringValue(const char *value,
/** Free the string duplicated by duplicateStringValue(). /** Free the string duplicated by duplicateStringValue().
*/ */
static inline void releaseStringValue(char *value) { static inline void releaseStringValue(char *value) {
if (value) free(value);
free(value);
} }
} // namespace Json } // namespace Json

View File

@ -67,6 +67,7 @@ std::string valueToString(double value) {
// Allocate a buffer that is more than large enough to store the 16 digits of // Allocate a buffer that is more than large enough to store the 16 digits of
// precision requested below. // precision requested below.
char buffer[32]; char buffer[32];
int len = -1;
// Print into the buffer. We need not request the alternative representation // Print into the buffer. We need not request the alternative representation
// that always has a decimal point because JSON doesn't distingish the // that always has a decimal point because JSON doesn't distingish the
@ -75,36 +76,35 @@ std::string valueToString(double value) {
// visual studio 2005 to // visual studio 2005 to
// avoid warning. // avoid warning.
#if defined(WINCE) #if defined(WINCE)
_snprintf(buffer, sizeof(buffer), "%.16g", value); len = _snprintf(buffer, sizeof(buffer), "%.16g", value);
#else #else
sprintf_s(buffer, sizeof(buffer), "%.16g", value); len = sprintf_s(buffer, sizeof(buffer), "%.16g", value);
#endif #endif
#else #else
if ( isfinite( value )) if ( isfinite( value ))
{ {
snprintf(buffer, sizeof(buffer), "%.16g", value); len = snprintf(buffer, sizeof(buffer), "%.16g", value);
} }
else else
{ {
// IEEE standard states that NaN values will not compare to themselves // IEEE standard states that NaN values will not compare to themselves
if ( value != value) if ( value != value)
{ {
snprintf(buffer, sizeof(buffer), "null"); len = snprintf(buffer, sizeof(buffer), "null");
} }
else if ( value < 0) else if ( value < 0)
{ {
snprintf(buffer, sizeof(buffer), "-1e+9999"); len = snprintf(buffer, sizeof(buffer), "-1e+9999");
} }
else else
{ {
snprintf(buffer, sizeof(buffer), "1e+9999"); len = snprintf(buffer, sizeof(buffer), "1e+9999");
} }
// nothing more to do, return. // For those, we do not need to call fixNumLoc, but it is fast.
return buffer;
} }
#endif #endif
fixNumericLocale(buffer, buffer + strlen(buffer)); assert(len>=0);
fixNumericLocale(buffer, buffer + len);
return buffer; return buffer;
} }