Skip to content

Commit f587e6a

Browse files
committed
Fixed compilation issues with MSVC 6: replace usage of ostringstream with valueToString to support 64 bits integer and high precision floating point conversion to string. Replace usage of ULL and LL literal with UInt64(expr) and Int64(expr). Introduced helper function uint64ToDouble() to work-around missing conversion. Unit tests do not pass yet.
1 parent f0b24e7 commit f587e6a

4 files changed

Lines changed: 111 additions & 67 deletions

File tree

src/lib_json/json_value.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,28 @@ const LargestUInt Value::maxLargestUInt = LargestUInt(-1);
4848
/// Unknown size marker
4949
static const unsigned int unknown = (unsigned)-1;
5050

51+
#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
52+
template <typename T, typename U>
53+
static inline bool InRange(double d, T min, U max) {
54+
return d >= min && d <= max;
55+
}
56+
#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
57+
static inline double integerToDouble( Json::UInt64 value )
58+
{
59+
return static_cast<double>( UInt(value >> 32) ) * (UInt64(1)<<32) + UInt(value & 0xffffffff);
60+
}
61+
62+
template<typename T>
63+
static inline double integerToDouble( T value )
64+
{
65+
return static_cast<double>( value );
66+
}
67+
5168
template <typename T, typename U>
5269
static inline bool InRange(double d, T min, U max) {
53-
return d >= min && d <= max;
70+
return d >= integerToDouble(min) && d <= integerToDouble(max);
5471
}
72+
#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
5573

5674

5775
/** Duplicates the specified string value.
@@ -673,9 +691,6 @@ Value::asCString() const
673691
std::string
674692
Value::asString() const
675693
{
676-
// Let the STL sort it out for numeric types.
677-
std::ostringstream oss;
678-
679694
switch ( type_ )
680695
{
681696
case nullValue:
@@ -685,18 +700,14 @@ Value::asString() const
685700
case booleanValue:
686701
return value_.bool_ ? "true" : "false";
687702
case intValue:
688-
oss << value_.int_;
689-
break;
703+
return valueToString( value_.int_ );
690704
case uintValue:
691-
oss << value_.uint_;
692-
break;
705+
return valueToString( value_.uint_ );
693706
case realValue:
694-
oss << value_.real_;
695-
break;
707+
return valueToString( value_.real_ );
696708
default:
697709
JSON_FAIL_MESSAGE( "Type is not convertible to string" );
698710
}
699-
return oss.str();
700711
}
701712

702713
# ifdef JSON_USE_CPPTL
@@ -842,7 +853,7 @@ Value::asDouble() const
842853
#if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
843854
return static_cast<double>( value_.uint_ );
844855
#else // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
845-
return static_cast<double>( Int(value_.uint_/2) ) * 2 + Int(value_.uint_ & 1);
856+
return integerToDouble( value_.uint_ );
846857
#endif // if !defined(JSON_USE_INT64_DOUBLE_CONVERSION)
847858
case realValue:
848859
return value_.real_;

src/test_lib_json/jsontest.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,23 @@ TestResult::addToLastFailure( const std::string &message )
249249
return *this;
250250
}
251251

252+
TestResult &
253+
TestResult::operator << ( Json::Int64 value ) {
254+
return addToLastFailure( Json::valueToString(value) );
255+
}
256+
257+
258+
TestResult &
259+
TestResult::operator << ( Json::UInt64 value ) {
260+
return addToLastFailure( Json::valueToString(value) );
261+
}
262+
263+
264+
TestResult &
265+
TestResult::operator << ( bool value ) {
266+
return addToLastFailure(value ? "true" : "false");
267+
}
268+
252269

253270
// class TestCase
254271
// //////////////////////////////////////////////////////////////////

src/test_lib_json/jsontest.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# include <json/config.h>
1010
# include <json/value.h>
11+
# include <json/writer.h>
1112
# include <stdio.h>
1213
# include <deque>
1314
# include <sstream>
@@ -90,14 +91,17 @@ namespace JsonTest {
9091
template <typename T>
9192
TestResult &operator << ( const T& value ) {
9293
std::ostringstream oss;
94+
oss.precision( 16 );
95+
oss.setf( std::ios_base::floatfield );
9396
oss << value;
9497
return addToLastFailure(oss.str());
9598
}
9699

97100
// Specialized versions.
98-
TestResult &operator << ( bool value ) {
99-
return addToLastFailure(value ? "true" : "false");
100-
}
101+
TestResult &operator << ( bool value );
102+
// std:ostream does not support 64bits integers on all STL implementation
103+
TestResult &operator << ( Json::Int64 value );
104+
TestResult &operator << ( Json::UInt64 value );
101105

102106
private:
103107
TestResult &addToLastFailure( const std::string &message );
@@ -195,6 +199,7 @@ namespace JsonTest {
195199
return result;
196200
}
197201

202+
198203
TestResult &
199204
checkStringEqual( TestResult &result,
200205
const std::string &expected, const std::string &actual,

0 commit comments

Comments
 (0)