Skip to content

Commit 61f8c4e

Browse files
committed
Quoted strings to String() or Key() are auto-sized by template
Same fix as previous commit, to prettywriter
1 parent dd97ede commit 61f8c4e

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

include/rapidjson/prettywriter.h

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

110-
bool String(const Ch* str, SizeType length, bool copy = false) {
110+
template <typename T>
111+
bool String(const T* str, SizeType length, bool copy = false, RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) {
111112
RAPIDJSON_ASSERT(str != 0);
112113
(void)copy;
113114
PrettyPrefix(kStringType);
@@ -126,7 +127,8 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
126127
return Base::WriteStartObject();
127128
}
128129

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

131133
#if RAPIDJSON_HAS_STDSTRING
132134
bool Key(const std::basic_string<Ch>& str) {
@@ -184,8 +186,16 @@ class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding,
184186
//@{
185187

186188
//! Simpler but slower overload.
187-
bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
188-
bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
189+
template <typename T>
190+
bool String(const T* const& str, RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) { return String(str, internal::StrLen(str)); }
191+
template <typename T>
192+
bool Key(const T* const& str, RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) { 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+
bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) { return String(str, N-1); }
197+
template <typename T, size_t N>
198+
bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame<Ch, T>))) { return Key(str, N-1); }
189199

190200
//@}
191201

0 commit comments

Comments
 (0)