Skip to content

Commit 033677c

Browse files
committed
Merge pull request #30 from mloy/redundant-strlen
2 parents b02ff20 + 9d69451 commit 033677c

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/lib_json/json_value.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,7 @@ static inline char *duplicateStringValue(const char *value,
105105
/** Free the string duplicated by duplicateStringValue().
106106
*/
107107
static inline void releaseStringValue(char *value) {
108-
if (value)
109-
free(value);
108+
free(value);
110109
}
111110

112111
} // namespace Json

src/lib_json/json_writer.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ std::string valueToString(double value) {
6767
// Allocate a buffer that is more than large enough to store the 16 digits of
6868
// precision requested below.
6969
char buffer[32];
70+
int len = -1;
7071

7172
// Print into the buffer. We need not request the alternative representation
7273
// that always has a decimal point because JSON doesn't distingish the
@@ -75,36 +76,35 @@ std::string valueToString(double value) {
7576
// visual studio 2005 to
7677
// avoid warning.
7778
#if defined(WINCE)
78-
_snprintf(buffer, sizeof(buffer), "%.16g", value);
79+
len = _snprintf(buffer, sizeof(buffer), "%.16g", value);
7980
#else
80-
sprintf_s(buffer, sizeof(buffer), "%.16g", value);
81+
len = sprintf_s(buffer, sizeof(buffer), "%.16g", value);
8182
#endif
8283
#else
8384
if ( isfinite( value ))
8485
{
85-
snprintf(buffer, sizeof(buffer), "%.16g", value);
86+
len = snprintf(buffer, sizeof(buffer), "%.16g", value);
8687
}
8788
else
8889
{
8990
// IEEE standard states that NaN values will not compare to themselves
9091
if ( value != value)
9192
{
92-
snprintf(buffer, sizeof(buffer), "null");
93+
len = snprintf(buffer, sizeof(buffer), "null");
9394
}
9495
else if ( value < 0)
9596
{
96-
snprintf(buffer, sizeof(buffer), "-1e+9999");
97+
len = snprintf(buffer, sizeof(buffer), "-1e+9999");
9798
}
9899
else
99100
{
100-
snprintf(buffer, sizeof(buffer), "1e+9999");
101+
len = snprintf(buffer, sizeof(buffer), "1e+9999");
101102
}
102-
// nothing more to do, return.
103-
return buffer;
103+
// For those, we do not need to call fixNumLoc, but it is fast.
104104
}
105-
106105
#endif
107-
fixNumericLocale(buffer, buffer + strlen(buffer));
106+
assert(len>=0);
107+
fixNumericLocale(buffer, buffer + len);
108108
return buffer;
109109
}
110110

0 commit comments

Comments
 (0)