From d5d18cf6941518a33901a0ce522d38787269b8bb Mon Sep 17 00:00:00 2001 From: John Stiles Date: Tue, 14 Mar 2017 23:48:41 -0700 Subject: [PATCH] Fix template length optimization issue in PrettyWriter Missed PrettyWriter in the initial fix for Issue #889 --- include/rapidjson/prettywriter.h | 24 ++++-------------------- test/unittest/prettywritertest.cpp | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index a9d0f02..b68b687 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -107,8 +107,7 @@ public: return Base::WriteString(str, length); } - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) String(const T* str, SizeType length, bool copy = false) { + bool String(const Ch* str, SizeType length, bool copy = false) { RAPIDJSON_ASSERT(str != 0); (void)copy; PrettyPrefix(kStringType); @@ -127,8 +126,7 @@ public: return Base::WriteStartObject(); } - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) Key(const T* str, SizeType length, bool copy = false) { return String(str, length, copy); } + bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } #if RAPIDJSON_HAS_STDSTRING bool Key(const std::basic_string& str) { @@ -186,22 +184,8 @@ public: //@{ //! Simpler but slower overload. - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) String(const T* const& str) { return String(str, internal::StrLen(str)); } - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) Key(const T* const& str) { return Key(str, internal::StrLen(str)); } - - //! The compiler can give us the length of quoted strings for free. - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) String(const T (&str)[N]) { - RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated) - return String(str, N-1); - } - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) Key(const T (&str)[N]) { - RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated) - return Key(str, N-1); - } + bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } //@} diff --git a/test/unittest/prettywritertest.cpp b/test/unittest/prettywritertest.cpp index 2891c76..bfc736f 100644 --- a/test/unittest/prettywritertest.cpp +++ b/test/unittest/prettywritertest.cpp @@ -258,6 +258,20 @@ TEST(PrettyWriter, InvalidEventSequence) { } } +TEST(PrettyWriter, Issue_889) { + char buf[100] = "Hello"; + + StringBuffer buffer; + PrettyWriter writer(buffer); + writer.StartArray(); + writer.String(buf); + writer.EndArray(); + + EXPECT_STREQ("[\n \"Hello\"\n]", buffer.GetString()); + EXPECT_TRUE(writer.IsComplete()); \ +} + + #if RAPIDJSON_HAS_CXX11_RVALUE_REFS static PrettyWriter WriterGen(StringBuffer &target) {