Skip to content

Commit d5d18cf

Browse files
committed
Fix template length optimization issue in PrettyWriter
Missed PrettyWriter in the initial fix for Issue Tencent#889
1 parent f0c108b commit d5d18cf

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

include/rapidjson/prettywriter.h

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
107107
return Base::WriteString(str, length);
108108
}
109109

110-
template <typename T>
111-
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T* str, SizeType length, bool copy = false) {
110+
bool String(const Ch* str, SizeType length, bool copy = false) {
112111
RAPIDJSON_ASSERT(str != 0);
113112
(void)copy;
114113
PrettyPrefix(kStringType);
@@ -127,8 +126,7 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
127126
return Base::WriteStartObject();
128127
}
129128

130-
template <typename T>
131-
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T* str, SizeType length, bool copy = false) { return String(str, length, copy); }
129+
bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
132130

133131
#if RAPIDJSON_HAS_STDSTRING
134132
bool Key(const std::basic_string<Ch>& str) {
@@ -186,22 +184,8 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
186184
//@{
187185

188186
//! Simpler but slower overload.
189-
template <typename T>
190-
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T* const& str) { return String(str, internal::StrLen(str)); }
191-
template <typename T>
192-
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T* const& str) { return Key(str, internal::StrLen(str)); }
193-
194-
//! The compiler can give us the length of quoted strings for free.
195-
template <typename T, size_t N>
196-
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T (&str)[N]) {
197-
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
198-
return String(str, N-1);
199-
}
200-
template <typename T, size_t N>
201-
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T (&str)[N]) {
202-
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
203-
return Key(str, N-1);
204-
}
187+
bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
188+
bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
205189

206190
//@}
207191

test/unittest/prettywritertest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,20 @@ TEST(PrettyWriter, InvalidEventSequence) {
258258
}
259259
}
260260

261+
TEST(PrettyWriter, Issue_889) {
262+
char buf[100] = "Hello";
263+
264+
StringBuffer buffer;
265+
PrettyWriter<StringBuffer> writer(buffer);
266+
writer.StartArray();
267+
writer.String(buf);
268+
writer.EndArray();
269+
270+
EXPECT_STREQ("[\n \"Hello\"\n]", buffer.GetString());
271+
EXPECT_TRUE(writer.IsComplete()); \
272+
}
273+
274+
261275
#if RAPIDJSON_HAS_CXX11_RVALUE_REFS
262276

263277
static PrettyWriter<StringBuffer> WriterGen(StringBuffer &target) {

0 commit comments

Comments
 (0)