Commit a7762a34 authored by Milo Yip's avatar Milo Yip

Custom dtoa() impleemntation

Modified from Milo's Grisu2 implementation. 99.9% cases return shortest
decimal format.
parent 69787788
This diff is collapsed.
......@@ -4,9 +4,9 @@
#include "rapidjson.h"
#include "internal/stack.h"
#include "internal/strfunc.h"
#include "internal/dtoa.h"
#include "internal/itoa.h"
#include "stringbuffer.h"
#include <cstdio> // snprintf() or _sprintf_s()
#include <new> // placement new
#ifdef _MSC_VER
......@@ -239,25 +239,17 @@ protected:
bool WriteUint64(uint64_t u64) {
char buffer[20];
const char* end = internal::u64toa(u64, buffer);
for (const char* p = buffer; p != end; ++p)
char* end = internal::u64toa(u64, buffer);
for (char* p = buffer; p != end; ++p)
os_->Put(*p);
return true;
}
#ifdef _MSC_VER
#define RAPIDJSON_SNPRINTF sprintf_s
#else
#define RAPIDJSON_SNPRINTF snprintf
#endif
//! \todo Optimization with custom double-to-string converter.
bool WriteDouble(double d) {
char buffer[100];
int ret = RAPIDJSON_SNPRINTF(buffer, sizeof(buffer), "%.*g", doublePrecision_, d);
RAPIDJSON_ASSERT(ret >= 1);
for (int i = 0; i < ret; i++)
os_->Put(buffer[i]);
char buffer[25];
char* end = internal::dtoa(d, buffer);
for (char* p = buffer; p != end; ++p)
os_->Put(*p);
return true;
}
#undef RAPIDJSON_SNPRINTF
......@@ -403,6 +395,14 @@ inline bool Writer<StringBuffer>::WriteUint64(uint64_t u) {
return true;
}
template<>
inline bool Writer<StringBuffer>::WriteDouble(double d) {
char *buffer = os_->Push(25);
char* end = internal::dtoa(d, buffer);
os_->Pop(25 - (end - buffer));
return true;
}
} // namespace rapidjson
#ifdef _MSC_VER
......
......@@ -60,62 +60,67 @@ TEST(Writer, String) {
TEST_ROUNDTRIP("[\"\\\"\\\\/\\b\\f\\n\\r\\t\"]");
}
TEST(Writer,DoublePrecision) {
const char json[] = "[1.2345,1.2345678,0.123456789012,1234567.8]";
TEST(Writer, Double) {
TEST_ROUNDTRIP("[1.2345,1.2345678,0.123456789012,1234567.8]");
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
const int kDefaultDoublePrecision = 6;
// handling the double precision
EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
writer.SetDoublePrecision(17);
EXPECT_EQ(writer.GetDoublePrecision(), 17);
writer.SetDoublePrecision(-1); // negative equivalent to reset
EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
writer.SetDoublePrecision(1);
writer.SetDoublePrecision(); // reset again
EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
{ // write with explicitly increased precision
StringStream s(json);
Reader reader;
reader.Parse<0>(s, writer.SetDoublePrecision(12));
EXPECT_EQ(writer.GetDoublePrecision(), 12);
EXPECT_STREQ(json, buffer.GetString());
}
{ // explicit individual double precisions
buffer.Clear();
writer.Reset(buffer);
writer.SetDoublePrecision(2);
writer.StartArray();
writer.Double(1.2345, 5);
writer.Double(1.2345678, 9);
writer.Double(0.123456789012, 12);
writer.Double(1234567.8, 8);
writer.EndArray();
EXPECT_EQ(writer.GetDoublePrecision(), 2);
EXPECT_STREQ(json, buffer.GetString());
}
{ // write with default precision (output with precision loss)
Document d;
d.Parse<0>(json);
buffer.Clear();
writer.Reset(buffer);
d.Accept(writer.SetDoublePrecision());
// parsed again to avoid platform-dependent floating point outputs
// (e.g. width of exponents)
d.Parse<0>(buffer.GetString());
EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
EXPECT_DOUBLE_EQ(d[0u].GetDouble(), 1.2345);
EXPECT_DOUBLE_EQ(d[1u].GetDouble(), 1.23457);
EXPECT_DOUBLE_EQ(d[2u].GetDouble(), 0.123457);
EXPECT_DOUBLE_EQ(d[3u].GetDouble(), 1234570);
}
}
//TEST(Writer,DoublePrecision) {
// const char json[] = "[1.2345,1.2345678,0.123456789012,1234567.8]";
//
// StringBuffer buffer;
// Writer<StringBuffer> writer(buffer);
//
// const int kDefaultDoublePrecision = 6;
// // handling the double precision
// EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
// writer.SetDoublePrecision(17);
// EXPECT_EQ(writer.GetDoublePrecision(), 17);
// writer.SetDoublePrecision(-1); // negative equivalent to reset
// EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
// writer.SetDoublePrecision(1);
// writer.SetDoublePrecision(); // reset again
// EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
//
// { // write with explicitly increased precision
// StringStream s(json);
// Reader reader;
// reader.Parse<0>(s, writer.SetDoublePrecision(12));
// EXPECT_EQ(writer.GetDoublePrecision(), 12);
// EXPECT_STREQ(json, buffer.GetString());
// }
// { // explicit individual double precisions
// buffer.Clear();
// writer.Reset(buffer);
// writer.SetDoublePrecision(2);
// writer.StartArray();
// writer.Double(1.2345, 5);
// writer.Double(1.2345678, 9);
// writer.Double(0.123456789012, 12);
// writer.Double(1234567.8, 8);
// writer.EndArray();
//
// EXPECT_EQ(writer.GetDoublePrecision(), 2);
// EXPECT_STREQ(json, buffer.GetString());
// }
// { // write with default precision (output with precision loss)
// Document d;
// d.Parse<0>(json);
// buffer.Clear();
// writer.Reset(buffer);
// d.Accept(writer.SetDoublePrecision());
//
// // parsed again to avoid platform-dependent floating point outputs
// // (e.g. width of exponents)
// d.Parse<0>(buffer.GetString());
// EXPECT_EQ(writer.GetDoublePrecision(), kDefaultDoublePrecision);
// EXPECT_DOUBLE_EQ(d[0u].GetDouble(), 1.2345);
// EXPECT_DOUBLE_EQ(d[1u].GetDouble(), 1.23457);
// EXPECT_DOUBLE_EQ(d[2u].GetDouble(), 0.123457);
// EXPECT_DOUBLE_EQ(d[3u].GetDouble(), 1234570);
// }
//}
TEST(Writer, Transcode) {
const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"dollar\":\"\x24\",\"cents\":\"\xC2\xA2\",\"euro\":\"\xE2\x82\xAC\",\"gclef\":\"\xF0\x9D\x84\x9E\"}";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment